No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.

ActiveForm.php 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * @link http://www.yiiframework.com/
  4. * @copyright Copyright (c) 2008 Yii Software LLC
  5. * @license http://www.yiiframework.com/license/
  6. */
  7. namespace yii\bootstrap;
  8. use Yii;
  9. use yii\base\InvalidConfigException;
  10. /**
  11. * A Bootstrap 3 enhanced version of [[\yii\widgets\ActiveForm]].
  12. *
  13. * This class mainly adds the [[layout]] property to choose a Bootstrap 3 form layout.
  14. * So for example to render a horizontal form you would:
  15. *
  16. * ```php
  17. * use yii\bootstrap\ActiveForm;
  18. *
  19. * $form = ActiveForm::begin(['layout' => 'horizontal'])
  20. * ```
  21. *
  22. * This will set default values for the [[ActiveField]]
  23. * to render horizontal form fields. In particular the [[ActiveField::template|template]]
  24. * is set to `{label} {beginWrapper} {input} {error} {endWrapper} {hint}` and the
  25. * [[ActiveField::horizontalCssClasses|horizontalCssClasses]] are set to:
  26. *
  27. * ```php
  28. * [
  29. * 'offset' => 'col-sm-offset-3',
  30. * 'label' => 'col-sm-3',
  31. * 'wrapper' => 'col-sm-6',
  32. * 'error' => '',
  33. * 'hint' => 'col-sm-3',
  34. * ]
  35. * ```
  36. *
  37. * To get a different column layout in horizontal mode you can modify those options
  38. * through [[fieldConfig]]:
  39. *
  40. * ```php
  41. * $form = ActiveForm::begin([
  42. * 'layout' => 'horizontal',
  43. * 'fieldConfig' => [
  44. * 'template' => "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}",
  45. * 'horizontalCssClasses' => [
  46. * 'label' => 'col-sm-4',
  47. * 'offset' => 'col-sm-offset-4',
  48. * 'wrapper' => 'col-sm-8',
  49. * 'error' => '',
  50. * 'hint' => '',
  51. * ],
  52. * ],
  53. * ]);
  54. * ```
  55. *
  56. * @see ActiveField for details on the [[fieldConfig]] options
  57. * @see http://getbootstrap.com/css/#forms
  58. *
  59. * @author Michael Härtl <haertl.mike@gmail.com>
  60. * @since 2.0
  61. */
  62. class ActiveForm extends \yii\widgets\ActiveForm
  63. {
  64. /**
  65. * @var string the default field class name when calling [[field()]] to create a new field.
  66. * @see fieldConfig
  67. */
  68. public $fieldClass = 'yii\bootstrap\ActiveField';
  69. /**
  70. * @var array HTML attributes for the form tag. Default is `['role' => 'form']`.
  71. */
  72. public $options = ['role' => 'form'];
  73. /**
  74. * @var string the form layout. Either 'default', 'horizontal' or 'inline'.
  75. * By choosing a layout, an appropriate default field configuration is applied. This will
  76. * render the form fields with slightly different markup for each layout. You can
  77. * override these defaults through [[fieldConfig]].
  78. * @see \yii\bootstrap\ActiveField for details on Bootstrap 3 field configuration
  79. */
  80. public $layout = 'default';
  81. /**
  82. * @inheritdoc
  83. */
  84. public function init()
  85. {
  86. if (!in_array($this->layout, ['default', 'horizontal', 'inline'])) {
  87. throw new InvalidConfigException('Invalid layout type: ' . $this->layout);
  88. }
  89. if ($this->layout !== 'default') {
  90. Html::addCssClass($this->options, 'form-' . $this->layout);
  91. }
  92. parent::init();
  93. }
  94. /**
  95. * @inheritdoc
  96. * @return ActiveField the created ActiveField object
  97. */
  98. public function field($model, $attribute, $options = [])
  99. {
  100. return parent::field($model, $attribute, $options);
  101. }
  102. }