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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
<?php
/**
* @copyright Copyright © Kartik Visweswaran, Krajee.com, 2015 - 2018
* @package yii2-widgets
* @subpackage yii2-widget-activeform
* @version 1.4.9
*/
namespace kartik\form;
use yii\helpers\ArrayHelper;
use yii\helpers\Html;
use yii\helpers\Inflector;
use yii\widgets\ActiveField as YiiActiveField;
/**
* ActiveField represents a form input field within an [[ActiveForm]] and extends the [[YiiActiveField]] component
* to handle various bootstrap functionality like form types, input groups/addons, toggle buttons, feedback icons, and
* other enhancements
*
* Usage example with addons:
*
* ```php
* // $form is your active form instance
* echo $form->field($model, 'email', ['addon' => ['type'=>'prepend', 'content'=>'@']]);
* echo $form->field($model, 'amount_paid', ['addon' => ['type'=>'append', 'content'=>'.00']]);
* echo $form->field($model, 'phone', [
* 'addon' => [
* 'type'=>'prepend',
* 'content'=>'<i class="glyphicon glyphicon-phone"></i>'
* ]
* ]);
* ```
*
* Usage example with horizontal form and advanced field layout CSS configuration:
*
* ```php
* echo $form->field($model, 'email', ['labelSpan' => 2, 'deviceSize' => ActiveForm::SIZE_SMALL]]);
* echo $form->field($model, 'amount_paid', ['horizontalCssClasses' => ['wrapper' => 'hidden-xs']]);
* echo $form->field($model, 'phone', [
* 'horizontalCssClasses' => ['label' => 'col-md-2 col-sm-3 col-xs-12 myRedClass']
* ]);
* echo $form->field($model, 'special', [
* 'template' => '{beginLabel}For: {labelTitle}{endLabel}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}'
* ]);
* ```
*
* @author Kartik Visweswaran <kartikv2@gmail.com>
* @since 1.0
*/
class ActiveField extends YiiActiveField
{
/**
* An empty string value
*/
const NOT_SET = '';
/**
* HTML radio input type
*/
const TYPE_RADIO = 'radio';
/**
* HTML checkbox input type
*/
const TYPE_CHECKBOX = 'checkbox';
/**
* Inline style flag for rendering checkboxes (as per bootstrap styling)
*/
const STYLE_INLINE = 'inline';
/**
* The default height for the Krajee multi select input
*/
const MULTI_SELECT_HEIGHT = '145px';
/**
* Default hint type that is displayed below the input
*/
const HINT_DEFAULT = 1;
/**
* Special hint type that allows display via an indicator icon or on hover/click of the field label
*/
const HINT_SPECIAL = 2;
/**
* @var array the list of hint keys that will be used by ActiveFieldHint jQuery plugin
*/
protected static $_pluginHintKeys = [
'iconCssClass',
'labelCssClass',
'contentCssClass',
'hideOnEscape',
'hideOnClickOut',
'title',
'placement',
'container',
'animation',
'delay',
'template',
'selector',
'viewport',
];
/**
* @var boolean whether to override the form layout styles and skip field formatting as per the form layout.
* Defaults to `false`.
*/
public $skipFormLayout = false;
/**
* @var integer the hint display type. If set to `self::HINT_DEFAULT`, the hint will be displayed as a text block below
* each input. If set to `self::HINT_SPECIAL`, then the `hintSettings` will be applied to display the field
* hint.
*/
public $hintType = self::HINT_DEFAULT;
/**
* @var array the settings for displaying the hint. These settings are parsed only if `hintType` is set to
* `self::HINT_SPECIAL`. The following properties are supported:
* - `showIcon`: _boolean_, whether to display the hint via a help icon indicator. Defaults to `true`.
* - `icon`: _string_, the markup to display the help icon. Defaults to `<i class="glyphicon glyphicon-question-sign
* text-info"></i>`.
* - `iconBesideInput`: _boolean_, whether to display the icon beside the input. Defaults to `false`. The following
* actions will be taken based on this setting:
* - if set to `false` the help icon is displayed beside the label and the `labelTemplate` setting is used to
* render the icon and label markups.
* - if set to `true` the help icon is displayed beside the input and the `inputTemplate` setting is used to
* render the icon and input markups.
* - `labelTemplate`: _string_, the template to render the help icon and the field label. Defaults to `{label}{help}`,
* where
* - `{label}` will be replaced by the ActiveField label content
* - `{help}` will be replaced by the help icon indicator markup
* - `inputTemplate`: _string_, the template to render the help icon and the field input. Defaults to `'<div
* style="width:90%; float:left">{input}</div><div style="padding-top:7px">{help}</div>',`, where
* - `{input}` will be replaced by the ActiveField input content
* - `{help}` will be replaced by the help icon indicator markup
* - `onLabelClick`: _boolean_, whether to display the hint on clicking the label. Defaults to `false`.
* - `onLabelHover`: _boolean_, whether to display the hint on hover of the label. Defaults to `true`.
* - `onIconClick`: _boolean_, whether to display the hint on clicking the icon. Defaults to `true`.
* - `onIconHover`: _boolean_, whether to display the hint on hover of the icon. Defaults to `false`.
* - `iconCssClass`: _string_, the CSS class appended to the `span` container enclosing the icon.
* - `labelCssClass`: _string_, the CSS class appended to the `span` container enclosing label text within label tag.
* - `contentCssClass`: _string_, the CSS class appended to the `span` container displaying help content within
* popover.
* - `hideOnEscape`: _boolean_, whether to hide the popover on clicking escape button on the keyboard. Defaults to `true`.
* - `hideOnClickOut`: _boolean_, whether to hide the popover on clicking outside the popover. Defaults to `true`.
* - `title`: _string_, the title heading for the popover dialog. Defaults to empty string, whereby the heading is not
* displayed.
* - `placement`: _string_, the placement of the help popover on hover or click of the icon or label. Defaults to
* `top`.
* - `container`: _string_, the specific element to which the popover will be appended to. Defaults to `table` when
* `iconBesideInput` is `true`, else defaults to `form`
* - `animation`: _boolean_, whether to add a CSS fade transition effect when opening and closing the popover. Defaults to
* `true`.
* - `delay``: _integer_|_array_, the number of milliseconds it will take to open and close the popover. Defaults to `0`.
* - `selector`: _integer_, the specified selector to add the popover to. Defaults to boolean `false`.
* - `viewport`: _string_|_array_, the element within which the popover will be bounded to. Defaults to
* `['selector' => 'body', 'padding' => 0]`.
*/
public $hintSettings = [];
/**
* @var array the feedback icon configuration (applicable for [bootstrap text inputs](http://getbootstrap.com/css/#with-optional-icons)).
* This must be setup as an array containing the following keys:
*
* - `type`: _string_, the icon type to use. Should be one of `raw` or `icon`. Defaults to `icon`, where the `default`,
* `error` and `success` settings will be treated as an icon CSS suffix name. If set to `raw`, they will be
* treated as a raw content markup.
* - `prefix`: _string_, the icon CSS class prefix to use if `type` is `icon`. Defaults to `glyphicon glyphicon-`.
* - `default`: _string_, the icon (CSS class suffix name or raw markup) to show by default. If not set will not be
* shown.
* - `error`: _string_, the icon (CSS class suffix name or raw markup) to use when input has an error validation. If
* not set will not be shown.
* - `success`: _string_, the icon (CSS class suffix name or raw markup) to use when input has a success validation. If
* not set will not be shown.
* - `defaultOptions`: _array_, the HTML attributes to apply for default icon. The special attribute `description` can
* be set to describe this feedback as an `aria` attribute for accessibility. Defaults to `(default)`.
* - `errorOptions`: _array_, the HTML attributes to apply for error icon. The special attribute `description` can be
* set to describe this feedback as an `aria` attribute for accessibility. Defaults to `(error)`.
* - `successOptions`: _array_, the HTML attributes to apply for success icon. The special attribute `description` can
* be set to describe this feedback as an `aria` attribute for accessibility. Defaults to `(success)`.
*
* @see http://getbootstrap.com/css/#with-optional-icons
*/
public $feedbackIcon = [];
/**
* @var string content to be placed before label
*/
public $contentBeforeLabel = '';
/**
* @var string content to be placed after label
*/
public $contentAfterLabel = '';
/**
* @var string content to be placed before input
*/
public $contentBeforeInput = '';
/**
* @var string content to be placed after input
*/
public $contentAfterInput = '';
/**
* @var string content to be placed before error block
*/
public $contentBeforeError = '';
/**
* @var string content to be placed after error block
*/
public $contentAfterError = '';
/**
* @var string content to be placed before hint block
*/
public $contentBeforeHint = '';
/**
* @var string content to be placed after hint block
*/
public $contentAfterHint = '';
/**
* @var array addon options for text and password inputs. The following settings can be configured:
* - `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
*/
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 string inherits and overrides values from parent class. The value can be overridden within
* [[ActiveForm::field()]] method. The following tokens are supported:
* - `{beginLabel}`: Container begin tag for labels (to be used typically along with `{labelTitle}` token
* when you do not wish to directly use the `{label}` token)
* - `{labelTitle}`: Label content without tags (to be used typically when you do not wish to directly use
* the `{label` token)
* - `{endLabel}`: Container end tag for labels (to be used typically along with `{labelTitle}` token
* when you do not wish to directly use the `{label}` token)
* - `{label}`: Full label tag with begin tag, content and end tag
* - `{beginWrapper}`: Container for input,error and hint start tag. Uses a `<div>` tag if there is a input wrapper
* CSS detected, else defaults to empty string.
* - `{input}`: placeholder for input control whatever it is
* - `{hint}`: placeholder for hint/help text including sub container
* - `{error}`: placeholder for error text including sub container
* - `{endWrapper}`: end tag for `{beginWrapper}`. Defaults to `</div>` if there is a input wrapper CSS detected,
* else defaults to empty string.
*/
public $template = "{label}\n{beginWrapper}\n{input}\n{hint}\n{error}\n{endWrapper}";
/**
*
* @var integer the bootstrap grid column width (usually between 1 to 12)
*/
public $labelSpan;
/**
*
* @var string one of the bootstrap sizes (refer the ActiveForm::SIZE constants)
*/
public $deviceSize;
/**
* @var boolean whether to render the error. Default is `true` except for layout `inline`.
*/
public $enableError;
/**
* @var boolean whether to render the label. Default is `true`.
*/
public $enableLabel;
/**
* @var null|array CSS grid classes for horizontal layout. This must be an array with these keys:
* - `offset`: the offset grid class to append to the wrapper if no label is rendered
* - `label`: the label grid class
* - `wrapper`: the wrapper grid class
* - `error`: the error grid class
* - `hint`: the hint grid class
* These options are compatible and similar to [[\yii\bootstrap\ActiveForm]] and provide a complete flexible
* container. If `labelSpan` is set in [[ActiveForm::formConfig]] and `wrapper` is also set, then both css options
* are concatenated. If `wrapper` contains a 'col-' class wrapper, it overrides the tag from `labelSpan`.
*/
public $horizontalCssClasses;
/**
* @var boolean whether the input is to be offset (like for checkbox or radio).
*/
protected $_offset = false;
/**
* @var boolean the container for multi select
*/
protected $_multiselect = '';
/**
* @var boolean is it a static input
*/
protected $_isStatic = false;
/**
* @var array the settings for the active field layout
*/
protected $_settings = [
'input' => '{input}',
'error' => '{error}',
'hint' => '{hint}',
'showLabels' => true,
'showErrors' => true,
'labelSpan' => ActiveForm::DEFAULT_LABEL_SPAN,
'deviceSize' => ActiveForm::SIZE_MEDIUM,
];
/**
* @var boolean whether there is a feedback icon configuration set
*/
protected $_hasFeedback = false;
/**
* @var boolean whether there is a feedback icon configuration set
*/
protected $_isHintSpecial = false;
/**
* @var string the label additional css class for horizontal forms and special inputs like checkbox and radio.
*/
private $_labelCss;
/**
* @var string the input container additional css class for horizontal forms and special inputs like checkbox and
* radio.
*/
private $_inputCss;
/**
* @var string the offset class for error and hint for horizontal forms or for special inputs like checkbox and
* radio.
*/
private $_offsetCss;
/**
* @var boolean whether the hint icon is beside the input.
*/
private $_iconBesideInput = false;
/**
* @var string the identifier for the hint popover container.
*/
private $_hintPopoverContainer;
/**
* Parses and returns addon content
*
* @param string|array $addon the addon parameter or the array of addon parameters
*
* @return string
*/
public static function getAddonContent($addon)
{
if (!is_array($addon)) {
return $addon;
}
if (!ArrayHelper::isIndexed($addon)) {
$addon = [$addon]; //pack existing array into indexed array
}
$html = "";
foreach ($addon as $addonItem) {
$content = ArrayHelper::getValue($addonItem, 'content', '');
if (empty($content)) {
continue;
}
$options = ArrayHelper::getValue($addonItem, 'options', []);
$suffix = ArrayHelper::getValue($addonItem, 'asButton', false) ? 'btn' : 'addon';
Html::addCssClass($options, 'input-group-' . $suffix);
$html .= Html::tag('span', $content, $options);
}
return $html;
}
/**
* @inheritdoc
*/
public function begin()
{
if ($this->_hasFeedback) {
Html::addCssClass($this->options, 'has-feedback');
}
return parent::begin();
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->initActiveField();
}
/**
* 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 checkbox. If not set, it will take
* the default value `0`. This method will render a hidden input so that if the checkbox 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'=>'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 checkbox($options = [], $enclosedByLabel = true)
{
return $this->getToggleField(self::TYPE_CHECKBOX, $options, $enclosedByLabel);
}
/**
* 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 be encoded.
* @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);
}
/**
* @inheritdoc
*/
public function dropDownList($items, $options = [])
{
$this->initDisability($options);
Html::addCssClass($options, $this->addClass);
return parent::dropDownList($items, $options);
}
/**
* @inheritdoc
*/
public function hint($content, $options = [])
{
if ($this->getConfigParam('showHints') === false) {
$this->parts['{hint}'] = '';
return $this;
}
if ($this->_isHintSpecial) {
Html::addCssClass($options, 'kv-hint-block');
}
return parent::hint($this->generateHint($content), $options);
}
/**
* @inheritdoc
*/
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);
}
/**
* @inheritdoc
*/
public function label($label = null, $options = [])
{
$hasLabels = $this->hasLabels();
$processLabels = $label !== false && $this->_isHintSpecial && $hasLabels !== false &&
$hasLabels !== ActiveForm::SCREEN_READER && ($this->getHintData('onLabelClick') || $this->getHintData(
'onLabelHover'
));
if ($processLabels) {
if ($label === null) {
$label = $this->model->getAttributeLabel($this->attribute);
}
$opts = ['class' => 'kv-type-label'];
Html::addCssClass($opts, $this->getHintIconCss('Label'));
$label = Html::tag('span', $label, $opts);
if ($this->getHintData('showIcon') && !$this->getHintData('iconBesideInput')) {
$label = strtr(
$this->getHintData('labelTemplate'),
['{label}' => $label, '{help}' => $this->getHintIcon()]
);
}
}
if (strpos($this->template, '{beginLabel}') !== false) {
$this->renderLabelParts($label, $options);
}
return parent::label($label, $options);
}
/**
* @inheritdoc
*/
public function listBox($items, $options = [])
{
$this->initDisability($options);
Html::addCssClass($options, $this->addClass);
return parent::listBox($items, $options);
}
/**
* @inheritdoc
*/
public function passwordInput($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::passwordInput($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);
}
/**
* 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);
}
/**
* @inheritdoc
*/
public function render($content = null)
{
if ($this->getConfigParam('showHints') === false) {
$this->hintOptions['hint'] = '';
} else {
if ($content === null && !isset($this->parts['{hint}']) && !isset($this->hintOptions['hint'])) {
$this->hintOptions['hint'] = $this->generateHint();
}
$this->template = strtr($this->template, ['{hint}' => $this->_settings['hint']]);
}
if ($this->form->staticOnly === true) {
$this->buildTemplate();
$this->staticInput();
} else {
$this->initPlaceholder($this->inputOptions);
$this->initDisability($this->inputOptions);
$this->buildTemplate();
}
return parent::render($content);
}
/**
* @inheritdoc
*/
public function textInput($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::textInput($options);
}
/**
* @inheritdoc
*/
public function textarea($options = [])
{
$this->initPlaceholder($options);
Html::addCssClass($options, $this->addClass);
$this->initDisability($options);
return parent::textarea($options);
}
/**
* @inheritdoc
*/
public function widget($class, $config = [])
{
if (property_exists($class, 'disabled') && property_exists($class, 'readonly')) {
$this->initDisability($config);
}
return parent::widget($class, $config);
}
/**
* 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;
}
/**
* Renders a multi select list box. This control extends the checkboxList and radioList available in
* [[YiiActiveField]] - 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 [[TYPE_RADIO]] or [[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 be encoded.
* @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);
}
/**
* Generates the hint icon
*
* @return string
*/
protected function getHintIcon()
{
if (!$this->getHintData('showIcon')) {
return '';
}
$options = [];
Html::addCssClass($options, $this->getHintIconCss('Icon'));
return Html::tag('span', $this->getHintData('icon'), $options);
}
/**
* 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.
* @param boolean $enclosedByLabel whether the input is enclosed by the label tag
*
* @return ActiveField object
*/
protected function getToggleField($type = self::TYPE_CHECKBOX, $options = [], $enclosedByLabel = true)
{
$this->initDisability($options);
$inputType = 'active' . ucfirst($type);
$disabled = ArrayHelper::getValue($options, 'disabled', false);
$css = $disabled ? $type . ' disabled' : $type;
$container = ArrayHelper::remove($options, 'container', ['class' => $css]);
if ($enclosedByLabel) {
$this->_offset = true;
$this->parts['{label}'] = '';
$showLabels = $this->hasLabels();
if ($showLabels === false) {
$options['label'] = '';
$this->showLabels = true;
}
} else {
$this->_offset = false;
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;
}
/**
* 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;
}
}
/**
* Gets configuration parameter from formConfig
*
* @param string $param the parameter name
* @param mixed $default the default parameter value
*
* @return bool the parsed parameter value
*/
protected function getConfigParam($param, $default = true)
{
return isset($this->$param) ? $this->$param : ArrayHelper::getValue($this->form->formConfig, $param, $default);
}
/**
* Generates the hint.
*
* @param string $content the hint content
*
* @return string
*/
protected function generateHint($content = null)
{
if ($content === null && method_exists($this->model, 'getAttributeHint')) {
$content = $this->model->getAttributeHint($this->attribute);
}
return $this->contentBeforeHint . $content . $this->contentAfterHint;
}
/**
* Initialize the active field
*/
protected function initActiveField()
{
if (isset($this->enableError)) {
$this->showErrors = $this->enableError;
}
if (isset($this->enableLabel)) {
$this->showLabels = $this->enableLabel;
}
$showLabels = $this->getConfigParam('showLabels');
$this->_isHintSpecial = $this->hintType === self::HINT_SPECIAL;
if ($this->form->type === ActiveForm::TYPE_INLINE && !isset($this->autoPlaceholder) && $showLabels !== true) {
$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 ($showLabels === ActiveForm::SCREEN_READER) {
Html::addCssClass($this->labelOptions, ActiveForm::SCREEN_READER);
}
if ($this->form->type === ActiveForm::TYPE_HORIZONTAL) {
$this->initHorizontal();
}
$this->initLabels();
$this->initHints();
$this->_hasFeedback = !empty($this->feedbackIcon) && is_array($this->feedbackIcon);
$this->_iconBesideInput = ArrayHelper::getValue($this->hintSettings, 'iconBesideInput') ? true : false;
if ($this->_iconBesideInput) {
$id = ArrayHelper::getValue($this->options, 'id', '');
$this->_hintPopoverContainer = $id ? "#{$id}-table" : '';
} else {
$id = ArrayHelper::getValue($this->form->options, 'id', '');
$this->_hintPopoverContainer = $id ? "#{$id}" : '';
}
}
/**
* Initialize label options
*/
protected function initLabels()
{
$labelCss = $this->_labelCss;
if ($this->hasLabels() === ActiveForm::SCREEN_READER) {
Html::addCssClass($this->labelOptions, ActiveForm::SCREEN_READER);
} elseif ($labelCss != self::NOT_SET) {
Html::addCssClass($this->labelOptions, $labelCss);
}
}
/**
* Validate label display status
*
* @return boolean|string whether labels are to be shown
*/
protected function hasLabels()
{
$showLabels = $this->getConfigParam('showLabels'); // plus abfrage $this-showLabels kombinieren.
if ($this->autoPlaceholder && $showLabels !== ActiveForm::SCREEN_READER) {
$showLabels = false;
}
return $showLabels;
}
/**
* Prepares bootstrap grid col classes for horizontal layout including label and input tags and initiate private
* CSS variables. The process order for 'labelSpan' and 'wrapper' is as follows:
*
* - Step 1: Check `$labelSpan` and `$deviceSize`.
* - Step 2: Check `formConfig(['labelSpan' => x, 'deviceSize' => xy]) and build css tag.
* - If `horizontalCssClasses['wrapper']` is set and no 'col-' tag then add this to css tag from Step 1.
* - If `horizontalCssClasses['wrapper']` is set and wrapper has 'col-' tag then override css tag completely.
* - If no `$labelSpan` and no `horizontalCssClasses['wrapper']` is set then use default from [[$_settings]].
* Similar behavior to `horizontalCssClasses['label']`.
*/
protected function initHorizontal()
{
$hor = $this->horizontalCssClasses;
$span = $this->getConfigParam('labelSpan', '');
$size = $this->getConfigParam('deviceSize', '');
// check horizontalCssClasses['wrapper'] if there is a col- class
if (isset($hor['wrapper']) && strpos($hor['wrapper'], 'col-') !== false) {
$span = '';
}
if (empty($span) && !isset($hor['wrapper'])) {
$span = $this->_settings['labelSpan'];
}
if (empty($size)) {
$size = ArrayHelper::getValue($this->_settings, 'deviceSize');
}
$this->deviceSize = $size;
if ($span != self::NOT_SET && intval($span) > 0) {
$span = intval($span);
// validate if invalid labelSpan is passed - else set to DEFAULT_LABEL_SPAN
if ($span <= 0 || $span >= $this->form->fullSpan) {
$span = $this->form->fullSpan;
}
// validate if invalid deviceSize is passed - else default to SIZE_MEDIUM
$sizes = [ActiveForm::SIZE_TINY, ActiveForm::SIZE_SMALL, ActiveForm::SIZE_MEDIUM, ActiveForm::SIZE_LARGE];
if ($size == self::NOT_SET || !in_array($size, $sizes)) {
$size = ActiveForm::SIZE_MEDIUM;
}
$this->labelSpan = $span;
$prefix = "col-{$size}-";
$this->_labelCss = $prefix . $span;
$this->_inputCss = $prefix . ($this->form->fullSpan - $span);
$this->_offsetCss = $prefix . "offset-" . $span;
}
if (isset($hor['wrapper'])) {
if ($span !== self::NOT_SET) {
$this->_inputCss .= " ";
}
$this->_inputCss .= $hor['wrapper'];
}
if (isset($hor['offset'])) {
$this->_offsetCss = $hor['offset'];
}
if (isset($hor['label'])) {
if ($span !== self::NOT_SET) {
$this->_labelCss .= " ";
}
$this->_labelCss .= $hor['label'];
}
if (isset($hor['error'])) {
Html::addCssClass($this->errorOptions, $hor['error']);
}
}
/**
* Initialize layout settings for label, input, error and hint blocks and for various bootstrap 3 form layouts
*/
protected function initLayout()
{
$showLabels = $this->hasLabels();
$showErrors = $this->getConfigParam('showErrors');
$this->mergeSettings($showLabels, $showErrors);
$this->buildLayoutParts($showLabels, $showErrors);
}
/**
* Merges the parameters for layout settings
*
* @param boolean $showLabels whether to show labels
* @param boolean $showErrors whether to show errors
*/
protected function mergeSettings($showLabels, $showErrors)
{
$this->_settings['showLabels'] = $showLabels;
$this->_settings['showErrors'] = $showErrors;
}
/**
* Builds the field layout parts
*
* @param boolean $showLabels whether to show labels
* @param boolean $showErrors whether to show errors
*/
protected function buildLayoutParts($showLabels, $showErrors)
{
if (!$showErrors) {
$this->_settings['error'] = '';
}
$this->parts['{beginWrapper}'] = '';
$this->parts['{endWrapper}'] = '';
if ($this->skipFormLayout) {
$this->mergeSettings($showLabels, $showErrors);
$this->parts['{beginLabel}'] = '';
$this->parts['{labelTitle}'] = '';
$this->parts['{endLabel}'] = '';
return;
}
if (!empty($this->_inputCss)) {
$offsetDivClass = $this->_offsetCss . " " . $this->_inputCss;
$inputDivClass = ($this->_offset) ? $offsetDivClass : $this->_inputCss;
if ($showLabels === false || $showLabels === ActiveForm::SCREEN_READER) {
$inputDivClass = "col-{$this->deviceSize}-{$this->form->fullSpan}";
}
$this->parts['{beginWrapper}'] = Html::beginTag('div', ['class' => $inputDivClass]);
$this->parts['{endWrapper}'] = Html::endTag('div');
}
$this->mergeSettings($showLabels, $showErrors);
}
/**
* Sets the layout element container
*
* @param string $type the layout element type
* @param string $css the css class for the container
* @param boolean $chk whether to create the container for the layout element
*/
protected function setLayoutContainer($type, $css = '', $chk = true)
{
if (!empty($css) && $chk) {
$this->_settings[$type] = "<div class='{$css}'>{{$type}}</div>";
}
}
/**
* Initialize hint settings
*/
protected function initHints()
{
if ($this->hintType !== self::HINT_SPECIAL) {
return;
}
$container = $this->_hintPopoverContainer;
if ($container === '') {
$container = $this->_iconBesideInput ? 'table' : 'form';
}
$defaultSettings = [
'showIcon' => true,
'iconBesideInput' => false,
'labelTemplate' => '{label}{help}',
'inputTemplate' => '<table style="width:100%"' . '{id}' . '><tr><td>{input}</td>' .
'<td style="width:5%">{help}</td></tr></table>',
'onLabelClick' => false,
'onLabelHover' => true,
'onIconClick' => true,
'onIconHover' => false,
'labelCssClass' => 'kv-hint-label',
'iconCssClass' => 'kv-hint-icon',
'contentCssClass' => 'kv-hint-content',
'icon' => '<i class="glyphicon glyphicon-question-sign text-info"></i>',
'hideOnEscape' => true,
'hideOnClickOut' => true,
'placement' => 'top',
'container' => $container,
'viewport' => ['selector' => 'body', 'padding' => 0],
];
$this->hintSettings = array_replace_recursive($defaultSettings, $this->hintSettings);
Html::addCssClass($this->options, 'kv-hint-special');
foreach (static::$_pluginHintKeys as $key) {
$this->setHintData($key);
}
}
/**
* Sets a hint property setting as a data attribute within `self::$options`
*
* @param string $key the hint property key
*/
protected function setHintData($key)
{
if (isset($this->hintSettings[$key])) {
$value = $this->hintSettings[$key];
$this->options['data-' . Inflector::camel2id($key)] = is_bool($value) ? (int)$value : $value;
}
}
/**
* 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;
}
}
/**
* Gets a hint configuration setting value
*
* @param string $key the hint setting key to fetch
* @param mixed $default the default value if not set
*
* @return mixed
*/
protected function getHintData($key, $default = null)
{
return ArrayHelper::getValue($this->hintSettings, $key, $default);
}
/**
* Gets the hint icon css based on `hintSettings`
*
* @param string $type whether `Label` or `Icon`
*
* @return array the css to be applied
*/
protected function getHintIconCss($type)
{
$css = ["kv-hintable"];
if ($type === 'Icon') {
$css[] = 'hide';
}
if (!empty($this->hintSettings["on{$type}Click"])) {
$css[] = "kv-hint-click";
}
if (!empty($this->hintSettings["on{$type}Hover"])) {
$css[] = "kv-hint-hover";
}
return $css;
}
/**
* Builds the final template based on the bootstrap form type, display settings for label, error, and hint, and
* content before and after label, input, error, and hint.
*/
protected function buildTemplate()
{
$showLabels = $showErrors = $input = $error = null;
extract($this->_settings);
if ($this->_isStatic && $this->showErrors !== true) {
$showErrors = false;
}
$showLabels = $showLabels && $this->hasLabels();
$this->buildLayoutParts($showLabels, $showErrors);
extract($this->_settings);
if (!empty($this->_multiselect)) {
$input = str_replace('{input}', $this->_multiselect, $input);
}
if ($this->_isHintSpecial && $this->getHintData('iconBesideInput') && $this->getHintData('showIcon')) {
$id = $this->_hintPopoverContainer ? ' id="' . $this->_hintPopoverContainer . '"' : '';
$help = strtr($this->getHintData('inputTemplate'), ['{help}' => $this->getHintIcon(), '{id}' => $id,]);
$input = str_replace('{input}', $help, $input);
}
$newInput = $this->contentBeforeInput . $this->generateAddon() . $this->renderFeedbackIcon() .
$this->contentAfterInput;
$newError = "{$this->contentBeforeError}{error}{$this->contentAfterError}";
$config = [
'{beginLabel}' => $showLabels ? '{beginLabel}' : "",
'{endLabel}' => $showLabels ? '{endLabel}' : "",
'{label}' => $showLabels ? "{$this->contentBeforeLabel}{label}{$this->contentAfterLabel}" : "",
'{labelTitle}' => $showLabels ? "{$this->contentBeforeLabel}{labelTitle}{$this->contentAfterLabel}" : "",
'{input}' => str_replace('{input}', $newInput, $input),
'{error}' => $showErrors ? str_replace('{error}', $newError, $error) : '',
];
$this->template = strtr($this->template, $config);
}
/**
* Generates the addon markup
*
* @return string
*/
protected function generateAddon()
{
if (empty($this->addon)) {
return '{input}';
}
$addon = $this->addon;
$prepend = static::getAddonContent(ArrayHelper::getValue($addon, 'prepend', ''));
$append = static::getAddonContent(ArrayHelper::getValue($addon, 'append', ''));
$content = $prepend . '{input}' . $append;
$group = ArrayHelper::getValue($addon, 'groupOptions', []);
Html::addCssClass($group, 'input-group');
$contentBefore = ArrayHelper::getValue($addon, 'contentBefore', '');
$contentAfter = ArrayHelper::getValue($addon, 'contentAfter', '');
$content = Html::tag('div', $contentBefore . $content . $contentAfter, $group);
return $content;
}
/**
* Render the bootstrap feedback icon
*
* @see http://getbootstrap.com/css/#with-optional-icons
*
* @return string
*/
protected function renderFeedbackIcon()
{
if (!$this->_hasFeedback) {
return '';
}
$config = $this->feedbackIcon;
$type = ArrayHelper::getValue($config, 'type', 'icon');
$prefix = ArrayHelper::getValue($config, 'prefix', 'glyphicon glyphicon-');
$id = Html::getInputId($this->model, $this->attribute);
return $this->getFeedbackIcon($config, 'default', $type, $prefix, $id) .
$this->getFeedbackIcon($config, 'success', $type, $prefix, $id) .
$this->getFeedbackIcon($config, 'error', $type, $prefix, $id);
}
/**
* Render the label parts
*
* @param string|null $label the label or null to use model label
* @param array $options the tag options
*/
protected function renderLabelParts($label = null, $options = [])
{
$options = array_merge($this->labelOptions, $options);
if ($label === null) {
if (isset($options['label'])) {
$label = $options['label'];
unset($options['label']);
} else {
$attribute = Html::getAttributeName($this->attribute);
$label = Html::encode($this->model->getAttributeLabel($attribute));
}
}
if (!isset($options['for'])) {
$options['for'] = Html::getInputId($this->model, $this->attribute);
}
$this->parts['{beginLabel}'] = Html::beginTag('label', $options);
$this->parts['{endLabel}'] = Html::endTag('label');
if (!isset($this->parts['{labelTitle}'])) {
$this->parts['{labelTitle}'] = $label;
}
}
/**
* Generates a feedback icon
*
* @param array $config the feedback icon configuration
* @param string $cat the feedback icon category
* @param string $type the feedback icon type
* @param string $prefix the feedback icon prefix
* @param string $id the input attribute identifier
*
* @return string
*/
protected function getFeedbackIcon($config, $cat, $type, $prefix, $id)
{
$markup = ArrayHelper::getValue($config, $cat, null);
if ($markup === null) {
return '';
}
$desc = ArrayHelper::remove($options, 'description', "({$cat})");
$options = ArrayHelper::getValue($config, $cat . 'Options', []);
$options['aria-hidden'] = true;
$key = $id . '-' . $cat;
$this->inputOptions['aria-describedby'] = empty($this->inputOptions['aria-describedby']) ? $key :
$this->inputOptions['aria-describedby'] . ' ' . $key;
Html::addCssClass($options, 'form-control-feedback');
Html::addCssClass($options, 'kv-feedback-' . $cat);
$icon = $type === 'raw' ? $markup : Html::tag('i', '', ['class' => $prefix . $markup]);
return Html::tag('span', $icon, $options) . Html::tag('span', $desc, ['id' => $key, 'class' => 'sr-only']);
}
/**
* 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 be encoded.
* @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 boolean $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";
$opts = ArrayHelper::getValue($options, 'itemOptions', []);
$this->initDisability($opts);
$css = $this->form->disabled ? ' disabled' : '';
$css .= $this->form->readonly ? ' readonly' : '';
if ($inline && !isset($options['itemOptions']['labelOptions']['class'])) {
$options['itemOptions']['labelOptions']['class'] = "{$type}-inline{$css}";
} elseif (!isset($options['item'])) {
$labelOptions = ArrayHelper::getValue($opts, 'labelOptions', []);
$options['item'] = function ($index, $label, $name, $checked, $value)
use ($type, $css, $disabled, $readonly, $asButtonGroup, $labelOptions, $opts) {
$opts += [
'data-index' => $index,
'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);
}
}