You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

384 line
14KB

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