Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

102 linhas
3.0KB

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