forked from kartik-v/yii2-widget-activeform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ActiveField.php
762 lines (718 loc) · 31.3 KB
/
ActiveField.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015
* @package yii2-widgets
* @subpackage yii2-widget-activeform
* @version 1.3.0
*/
namespace kartik\form;
use yii\helpers\Html;
use yii\helpers\ArrayHelper;
/**
* Extends the ActiveField widget to handle various
* bootstrap form types and handle input groups.
*
* ADDITIONAL VARIABLES/PARAMETERS:
* ===============================
*
* @param boolean $autoPlaceholder whether to display the label as a placeholder (default false)
* @param array $addon whether to prepend or append an addon to an input group - contains these keys:
* - prepend: array the prepend addon configuration
* - content: string the prepend addon content
* - asButton: boolean whether the addon is a button or button group. Defaults to false.
* - options: array the HTML attributes to be added to the container.
* - append: array the append addon configuration
* - content: string/array the append addon content
* - asButton: boolean whether the addon is a button or button group. Defaults to false.
* - options: array the HTML attributes to be added to the container.
* - groupOptions: array HTML options for the input group
* - contentBefore: string content placed before addon
* - contentAfter: string content placed after addon
*
* Example(s):
* ```php
* echo $this->form->field($model, 'email', ['addon' => ['type'=>'prepend', 'content'=>'@']]);
* echo $this->form->field($model, 'amount_paid', ['addon' => ['type'=>'append', 'content'=>'.00']]);
* echo $this->form->field($model, 'phone', ['addon' => ['type'=>'prepend', 'content'=>'<i class="glyphicon
* glyphicon-phone']]);
* ```
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class ActiveField extends \yii\widgets\ActiveField
{
const TYPE_RADIO = 'radio';
const TYPE_CHECKBOX = 'checkbox';
const STYLE_INLINE = 'inline';
const MULTI_SELECT_HEIGHT = '145px';
/**
* @var string content to be placed before input
*/
public $contentBeforeInput = '';
/**
* @var string content to be placed after input
*/
public $contentAfterInput = '';
/**
* @var array addon options for text and password inputs
*/
public $addon = [];
/**
* @var string CSS classname to add to the input
*/
public $addClass = 'form-control';
/**
* @var string the static value for the field to be displayed
* for the static input OR when the form is in staticOnly mode.
* This value is not HTML encoded.
*/
public $staticValue;
/**
* @var boolean|string whether to show labels for the field. Should
* be one of the following values:
* - `true`: show labels for the field
* - `false`: hide labels for the field
* - `ActiveForm::SCREEN_READER`: show in screen reader only (hide from normal display)
*/
public $showLabels;
/**
* @var boolean whether to show errors for the field
*/
public $showErrors;
/**
* @var boolean whether to show hints for the field
*/
public $showHints;
/**
* @var boolean whether the label is to be hidden and auto-displayed as a placeholder
*/
public $autoPlaceholder;
/**
* @var boolean whether the input is to be offset (like for checkbox or radio).
*/
private $_offset = false;
/**
* @var boolean the container for multi select
*/
private $_multiselect = '';
/**
* @var boolean is it a static input
*/
private $_isStatic = false;
/**
* @inherit doc
*/
public function init()
{
parent::init();
if ($this->form->type === ActiveForm::TYPE_INLINE && !isset($this->autoPlaceholder)) {
$this->autoPlaceholder = true;
} elseif (!isset($this->autoPlaceholder)) {
$this->autoPlaceholder = false;
}
if ($this->form->type === ActiveForm::TYPE_HORIZONTAL || $this->form->type === ActiveForm::TYPE_VERTICAL) {
Html::addCssClass($this->labelOptions, 'control-label');
}
if ($this->showLabels === ActiveForm::SCREEN_READER) {
Html::addCssClass($this->labelOptions, ActiveForm::SCREEN_READER);
}
}
/**
* Renders a static input (display only).
*
* @param array $options the tag options in terms of name-value pairs.
*
* @return ActiveField object
*/
public function staticInput($options = [])
{
$content = isset($this->staticValue) ? $this->staticValue : Html::getAttributeValue($this->model,
$this->attribute);
Html::addCssClass($options, 'form-control-static');
$this->parts['{input}'] = Html::tag('div', $content, $options);
$this->_isStatic = true;
return $this;
}
/**
* @inherit doc
*/
public function input($type, $options = [])
{
$this->initPlaceholder($options);
if ($type != 'range' || $type != 'color') {
Html::addCssClass($options, $this->addClass);
}
$this->initDisability($options);
return parent::input($type, $options);
}
/**
* Initializes placeholder based on $autoPlaceholder
*
* @param array $options the HTML attributes for the input
*/
protected function initPlaceholder(&$options)
{
if ($this->autoPlaceholder) {
$label = $this->model->getAttributeLabel(Html::getAttributeName($this->attribute));
$this->inputOptions['placeholder'] = $label;
$options['placeholder'] = $label;
}
}
/**
* Validates and sets disabled or readonly inputs
*
* @param array $options the HTML attributes for the input
*/
protected function initDisability(&$options)
{
if ($this->form->disabled && !isset($options['disabled'])) {
$options['disabled'] = true;
}
if ($this->form->readonly && !isset($options['readonly'])) {
$options['readonly'] = true;
}
}
/**
* @inherit doc
*/
public function textInput($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::textInput($options);
}
/**
* @inherit doc
*/
public function passwordInput($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::passwordInput($options);
}
/**
* @inherit doc
*/
public function textarea($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::textarea($options);
}
/**
* @inherit doc
*/
public function dropDownList($items, $options = [])
{
$this->initDisability($options);
Html::addCssClass($options, $this->addClass);
return parent::dropDownList($items, $options);
}
/**
* @inherit doc
*/
public function listBox($items, $options = [])
{
$this->initDisability($options);
Html::addCssClass($options, $this->addClass);
return parent::listBox($items, $options);
}
/**
* Renders a radio button.
* This method will generate the "checked" tag attribute according to the model attribute value.
*
* @param array $options the tag options in terms of name-value pairs. The following options are specially
* handled:
*
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
* it will take the default value '0'. This method will render a hidden input so that if the radio button
* is not checked and is submitted, the value of this attribute will still be submitted to the server
* via the hidden input.
* - label: string, a label displayed next to the radio button. It will NOT be HTML-encoded. Therefore you can
* pass
* in HTML code such as an image tag. If this is is coming from end users, you should [[Html::encode()]] it to
* prevent XSS attacks. When this option is specified, the radio button will be enclosed by a label tag.
* - labelOptions: array, the HTML attributes for the label tag. This is only used when the "label" option is
* specified.
* - container: boolean|array, the HTML attributes for the checkbox container. If this is set to false, no
* container will be rendered. The special option `tag` will be recognized which defaults to `div`. This
* defaults to:
* `['tag' => 'div', 'class'=>'radio']`
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.
* @param boolean $enclosedByLabel whether to enclose the radio within the label.
* If true, the method will still use [[template]] to layout the checkbox and the error message
* except that the radio is enclosed by the label tag.
*
* @return ActiveField object
*/
public function radio($options = [], $enclosedByLabel = true)
{
return $this->getToggleField(self::TYPE_RADIO, $options, $enclosedByLabel);
}
/**
* Generates a toggle field (checkbox or radio)
*
* @param string $type the toggle input type 'checkbox' or 'radio'.
* @param array $options options (name => config) for the toggle input list container tag.
*
* @return ActiveField object
*/
protected function getToggleField($type = self::TYPE_CHECKBOX, $options = [], $enclosedByLabel = true)
{
$this->initDisability($options);
$this->_offset = true;
$inputType = 'active' . ucfirst($type);
$disabled = ArrayHelper::getValue($options, 'disabled', false);
$readonly = ArrayHelper::getValue($options, 'readonly', false);
$css = $disabled ? $type . ' disabled' : $type;
$container = ArrayHelper::remove($options, 'container', ['class' => $css]);
if ($enclosedByLabel) {
$this->parts['{label}'] = '';
} else {
if (isset($options['label']) && !isset($this->parts['{label}'])) {
$this->parts['label'] = $options['label'];
if (!empty($options['labelOptions'])) {
$this->labelOptions = $options['labelOptions'];
}
}
$options['label'] = null;
$container = false;
unset($options['labelOptions']);
}
$input = Html::$inputType($this->model, $this->attribute, $options);
if (is_array($container)) {
$tag = ArrayHelper::remove($container, 'tag', 'div');
$input = Html::tag($tag, $input, $container);
}
$this->parts['{input}'] = $input;
$this->adjustLabelFor($options);
return $this;
}
/**
* Renders a checkbox.
* This method will generate the "checked" tag attribute according to the model attribute value.
*
* @param array $options the tag options in terms of name-value pairs. The following options are specially
* handled:
*
* - uncheck: string, the value associated with the uncheck state of the radio button. If not set,
* it will take the default value '0'. This method will render a hidden input so that if the radio button
* is not checked and is submitted, the value of this attribute will still be submitted to the server
* via the hidden input.
* - label: string, a label displayed next to the checkbox. It will NOT be HTML-encoded. Therefore you can pass
* in HTML code such as an image tag. If this is is coming from end users, you should [[Html::encode()]] it to
* prevent XSS attacks. When this option is specified, the checkbox will be enclosed by a label tag.
* - labelOptions: array, the HTML attributes for the label tag. This is only used when the "label" option is
* specified.
* - container: boolean|array, the HTML attributes for the checkbox container. If this is set to false, no
* container will be rendered. The special option `tag` will be recognized which defaults to `div`. This
* defaults to:
* `['tag' => 'div', 'class'=>'checkbox']`
*
* The rest of the options will be rendered as the attributes of the resulting tag. The values will
* be HTML-encoded using [[Html::encode()]]. If a value is null, the corresponding attribute will not be rendered.
* @param boolean $enclosedByLabel whether to enclose the checkbox within the label.
* If true, the method will still use [[template]] to layout the checkbox and the error message
* except that the checkbox is enclosed by the label tag.
*
* @return ActiveField object
*/
public function checkbox($options = [], $enclosedByLabel = true)
{
return $this->getToggleField(self::TYPE_CHECKBOX, $options, $enclosedByLabel);
}
/**
* Renders a multi select list box. This control extends the checkboxList and radioList
* available in yii\widgets\ActiveField - to display a scrolling multi select list box.
*
* @param array $items the data item used to generate the checkboxes or radio.
* @param array $options the options for checkboxList or radioList. Additional parameters
* - height: string, the height of the multiselect control - defaults to 145px
* - selector: string, whether checkbox or radio - defaults to checkbox
* - container: array, options for the multiselect container
* - unselect: string, the value that should be submitted when none of the radio buttons is selected.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
* - inline: boolean, whether the list should be displayed as a series on the same line, default is false
* - selector: string, whether the selection input is [[self::TYPE_RADIO]] or [[self::TYPE_CHECKBOX]]
*
* @return ActiveField object
*/
public function multiselect($items, $options = [])
{
$this->initDisability($options);
$options['encode'] = false;
$height = ArrayHelper::remove($options, 'height', self::MULTI_SELECT_HEIGHT);
$selector = ArrayHelper::remove($options, 'selector', self::TYPE_CHECKBOX);
$container = ArrayHelper::remove($options, 'container', []);
Html::addCssStyle($container, 'height:' . $height, true);
Html::addCssClass($container, $this->addClass . ' input-multiselect');
$container['tabindex'] = 0;
$this->_multiselect = Html::tag('div', '{input}', $container);
return $selector == self::TYPE_RADIO ? $this->radioList($items, $options) : $this->checkboxList($items,
$options);
}
/**
* Renders a list of radio toggle buttons.
* @see http://getbootstrap.com/javascript/#buttons-checkbox-radio
*
* @param array $items the data item used to generate the radios.
* The array values are the labels, while the array keys are the corresponding radio values.
* Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the radio button list. The following options are specially handled:
*
* - unselect: string, the value that should be submitted when none of the radios is selected.
* By setting this option, a hidden input will be generated. If you do not want any hidden input,
* you should explicitly set this option as null.
* - separator: string, the HTML code that separates items.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $checked, $value)
* ~~~
*
* where $index is the zero-based index of the radio button in the whole list; $label
* is the label for the radio button; and $name, $value and $checked represent the name,
* value and the checked status of the radio button input.
*
* @return ActiveField object
*/
public function radioButtonGroup($items, $options = [])
{
return $this->getToggleFieldList(self::TYPE_RADIO, $items, $options, true);
}
/**
* Renders a list of checkbox toggle buttons.
* @see http://getbootstrap.com/javascript/#buttons-checkbox-radio
*
* @param array $items the data item used to generate the checkboxes.
* The array values are the labels, while the array keys are the corresponding checkbox values.
* Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the checkbox button list. The following options are specially handled:
*
* - unselect: string, the value that should be submitted when none of the checkboxes is selected.
* By setting this option, a hidden input will be generated. If you do not want any hidden input,
* you should explicitly set this option as null.
* - separator: string, the HTML code that separates items.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $checked, $value)
* ~~~
*
* where $index is the zero-based index of the checkbox button in the whole list; $label
* is the label for the checkbox button; and $name, $value and $checked represent the name,
* value and the checked status of the checkbox button input.
*
* @return ActiveField object
*/
public function checkboxButtonGroup($items, $options = [])
{
return $this->getToggleFieldList(self::TYPE_CHECKBOX, $items, $options, true);
}
/**
* Renders a list of radio buttons.
* A radio button list is like a checkbox list, except that it only allows single selection.
* The selection of the radio buttons is taken from the value of the model attribute.
*
* @param array $items the data item used to generate the radio buttons.
* The array keys are the labels, while the array values are the corresponding radio button values.
* Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the radio button list. The following options are specially
* handled:
*
* - unselect: string, the value that should be submitted when none of the radio buttons is selected.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - inline: boolean, whether the list should be displayed as a series on the same line, default is false
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $checked, $value)
* ~~~
*
* where $index is the zero-based index of the radio button in the whole list; $label
* is the label for the radio button; and $name, $value and $checked represent the name,
* value and the checked status of the radio button input.
*
* @return ActiveField object
*/
public function radioList($items, $options = [])
{
return $this->getToggleFieldList(self::TYPE_RADIO, $items, $options);
}
/**
* Renders a list of checkboxes / radio buttons.
* The selection of the checkbox / radio buttons is taken from the value of the model attribute.
*
* @param string $type the toggle input type 'checkbox' or 'radio'.
* @param array $items the data item used to generate the checkbox / radio buttons.
* The array keys are the labels, while the array values are the corresponding checkbox / radio button values.
* Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the checkbox / radio button list. The following options are
* specially handled:
*
* - unselect: string, the value that should be submitted when none of the checkbox / radio buttons is selected.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - inline: boolean, whether the list should be displayed as a series on the same line, default is false
* - disabledItems: array, the list of values that will be disabled.
* - readonlyItems: array, the list of values that will be readonly.
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $checked, $value)
* ~~~
*
* where $index is the zero-based index of the checkbox/ radio button in the whole list; $label
* is the label for the checkbox/ radio button; and $name, $value and $checked represent the name,
* value and the checked status of the checkbox/ radio button input.
*
* @param bool $asButtonGroup whether to generate the toggle list as a bootstrap button group
*
* @return ActiveField object
*/
protected function getToggleFieldList($type, $items, $options = [], $asButtonGroup = false)
{
$disabled = ArrayHelper::remove($options, 'disabledItems', []);
$readonly = ArrayHelper::remove($options, 'readonlyItems', []);
if ($asButtonGroup) {
Html::addCssClass($options, 'btn-group');
$options['data-toggle'] = 'buttons';
$options['inline'] = true;
if (!isset($options['itemOptions']['labelOptions']['class'])) {
$options['itemOptions']['labelOptions']['class'] = 'btn btn-default';
}
}
$inline = ArrayHelper::remove($options, 'inline', false);
$inputType = "{$type}List";
$this->initDisability($options['itemOptions']);
$css = $this->form->disabled ? ' disabled' : '';
$css = $this->form->readonly ? $css . ' readonly' : $css;
if ($inline && !isset($options['itemOptions']['labelOptions']['class'])) {
$options['itemOptions']['labelOptions']['class'] = "{$type}-inline{$css}";
} elseif (!isset($options['item'])) {
$labelOptions = ArrayHelper::getValue($options, 'itemOptions.labelOptions');
$options['item'] = function ($index, $label, $name, $checked, $value)
use ($type, $css, $disabled, $readonly, $asButtonGroup, $labelOptions) {
$opts = [
'label' => $label,
'value' => $value,
'disabled' => $this->form->disabled,
'readonly' => $this->form->readonly,
];
if ($asButtonGroup && $checked) {
Html::addCssClass($labelOptions, 'active');
}
if (!empty($disabled) && in_array($value, $disabled) || $this->form->disabled) {
Html::addCssClass($labelOptions, 'disabled');
$opts['disabled'] = true;
}
if (!empty($readonly) && in_array($value, $readonly) || $this->form->readonly) {
Html::addCssClass($labelOptions, 'disabled');
$opts['readonly'] = true;
}
$opts['labelOptions'] = $labelOptions;
$out = Html::$type($name, $checked, $opts);
return $asButtonGroup ? $out : "<div class='{$type}{$css}'>{$out}</div>";
};
}
return parent::$inputType($items, $options);
}
/**
* Renders a list of checkboxes.
* A checkbox list allows multiple selection, like [[listBox()]].
* As a result, the corresponding submitted value is an array.
* The selection of the checkbox list is taken from the value of the model attribute.
*
* @param array $items the data item used to generate the checkboxes.
* The array values are the labels, while the array keys are the corresponding checkbox values.
* Note that the labels will NOT be HTML-encoded, while the values will.
* @param array $options options (name => config) for the checkbox list. The following options are specially
* handled:
*
* - unselect: string, the value that should be submitted when none of the checkboxes is selected.
* By setting this option, a hidden input will be generated.
* - separator: string, the HTML code that separates items.
* - inline: boolean, whether the list should be displayed as a series on the same line, default is false
* - item: callable, a callback that can be used to customize the generation of the HTML code
* corresponding to a single item in $items. The signature of this callback must be:
*
* ~~~
* function ($index, $label, $name, $checked, $value)
* ~~~
*
* where $index is the zero-based index of the checkbox in the whole list; $label
* is the label for the checkbox; and $name, $value and $checked represent the name,
* value and the checked status of the checkbox input.
*
* @return ActiveField object
*/
public function checkboxList($items, $options = [])
{
return $this->getToggleFieldList(self::TYPE_CHECKBOX, $items, $options);
}
/**
* @inherit doc
*/
public function widget($class, $config = [])
{
if (property_exists($class, 'disabled') && property_exists($class, 'readonly')) {
$this->initDisability($config);
}
return parent::widget($class, $config);
}
/**
* @inherit doc
*/
public function label($label = null, $options = [])
{
if ($label === false) {
$this->showLabels = false;
}
return parent::label($label, $options);
}
/**
* @inherit doc
*/
public function render($content = null)
{
if ($this->form->staticOnly === true) {
$field = $this->staticInput();
$this->initTemplate();
$this->buildTemplate();
return parent::render(null);
}
$this->initTemplate();
$this->initPlaceholder($this->inputOptions);
$this->initAddon();
$this->initDisability($this->inputOptions);
$this->buildTemplate();
return parent::render($content);
}
/**
* Initializes template based on layout settings for label, input,
* error and hint blocks and for various bootstrap 3 form layouts
*/
protected function initTemplate()
{
/**
* @var ActiveForm $form
*/
$form = $this->form;
$inputDivClass = '';
$errorDivClass = '';
$showLabels = isset($this->showLabels) ? $this->showLabels :
ArrayHelper::getValue($form->formConfig, 'showLabels', true);
$showErrors = isset($this->showErrors) ? $this->showErrors :
ArrayHelper::getValue($form->formConfig, 'showErrors', true);
$showHints = isset($this->showHints) ? $this->showHints :
ArrayHelper::getValue($form->formConfig, 'showHints', true);
if (!isset($this->parts['{hint}'])) {
$showHints = false;
}
if ($form->hasInputCss()) {
$offsetDivClass = $form->getOffsetCss();
$inputDivClass = ($this->_offset) ? $offsetDivClass : $form->getInputCss();
$error = $showErrors ? "{error}\n" : "";
if ($showLabels === false) {
$size = ArrayHelper::getValue($form->formConfig, 'deviceSize', ActiveForm::SIZE_MEDIUM);
$errorDivClass = "col-{$size}-{$form->fullSpan}";
$inputDivClass = $errorDivClass;
} elseif ($form->hasOffsetCss()) {
$errorDivClass = $offsetDivClass;
}
}
if ($this->autoPlaceholder && $showLabels !== ActiveForm::SCREEN_READER) {
$showLabels = false;
}
$input = '{input}';
$label = '{label}';
$error = '{error}';
$hint = '{hint}';
if (!empty($inputDivClass)) {
$input = "<div class='{$inputDivClass}'>{input}</div>";
}
if (!empty($this->_multiselect)) {
$input = str_replace('{input}', $this->_multiselect, $input);
}
if ($this->_isStatic && $this->showErrors !== true) {
$showErrors = false;
}
if (!empty($errorDivClass) && $showErrors) {
$error = "<div class='{$errorDivClass}'>{error}</div>";
}
if (!empty($errorDivClass) && $showHints) {
$hint = "<div class='{$errorDivClass}'>{hint}</div>";
}
$this->template = strtr($this->template, [
'{label}' => $showLabels ? $label : '',
'{input}' => $input,
'{error}' => $showErrors ? $error : '',
'{hint}' => $showHints ? $hint : ''
]);
}
/**
* Builds the template based on content before and after input
*
* @return void
*/
protected function buildTemplate()
{
$this->template = strtr($this->template, [
'{input}' => $this->contentBeforeInput . '{input}' . $this->contentAfterInput
]);
}
/**
* Initializes the addon for text inputs
*/
protected function initAddon()
{
if (!empty($this->addon)) {
$addon = $this->addon;
$prepend = static::getAddonContent(ArrayHelper::getValue($addon, 'prepend', ''));
$append = static::getAddonContent(ArrayHelper::getValue($addon, 'append', ''));
$addonText = $prepend . '{input}' . $append;
$group = ArrayHelper::getValue($addon, 'groupOptions', []);
Html::addCssClass($group, 'input-group');
$contentBefore = ArrayHelper::getValue($addon, 'contentBefore', '');
$contentAfter = ArrayHelper::getValue($addon, 'contentAfter', '');
$addonText = Html::tag('div', $contentBefore . $addonText . $contentAfter, $group);
$this->template = str_replace('{input}', $addonText, $this->template);
}
}
/**
* Parses and returns addon content
*
* @param string /array $addon the addon parameter
*
* @return string
*/
public static function getAddonContent($addon)
{
if (is_array($addon)) {
$content = ArrayHelper::getValue($addon, 'content', '');
$options = ArrayHelper::getValue($addon, 'options', []);
if (ArrayHelper::getValue($addon, 'asButton', false) == true) {
Html::addCssClass($options, 'input-group-btn');
return Html::tag('div', $content, $options);
} else {
Html::addCssClass($options, 'input-group-addon');
return Html::tag('span', $content, $options);
}
}
return $addon;
}
}