ActiveField.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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\ArrayHelper;
  9. /**
  10. * A Bootstrap 3 enhanced version of [[\yii\widgets\ActiveField]].
  11. *
  12. * This class adds some useful features to [[\yii\widgets\ActiveField|ActiveField]] to render all
  13. * sorts of Bootstrap 3 form fields in different form layouts:
  14. *
  15. * - [[inputTemplate]] is an optional template to render complex inputs, for example input groups
  16. * - [[horizontalCssClasses]] defines the CSS grid classes to add to label, wrapper, error and hint
  17. * in horizontal forms
  18. * - [[inline]]/[[inline()]] is used to render inline [[checkboxList()]] and [[radioList()]]
  19. * - [[enableError]] can be set to `false` to disable to the error
  20. * - [[enableLabel]] can be set to `false` to disable to the label
  21. * - [[label()]] can be used with a `bool` argument to enable/disable the label
  22. *
  23. * There are also some new placeholders that you can use in the [[template]] configuration:
  24. *
  25. * - `{beginLabel}`: the opening label tag
  26. * - `{labelTitle}`: the label title for use with `{beginLabel}`/`{endLabel}`
  27. * - `{endLabel}`: the closing label tag
  28. * - `{beginWrapper}`: the opening wrapper tag
  29. * - `{endWrapper}`: the closing wrapper tag
  30. *
  31. * The wrapper tag is only used for some layouts and form elements.
  32. *
  33. * Note that some elements use slightly different defaults for [[template]] and other options.
  34. * You may want to override those predefined templates for checkboxes, radio buttons, checkboxLists
  35. * and radioLists in the [[\yii\widgets\ActiveForm::fieldConfig|fieldConfig]] of the
  36. * [[\yii\widgets\ActiveForm]]:
  37. *
  38. * - [[checkboxTemplate]] the template for checkboxes in default layout
  39. * - [[radioTemplate]] the template for radio buttons in default layout
  40. * - [[horizontalCheckboxTemplate]] the template for checkboxes in horizontal layout
  41. * - [[horizontalRadioTemplate]] the template for radio buttons in horizontal layout
  42. * - [[inlineCheckboxListTemplate]] the template for inline checkboxLists
  43. * - [[inlineRadioListTemplate]] the template for inline radioLists
  44. *
  45. * Example:
  46. *
  47. * ```php
  48. * use yii\bootstrap\ActiveForm;
  49. *
  50. * $form = ActiveForm::begin(['layout' => 'horizontal']);
  51. *
  52. * // Form field without label
  53. * echo $form->field($model, 'demo', [
  54. * 'inputOptions' => [
  55. * 'placeholder' => $model->getAttributeLabel('demo'),
  56. * ],
  57. * ])->label(false);
  58. *
  59. * // Inline radio list
  60. * echo $form->field($model, 'demo')->inline()->radioList($items);
  61. *
  62. * // Control sizing in horizontal mode
  63. * echo $form->field($model, 'demo', [
  64. * 'horizontalCssClasses' => [
  65. * 'wrapper' => 'col-sm-2',
  66. * ]
  67. * ]);
  68. *
  69. * // With 'default' layout you would use 'template' to size a specific field:
  70. * echo $form->field($model, 'demo', [
  71. * 'template' => '{label} <div class="row"><div class="col-sm-4">{input}{error}{hint}</div></div>'
  72. * ]);
  73. *
  74. * // Input group
  75. * echo $form->field($model, 'demo', [
  76. * 'inputTemplate' => '<div class="input-group"><span class="input-group-addon">@</span>{input}</div>',
  77. * ]);
  78. *
  79. * ActiveForm::end();
  80. * ```
  81. *
  82. * @see \yii\bootstrap\ActiveForm
  83. * @see http://getbootstrap.com/css/#forms
  84. *
  85. * @author Michael Härtl <haertl.mike@gmail.com>
  86. * @since 2.0
  87. */
  88. class ActiveField extends \yii\widgets\ActiveField
  89. {
  90. /**
  91. * @var bool whether to render [[checkboxList()]] and [[radioList()]] inline.
  92. */
  93. public $inline = false;
  94. /**
  95. * @var string|null optional template to render the `{input}` placeholder content
  96. */
  97. public $inputTemplate;
  98. /**
  99. * @var array options for the wrapper tag, used in the `{beginWrapper}` placeholder
  100. */
  101. public $wrapperOptions = [];
  102. /**
  103. * @var null|array CSS grid classes for horizontal layout. This must be an array with these keys:
  104. * - 'offset' the offset grid class to append to the wrapper if no label is rendered
  105. * - 'label' the label grid class
  106. * - 'wrapper' the wrapper grid class
  107. * - 'error' the error grid class
  108. * - 'hint' the hint grid class
  109. */
  110. public $horizontalCssClasses = [];
  111. /**
  112. * @var string the template for checkboxes in default layout
  113. */
  114. public $checkboxTemplate = "<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
  115. /**
  116. * @var string the template for radios in default layout
  117. */
  118. public $radioTemplate = "<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n{error}\n{hint}\n</div>";
  119. /**
  120. * @var string the template for checkboxes in horizontal layout
  121. */
  122. public $horizontalCheckboxTemplate = "{beginWrapper}\n<div class=\"checkbox\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
  123. /**
  124. * @var string the template for radio buttons in horizontal layout
  125. */
  126. public $horizontalRadioTemplate = "{beginWrapper}\n<div class=\"radio\">\n{beginLabel}\n{input}\n{labelTitle}\n{endLabel}\n</div>\n{error}\n{endWrapper}\n{hint}";
  127. /**
  128. * @var string the template for inline checkboxLists
  129. */
  130. public $inlineCheckboxListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  131. /**
  132. * @var string the template for inline radioLists
  133. */
  134. public $inlineRadioListTemplate = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  135. /**
  136. * @var bool whether to render the error. Default is `true` except for layout `inline`.
  137. */
  138. public $enableError = true;
  139. /**
  140. * @var bool whether to render the label. Default is `true`.
  141. */
  142. public $enableLabel = true;
  143. /**
  144. * {@inheritdoc}
  145. */
  146. public function __construct($config = [])
  147. {
  148. $layoutConfig = $this->createLayoutConfig($config);
  149. $config = ArrayHelper::merge($layoutConfig, $config);
  150. parent::__construct($config);
  151. }
  152. /**
  153. * {@inheritdoc}
  154. */
  155. public function render($content = null)
  156. {
  157. if ($content === null) {
  158. if (!isset($this->parts['{beginWrapper}'])) {
  159. $options = $this->wrapperOptions;
  160. $tag = ArrayHelper::remove($options, 'tag', 'div');
  161. $this->parts['{beginWrapper}'] = Html::beginTag($tag, $options);
  162. $this->parts['{endWrapper}'] = Html::endTag($tag);
  163. }
  164. if ($this->enableLabel === false) {
  165. $this->parts['{label}'] = '';
  166. $this->parts['{beginLabel}'] = '';
  167. $this->parts['{labelTitle}'] = '';
  168. $this->parts['{endLabel}'] = '';
  169. } elseif (!isset($this->parts['{beginLabel}'])) {
  170. $this->renderLabelParts();
  171. }
  172. if ($this->enableError === false) {
  173. $this->parts['{error}'] = '';
  174. }
  175. if ($this->inputTemplate) {
  176. $input = isset($this->parts['{input}']) ?
  177. $this->parts['{input}'] : Html::activeTextInput($this->model, $this->attribute, $this->inputOptions);
  178. $this->parts['{input}'] = strtr($this->inputTemplate, ['{input}' => $input]);
  179. }
  180. }
  181. return parent::render($content);
  182. }
  183. /**
  184. * {@inheritdoc}
  185. */
  186. public function checkbox($options = [], $enclosedByLabel = true)
  187. {
  188. if ($enclosedByLabel) {
  189. if (!isset($options['template'])) {
  190. $this->template = $this->form->layout === 'horizontal' ?
  191. $this->horizontalCheckboxTemplate : $this->checkboxTemplate;
  192. } else {
  193. $this->template = $options['template'];
  194. unset($options['template']);
  195. }
  196. if (isset($options['label'])) {
  197. $this->parts['{labelTitle}'] = $options['label'];
  198. }
  199. if ($this->form->layout === 'horizontal') {
  200. Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
  201. }
  202. $this->labelOptions['class'] = null;
  203. }
  204. return parent::checkbox($options, false);
  205. }
  206. /**
  207. * {@inheritdoc}
  208. */
  209. public function radio($options = [], $enclosedByLabel = true)
  210. {
  211. if ($enclosedByLabel) {
  212. if (!isset($options['template'])) {
  213. $this->template = $this->form->layout === 'horizontal' ?
  214. $this->horizontalRadioTemplate : $this->radioTemplate;
  215. } else {
  216. $this->template = $options['template'];
  217. unset($options['template']);
  218. }
  219. if (isset($options['label'])) {
  220. $this->parts['{labelTitle}'] = $options['label'];
  221. }
  222. if ($this->form->layout === 'horizontal') {
  223. Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
  224. }
  225. $this->labelOptions['class'] = null;
  226. }
  227. return parent::radio($options, false);
  228. }
  229. /**
  230. * {@inheritdoc}
  231. */
  232. public function checkboxList($items, $options = [])
  233. {
  234. if ($this->inline) {
  235. if (!isset($options['template'])) {
  236. $this->template = $this->inlineCheckboxListTemplate;
  237. } else {
  238. $this->template = $options['template'];
  239. unset($options['template']);
  240. }
  241. if (!isset($options['itemOptions'])) {
  242. $options['itemOptions'] = [
  243. 'labelOptions' => ['class' => 'checkbox-inline'],
  244. ];
  245. }
  246. } elseif (!isset($options['item'])) {
  247. $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
  248. $encode = ArrayHelper::getValue($options, 'encode', true);
  249. $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
  250. $options = array_merge([
  251. 'label' => $encode ? Html::encode($label) : $label,
  252. 'value' => $value
  253. ], $itemOptions);
  254. return '<div class="checkbox">' . Html::checkbox($name, $checked, $options) . '</div>';
  255. };
  256. }
  257. parent::checkboxList($items, $options);
  258. return $this;
  259. }
  260. /**
  261. * {@inheritdoc}
  262. */
  263. public function radioList($items, $options = [])
  264. {
  265. if ($this->inline) {
  266. if (!isset($options['template'])) {
  267. $this->template = $this->inlineRadioListTemplate;
  268. } else {
  269. $this->template = $options['template'];
  270. unset($options['template']);
  271. }
  272. if (!isset($options['itemOptions'])) {
  273. $options['itemOptions'] = [
  274. 'labelOptions' => ['class' => 'radio-inline'],
  275. ];
  276. }
  277. } elseif (!isset($options['item'])) {
  278. $itemOptions = isset($options['itemOptions']) ? $options['itemOptions'] : [];
  279. $encode = ArrayHelper::getValue($options, 'encode', true);
  280. $options['item'] = function ($index, $label, $name, $checked, $value) use ($itemOptions, $encode) {
  281. $options = array_merge([
  282. 'label' => $encode ? Html::encode($label) : $label,
  283. 'value' => $value
  284. ], $itemOptions);
  285. return '<div class="radio">' . Html::radio($name, $checked, $options) . '</div>';
  286. };
  287. }
  288. parent::radioList($items, $options);
  289. return $this;
  290. }
  291. /**
  292. * Renders Bootstrap static form control.
  293. * @param array $options the tag options in terms of name-value pairs. These will be rendered as
  294. * the attributes of the resulting tag. There are also a special options:
  295. *
  296. * - encode: bool, whether value should be HTML-encoded or not.
  297. *
  298. * @return $this the field object itself
  299. * @since 2.0.5
  300. * @see http://getbootstrap.com/css/#forms-controls-static
  301. */
  302. public function staticControl($options = [])
  303. {
  304. $this->adjustLabelFor($options);
  305. $this->parts['{input}'] = Html::activeStaticControl($this->model, $this->attribute, $options);
  306. return $this;
  307. }
  308. /**
  309. * {@inheritdoc}
  310. */
  311. public function label($label = null, $options = [])
  312. {
  313. if (is_bool($label)) {
  314. $this->enableLabel = $label;
  315. if ($label === false && $this->form->layout === 'horizontal') {
  316. Html::addCssClass($this->wrapperOptions, $this->horizontalCssClasses['offset']);
  317. }
  318. } else {
  319. $this->enableLabel = true;
  320. $this->renderLabelParts($label, $options);
  321. parent::label($label, $options);
  322. }
  323. return $this;
  324. }
  325. /**
  326. * @param bool $value whether to render a inline list
  327. * @return $this the field object itself
  328. * Make sure you call this method before [[checkboxList()]] or [[radioList()]] to have any effect.
  329. */
  330. public function inline($value = true)
  331. {
  332. $this->inline = (bool) $value;
  333. return $this;
  334. }
  335. /**
  336. * @param array $instanceConfig the configuration passed to this instance's constructor
  337. * @return array the layout specific default configuration for this instance
  338. */
  339. protected function createLayoutConfig($instanceConfig)
  340. {
  341. $config = [
  342. 'hintOptions' => [
  343. 'tag' => 'p',
  344. 'class' => 'help-block',
  345. ],
  346. 'errorOptions' => [
  347. 'tag' => 'p',
  348. 'class' => 'help-block help-block-error',
  349. ],
  350. 'inputOptions' => [
  351. 'class' => 'form-control',
  352. ],
  353. ];
  354. $layout = $instanceConfig['form']->layout;
  355. if ($layout === 'horizontal') {
  356. $config['template'] = "{label}\n{beginWrapper}\n{input}\n{error}\n{endWrapper}\n{hint}";
  357. $cssClasses = array_merge([
  358. 'offset' => 'col-sm-offset-3',
  359. 'label' => 'col-sm-3',
  360. 'wrapper' => 'col-sm-6',
  361. 'error' => '',
  362. 'hint' => 'col-sm-3',
  363. ], $this->horizontalCssClasses);
  364. if (isset($instanceConfig['horizontalCssClasses'])) {
  365. $cssClasses = ArrayHelper::merge($cssClasses, $instanceConfig['horizontalCssClasses']);
  366. }
  367. $config['horizontalCssClasses'] = $cssClasses;
  368. $config['wrapperOptions'] = ['class' => $cssClasses['wrapper']];
  369. $config['labelOptions'] = ['class' => 'control-label ' . $cssClasses['label']];
  370. $config['errorOptions']['class'] = 'help-block help-block-error ' . $cssClasses['error'];
  371. $config['hintOptions']['class'] = 'help-block ' . $cssClasses['hint'];
  372. } elseif ($layout === 'inline') {
  373. $config['labelOptions'] = ['class' => 'sr-only'];
  374. $config['enableError'] = false;
  375. }
  376. return $config;
  377. }
  378. /**
  379. * @param string|null $label the label or null to use model label
  380. * @param array $options the tag options
  381. */
  382. protected function renderLabelParts($label = null, $options = [])
  383. {
  384. $options = array_merge($this->labelOptions, $options);
  385. if ($label === null) {
  386. if (isset($options['label'])) {
  387. $label = $options['label'];
  388. unset($options['label']);
  389. } else {
  390. $attribute = Html::getAttributeName($this->attribute);
  391. $label = Html::encode($this->model->getAttributeLabel($attribute));
  392. }
  393. }
  394. if (!isset($options['for'])) {
  395. $options['for'] = Html::getInputId($this->model, $this->attribute);
  396. }
  397. $this->parts['{beginLabel}'] = Html::beginTag('label', $options);
  398. $this->parts['{endLabel}'] = Html::endTag('label');
  399. if (!isset($this->parts['{labelTitle}'])) {
  400. $this->parts['{labelTitle}'] = $label;
  401. }
  402. }
  403. }