ActiveField.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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\helpers\Html;
  9. use yii\helpers\ArrayHelper;
  10. /**
  11. * A Bootstrap 3 enhanced version of [[yii\widgets\ActiveField]].
  12. *
  13. * This class adds some useful features to [[yii\widgets\ActiveField|ActiveField]] to render all
  14. * sorts of Bootstrap 3 form fields in different form layouts:
  15. *
  16. * - [[inputTemplate]] is an optional template to render complex inputs, for example input groups
  17. * - [[horizontalClass]] defines the CSS grid classes to add to label, wrapper, error and hint
  18. * in horizontal forms
  19. * - [[inline]]/[[inline()]] is used to render inline [[checkboxList()]] and [[radioList()]]
  20. * - [[enableError]] can be set to `false` to disable to the error
  21. * - [[enableLabel]] can be set to `false` to disable to the label
  22. * - [[label()]] can be used with a `boolean` argument to enable/disable the label
  23. *
  24. * There are also some new placeholders that you can use in the [[template]] configuration:
  25. *
  26. * - `{beginLabel}`: the opening label tag
  27. * - `{labelTitle}`: the label title for use with `{beginLabel}`/`{endLabel}`
  28. * - `{endLabel}`: the closing label tag
  29. * - `{beginWrapper}`: the opening wrapper tag
  30. * - `{endWrapper}`: the closing wrapper tag
  31. *
  32. * The wrapper tag is only used for some layouts and form elements.
  33. *
  34. * Note that some elements use slightly different defaults for [[template]] and other options.
  35. * In particular the elements are [[checkbox()]], [[checkboxList()]] and [[radioList()]].
  36. * So to further customize these elements you may want to pass your custom options.
  37. *
  38. * Example:
  39. *
  40. * ```php
  41. * use yii\bootstrap\ActiveForm;
  42. *
  43. * $form = ActiveForm::begin(['layout' => 'horizontal'])
  44. *
  45. * // Form field without label
  46. * echo $form->field($model, 'demo', [
  47. * 'inputOptions' => [
  48. * 'placeholder' => $model->getAttributeLabel('demo'),
  49. * ],
  50. * ])->label(false);
  51. *
  52. * // Inline radio list
  53. * echo $form->field($model, 'demo')->inline()->radioList($items);
  54. *
  55. * // Control sizing in horizontal mode
  56. * echo $form->field($model, 'demo', [
  57. * 'horizontalCssClasses' => [
  58. * 'wrapper' => 'col-sm-2',
  59. * ]
  60. * ]);
  61. *
  62. * // With 'default' layout you would use 'template' to size a specific field:
  63. * // echo $form->field($model, 'demo', [
  64. * // 'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
  65. * // ]);
  66. *
  67. * // Input group
  68. * echo $form->field($model, 'demo', [
  69. * 'inputTemplate' => '<div class="input-group"><span class="input-group-addon">@</span>{input}</div>',
  70. * ]);
  71. *
  72. * ActiveForm::end();
  73. * ```
  74. *
  75. * @see \yii\bootstrap\ActiveForm
  76. * @see http://getbootstrap.com/css/#forms
  77. *
  78. * @author Michael Härtl <haertl.mike@gmail.com>
  79. * @since 2.0
  80. */
  81. class ActiveField extends \yii\widgets\ActiveField
  82. {
  83. /**
  84. * @var bool whether to render [[checkboxList()]] and [[radioList()]] inline.
  85. */
  86. public $inline = false;
  87. /**
  88. * @var string|null optional template to render the `{input}` placeholder content
  89. */
  90. public $inputTemplate;
  91. /**
  92. * @var array options for the wrapper tag, used in the `{beginWrapper}` placeholder
  93. */
  94. public $wrapperOptions = [];
  95. /**
  96. * @var null|array CSS grid classes for horizontal layout. This must be an array with these keys:
  97. * - 'offset' the offset grid class to append to the wrapper if no label is rendered
  98. * - 'label' the label grid class
  99. * - 'wrapper' the wrapper grid class
  100. * - 'error' the error grid class
  101. * - 'hint' the hint grid class
  102. */
  103. public $horizontalCssClasses;
  104. /**
  105. * @var bool whether to render the error. Default is `true` except for layout `inline`.
  106. */
  107. public $enableError = true;
  108. /**
  109. * @var bool whether to render the label. Default is `true`.
  110. */
  111. public $enableLabel = true;
  112. /**
  113. * @inheritdoc
  114. */
  115. public function __construct($config = [])
  116. {
  117. $layoutConfig = $this->createLayoutConfig($config);
  118. $config = ArrayHelper::merge($layoutConfig, $config);
  119. return parent::__construct($config);
  120. }
  121. /**
  122. * @inheritdoc
  123. */
  124. public function render($content = null)
  125. {
  126. if ($content === null) {
  127. if (!isset($this->parts['{beginWrapper}'])) {
  128. $options = $this->wrapperOptions;
  129. $tag = ArrayHelper::remove($options, 'tag', 'div');
  130. $this->parts['{beginWrapper}'] = Html::beginTag($tag, $options);
  131. $this->parts['{endWrapper}'] = Html::endTag($tag);
  132. }
  133. if ($this->enableLabel === false) {
  134. $this->parts['{label}'] = '';
  135. $this->parts['{beginLabel}'] = '';
  136. $this->parts['{labelTitle}'] = '';
  137. $this->parts['{endLabel}'] = '';
  138. } elseif (!isset($this->parts['{beginLabel}'])) {
  139. $this->renderLabelParts();
  140. }
  141. if ($this->enableError === false) {
  142. $this->parts['{error}'] = '';
  143. }
  144. if ($this->inputTemplate) {
  145. $input = isset($this->parts['{input}']) ?
  146. $this->parts['{input}'] : Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
  147. $this->parts['{input}'] = strtr($this->inputTemplate, ['{input}' => $input]);
  148. }
  149. }
  150. return parent::render($content);
  151. }
  152. /**
  153. * @inheritdoc
  154. */
  155. public function checkbox($options = [], $enclosedByLabel = true)
  156. {
  157. if ($enclosedByLabel) {
  158. if (!isset($options['template'])) {
  159. if ($this->form->layout === 'horizontal') {
  160. $this->template = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
  161. Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
  162. } else {
  163. $this->template = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
  164. }
  165. }
  166. $this->labelOptions['class'] = null;
  167. }
  168. parent::checkbox($options, false);
  169. return $this;
  170. }
  171. /**
  172. * @inheritdoc
  173. */
  174. public function checkboxList($items, $options = [])
  175. {
  176. if ($this->inline) {
  177. if (!isset($options['template'])) {
  178. $this->template = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  179. }
  180. if (!isset($options['itemOptions'])) {
  181. $options['itemOptions'] = [
  182. 'container' => false,
  183. 'labelOptions' => ['class' => 'checkbox-inline'],
  184. ];
  185. }
  186. }
  187. parent::checkboxList($items, $options);
  188. return $this;
  189. }
  190. /**
  191. * @inheritdoc
  192. */
  193. public function radioList($items, $options = [])
  194. {
  195. if ($this->inline) {
  196. if (!isset($options['template'])) {
  197. $this->template = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  198. }
  199. if (!isset($options['itemOptions'])) {
  200. $options['itemOptions'] = [
  201. 'container' => false,
  202. 'labelOptions' => ['class' => 'radio-inline'],
  203. ];
  204. }
  205. }
  206. parent::radioList($items, $options);
  207. return $this;
  208. }
  209. /**
  210. * @inheritdoc
  211. */
  212. public function label($label = null, $options = [])
  213. {
  214. if (is_bool($label)) {
  215. $this->enableLabel = $label;
  216. if ($label === false && $this->form->layout === 'horizontal') {
  217. Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
  218. }
  219. } else {
  220. $this->renderLabelParts($label, $options);
  221. parent::label($label, $options);
  222. }
  223. return $this;
  224. }
  225. /**
  226. * @param bool $value whether to render a inline list
  227. * @return static the field object itself
  228. * Make sure you call this method before [[checkboxList()]] or [[radioList()]] to have any effect.
  229. */
  230. public function inline($value = true)
  231. {
  232. $this->inline = (bool)$value;
  233. return $this;
  234. }
  235. /**
  236. * @param array $instanceConfig the configuration passed to this instance's constructor
  237. * @return array the layout specific default configuration for this instance
  238. */
  239. protected function createLayoutConfig($instanceConfig)
  240. {
  241. $config = [
  242. 'hintOptions' => [
  243. 'tag' => 'p',
  244. 'class' => 'help-block',
  245. ],
  246. 'errorOptions' => [
  247. 'tag' => 'p',
  248. 'class' => 'help-block',
  249. ],
  250. 'inputOptions' => [
  251. 'class' => 'form-control',
  252. ],
  253. ];
  254. $layout = $instanceConfig['form']->layout;
  255. if ($layout === 'horizontal') {
  256. $config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  257. $cssClasses = [
  258. 'offset' => 'col-sm-offset-3',
  259. 'label' => 'col-sm-3',
  260. 'wrapper' => 'col-sm-6',
  261. 'error' => '',
  262. 'hint' => 'col-sm-3',
  263. ];
  264. if (isset($instanceConfig['horizontalCssClasses'])) {
  265. $cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']);
  266. }
  267. $config['horizontalCssClasses'] = $cssClasses;
  268. $config['wrapperOptions'] = ['class' => $cssClasses['wrapper']];
  269. $config['labelOptions'] = ['class' => 'control-label ' . $cssClasses['label']];
  270. $config['errorOptions'] = ['class' => 'help-block ' . $cssClasses['error']];
  271. $config['hintOptions'] = ['class' => 'help-block ' . $cssClasses['hint']];
  272. } elseif ($layout === 'inline') {
  273. $config['labelOptions'] = ['class' => 'sr-only'];
  274. $config['enableError'] = false;
  275. }
  276. return $config;
  277. }
  278. /**
  279. * @param string|null $label the label or null to use model label
  280. * @param array $options the tag options
  281. */
  282. protected function renderLabelParts($label = null, $options = [])
  283. {
  284. $options = array_merge($this->labelOptions, $options);
  285. if ($label === null) {
  286. if (isset($options['label'])) {
  287. $label = $options['label'];
  288. unset($options['label']);
  289. } else {
  290. $attribute = Html::getAttributeName($this->attribute);
  291. $label = $this->model->getAttributeLabel($attribute);
  292. }
  293. }
  294. $this->parts['{beginLabel}'] = Html::beginTag('label', $options);
  295. $this->parts['{endLabel}'] = Html::endTag('label');
  296. $this->parts['{labelTitle}'] = Html::encode($label);
  297. }
  298. }