/
FormHelper.php
1573 lines (1372 loc) · 52.4 KB
/
FormHelper.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
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
<?php
declare(strict_types=1);
namespace BootstrapUI\View\Helper;
use Cake\Core\Configure\Engine\PhpConfig;
use Cake\Utility\Hash;
use Cake\Utility\Inflector;
use Cake\View\Helper\FormHelper as CoreFormHelper;
use Cake\View\View;
use InvalidArgumentException;
use function Cake\Core\h;
use function Cake\I18n\__;
class FormHelper extends CoreFormHelper
{
use OptionsAwareTrait;
/**
* The default feedback style.
*
* @var string
*/
public const FEEDBACK_STYLE_DEFAULT = 'default';
/**
* The tooltip feedback style.
*
* @var string
*/
public const FEEDBACK_STYLE_TOOLTIP = 'tooltip';
/**
* Absolute positioning.
*
* @var string
*/
public const POSITION_ABSOLUTE = 'absolute';
/**
* Fixed positioning.
*
* @var string
*/
public const POSITION_FIXED = 'fixed';
/**
* Relative positioning.
*
* @var string
*/
public const POSITION_RELATIVE = 'relative';
/**
* Static positioning.
*
* @var string
*/
public const POSITION_STATIC = 'static';
/**
* Sticky positioning.
*
* @var string
*/
public const POSITION_STICKY = 'sticky';
/**
* Form alignment types.
*
* @var array
*/
public const ALIGN_TYPES = ['default', 'horizontal', 'inline'];
/**
* Default alignment.
*
* @var string
*/
public const ALIGN_DEFAULT = 'default';
/**
* Horizontal alignment.
*
* @var string
*/
public const ALIGN_HORIZONTAL = 'horizontal';
/**
* Inlline alignment.
*
* @var string
*/
public const ALIGN_INLINE = 'inline';
/**
* First grid column.
*
* @var int
*/
public const GRID_COLUMN_ONE = 0;
/**
* Second grid column.
*
* @var int
*/
public const GRID_COLUMN_TWO = 1;
/**
* Set on `Form::create()` to tell if the type of alignment used (i.e. horizontal).
*
* @var string|null
*/
protected ?string $_align = null;
/**
* Set on `Form::create()` to tell grid type.
*
* @var array|null
*/
protected ?array $_grid = null;
/**
* Set on `Form::create()` to tell the spacing type.
*
* @var string|false|null
*/
protected string|false|null $_spacing = null;
/**
* Default Bootstrap string templates.
*
* @var array
*/
protected array $_templates = [
'error' =>
'<div id="{{id}}" class="ms-0 invalid-feedback">{{content}}</div>',
'errorTooltip' =>
'<div id="{{id}}" class="invalid-tooltip">{{content}}</div>',
'label' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'help' =>
'<small{{attrs}}>{{content}}</small>',
'tooltip' =>
'<span data-bs-toggle="tooltip" title="{{content}}" class="bi bi-info-circle-fill"></span>',
'formGroupFloatingLabel' =>
'{{input}}{{label}}',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{type}}{{required}}">{{content}}{{help}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}{{error}}{{help}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'inputContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{type}}{{required}}">{{content}}{{help}}</div>',
'inputContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}{{error}}{{help}}</div>',
'checkboxContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group form-check{{variant}} ' .
'{{type}}{{required}}">{{content}}{{help}}</div>',
'checkboxContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group form-check{{variant}} ' .
'{{formGroupPosition}}{{type}}{{required}} is-invalid">{{content}}{{error}}{{help}}</div>',
'checkboxInlineContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-check{{variant}} form-check-inline align-top {{type}}{{required}}">' .
'{{content}}{{help}}</div>',
'checkboxInlineContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-check{{variant}} form-check-inline align-top ' .
'{{formGroupPosition}}{{type}}{{required}} is-invalid">{{content}}{{error}}{{help}}</div>',
'checkboxFormGroup' =>
'{{input}}{{label}}',
'checkboxWrapper' =>
'<div class="form-check{{variant}}">{{label}}</div>',
'checkboxInlineWrapper' =>
'<div class="form-check{{variant}} form-check-inline">{{label}}</div>',
'radioContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group {{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}{{help}}</div>',
'radioContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}{{error}}{{help}}</div>',
'radioLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'radioWrapper' =>
'<div class="form-check">{{hidden}}{{label}}</div>',
'radioInlineWrapper' =>
'<div class="form-check form-check-inline">{{label}}</div>',
'staticControl' =>
'<p class="form-control-plaintext">{{content}}</p>',
'inputGroupContainer' =>
'<div{{attrs}}>{{prepend}}{{content}}{{append}}</div>',
'inputGroupText' =>
'<span class="input-group-text">{{content}}</span>',
'multicheckboxContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group {{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}{{help}}</div>',
'multicheckboxContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}{{error}}{{help}}</div>',
'multicheckboxLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'multicheckboxWrapper' =>
'<fieldset class="%s form-group">{{content}}</fieldset>',
'multicheckboxTitle' =>
'<legend class="col-form-label pt-0">{{text}}</legend>',
'nestingLabel' =>
'{{hidden}}{{input}}<label{{attrs}}>{{text}}{{tooltip}}</label>',
'nestingLabelNestedInput' =>
'{{hidden}}<label{{attrs}}>{{input}}{{text}}{{tooltip}}</label>',
'submitContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}submit">{{content}}</div>',
];
/**
* Templates set per alignment type
*
* @var array
*/
protected array $_templateSet = [
'default' => [
],
'inline' => [
'elementWrapper' =>
'<div class="col-auto">{{content}}</div>',
'checkboxInlineContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-check{{variant}} {{type}}{{required}}">' .
'{{content}}{{help}}</div>',
'checkboxInlineContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-check{{variant}} ' .
'{{formGroupPosition}}{{type}}{{required}} is-invalid">{{content}}{{error}}{{help}}</div>',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}}">' .
'{{content}}{{help}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}{{error}}{{help}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'radioContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}{{help}}</div>',
'radioContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group {{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}{{error}}{{help}}</div>',
'radioLabel' =>
'<span{{attrs}}>{{text}}{{tooltip}}</span>',
'multicheckboxContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group d-flex {{formGroupPosition}}{{type}}{{required}}" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}{{help}}</div>',
'multicheckboxContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group d-flex ' .
'{{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}{{error}}{{help}}</div>',
'multicheckboxLabel' =>
'<span{{attrs}}>{{text}}{{tooltip}}</span>',
'multicheckboxWrapper' =>
'<fieldset class="form-group">{{content}}</fieldset>',
'multicheckboxTitle' =>
'<legend class="col-form-label float-none pt-0">{{text}}</legend>',
],
'horizontal' => [
'label' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'formGroup' =>
'{{label}}<div class="%s">{{input}}{{error}}{{help}}</div>',
'formGroupFloatingLabel' =>
'<div class="%s form-floating">{{input}}{{label}}{{error}}{{help}}</div>',
'checkboxFormGroup' =>
'<div class="%s"><div class="form-check{{variant}}">{{input}}{{label}}{{error}}{{help}}</div></div>',
'datetimeContainer' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{type}}{{required}}">{{content}}</div>',
'datetimeContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}</div>',
'datetimeLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'checkboxInlineFormGroup' =>
'<div class="%s"><div class="form-check{{variant}} form-check-inline">{{input}}{{label}}</div></div>',
'submitContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group row">' .
'<div class="%s">{{content}}</div></div>',
'inputContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group row {{type}}{{required}}">' .
'{{content}}</div>',
'inputContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}</div>',
'checkboxContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group row {{type}}{{required}}">' .
'{{content}}</div>',
'checkboxContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid">' .
'{{content}}</div>',
'radioContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group row {{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}</div>',
'radioContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'radioLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
'multicheckboxContainer' =>
'<div{{containerAttrs}} class="{{containerClass}}form-group row {{type}}{{required}}" role="group" ' .
'aria-labelledby="{{groupId}}">{{content}}</div>',
'multicheckboxContainerError' =>
'<div{{containerAttrs}} ' .
'class="{{containerClass}}form-group row {{formGroupPosition}}{{type}}{{required}} is-invalid" ' .
'role="group" aria-labelledby="{{groupId}}">{{content}}</div>',
'multicheckboxLabel' =>
'<label{{attrs}}>{{text}}{{tooltip}}</label>',
],
];
/**
* Default Bootstrap widgets.
*
* @var array
*/
protected array $_widgets = [
'button' => 'BootstrapUI\View\Widget\ButtonWidget',
'datetime' => 'BootstrapUI\View\Widget\DateTimeWidget',
'file' => ['BootstrapUI\View\Widget\FileWidget', 'label'],
'select' => 'BootstrapUI\View\Widget\SelectBoxWidget',
'textarea' => 'BootstrapUI\View\Widget\TextareaWidget',
'_default' => 'BootstrapUI\View\Widget\BasicWidget',
];
/**
* The name of the field for which the current error is being generated.
*
* @var string|null
*/
private ?string $_errorFieldName = null;
/**
* {@inheritDoc}
*
* Additionally to the core form helper options, the following BootstrapUI related options are supported:
*
* - `align` - The default alignment to use for all forms.
* - `grid` - The default grid setup to use for all horizontal forms.
* - `spacing` - The spacing to use for all forms. Can be either a string to define a class that will be
* used for all form alignments, or an array of strings, keyed by the alignment type to define individual
* classes to use per alignment. Set to `false` to disable automatic spacing class usage.
* - `templateSet` - An array of template sets, keyed by the alignment type.
*/
public function __construct(View $View, array $config = [])
{
$this->_defaultConfig = [
'align' => 'default',
'errorClass' => 'is-invalid',
'grid' => [
static::GRID_COLUMN_ONE => 2,
static::GRID_COLUMN_TWO => 10,
],
'spacing' => null,
'templates' => $this->_templates + $this->_defaultConfig['templates'],
] + $this->_defaultConfig;
if (isset($this->_defaultConfig['templateSet'])) {
$templateSet = Hash::merge($this->_templateSet, $this->_defaultConfig['templateSet']);
} else {
$templateSet = $this->_templateSet;
}
$this->_defaultConfig['templateSet'] = $templateSet;
$this->_defaultWidgets = $this->_widgets + $this->_defaultWidgets;
parent::__construct($View, $config);
}
/**
* {@inheritDoc}
*
* Additionally to the core form helper create options, the following BootstrapUI related options are supported:
*
* - `spacing` - The spacing to use for the form. Can be either a string to define a class, or boolean `false` to
* disable automatic spacing class usage.
*/
public function create(mixed $context = null, array $options = []): string
{
$options += [
'class' => null,
'align' => null,
'templates' => [],
'spacing' => null,
];
return parent::create($context, $this->_processFormOptions($options));
}
/**
* @inheritDoc
*/
public function error(string $field, array|string|null $text = null, array $options = []): string
{
$this->_errorFieldName = $field;
$error = parent::error($field, $text, $options);
$this->_errorFieldName = null;
return $error;
}
/**
* @inheritDoc
*/
public function formatTemplate(string $name, array $data): string
{
// Injects the `id` attribute value for the error template.
// This is done for backwards compatibility reasons, as the
// core form helper only introduced this behavior with
// CakePHP 4.3. This can be removed once the required minimum
// CakePHP version is bumped accordingly.
if (
$name === 'error' &&
!isset($data['id']) &&
$this->_errorFieldName !== null
) {
$data['id'] = $this->_domId($this->_errorFieldName . '-error');
}
return parent::formatTemplate($name, $data);
}
/**
* @inheritDoc
*/
public function label(string $fieldName, ?string $text = null, array $options = []): string
{
unset($options['floating']);
return parent::label($fieldName, $text, $options);
}
/**
* @inheritDoc
*/
public function button(string $title, array $options = []): string
{
$result = parent::button($title, $options);
return $this->_postProcessElement($result, null, $options);
}
/**
* @inheritDoc
*/
public function submit(?string $caption = null, array $options = []): string
{
$options += [
'class' => 'primary',
];
$options = $this->applyButtonClasses($options);
$options = $this->_containerOptions(null, $options);
$result = parent::submit($caption, $options);
return $this->_postProcessElement($result, null, $options);
}
/**
* @inheritDoc
*/
public function select(string $fieldName, iterable $options = [], array $attributes = []): string
{
$attributes['injectFormControl'] = false;
$attributes = $this->injectClasses('form-select', $attributes);
return parent::select($fieldName, $options, $attributes);
}
/**
* {@inheritDoc}
*
* Additionally to the core form helper options, the following BootstrapUI related options are supported:
*
* - `container` - An array of container attributes, with `class` being a special case, prepending the value to
* the existing list of classes instead of replacing them.
* - `append` - Append addon to input.
* - `prepend` - Prepend addon to input.
* - `inline` - Boolean for generating inline checkbox/radio.
* - `switch` - Boolean for generating switch style checkboxes.
* - `help` - Help text to include in the input container.
* - `tooltip` - Tooltip text to include in the control's label.
* - `feedbackStyle` - The feedback style to use, `default`, or `tooltip` (will cause `formGroupPosition` to be set
* to `relative` unless explicitly configured otherwise).
* - `formGroupPosition` - CSS positioning of form groups, `absolute`, `fixed`, `relative`, `static`, or `sticky`.
* - `spacing` - The spacing to use for the control. Can be either a string to define a class, or boolean `false`
* to disable automatic spacing class usage.
*/
public function control(string $fieldName, array $options = []): string
{
$options += [
'feedbackStyle' => null,
'formGroupPosition' => null,
'prepend' => null,
'append' => null,
'inline' => null,
'nestedInput' => false,
'switch' => null,
'type' => null,
'label' => null,
'error' => null,
'required' => null,
'options' => null,
'help' => null,
'tooltip' => null,
'templates' => [],
'templateVars' => [],
'labelOptions' => true,
'container' => null,
'spacing' => null,
];
$options = $this->_parseOptions($fieldName, $options);
$newTemplates = $options['templates'];
if ($newTemplates) {
$this->templater()->push();
$templateMethod = is_string($options['templates']) ? 'load' : 'add';
$this->templater()->{$templateMethod}($options['templates']);
$options['templates'] = [];
}
switch ($options['type']) {
case 'datetime-local':
case 'datetime':
case 'date':
case 'time':
$options = $this->_dateTimeOptions($fieldName, $options);
break;
case 'checkbox':
case 'radio':
case 'select':
case 'range':
$function = '_' . $options['type'] . 'Options';
$options = $this->{$function}($fieldName, $options);
break;
default:
$options = $this->_labelOptions($fieldName, $options);
break;
}
$options = $this->_spacingOptions($fieldName, $options);
$options = $this->_containerOptions($fieldName, $options);
$options = $this->_feedbackStyleOptions($fieldName, $options);
$options = $this->_ariaOptions($fieldName, $options);
$options = $this->_placeholderOptions($fieldName, $options);
$options = $this->_helpOptions($fieldName, $options);
$options = $this->_tooltipOptions($fieldName, $options);
if (
isset($options['append']) ||
isset($options['prepend'])
) {
$options['injectErrorClass'] = $this->_config['errorClass'];
}
unset(
$options['formGroupPosition'],
$options['feedbackStyle'],
$options['spacing'],
$options['inline'],
$options['nestedInput'],
$options['switch']
);
$result = parent::control($fieldName, $options);
$result = $this->_postProcessElement($result, $fieldName, $options);
if ($newTemplates) {
$this->templater()->pop();
}
return $result;
}
/**
* Modify options and templates based on spacing.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _spacingOptions(string $fieldName, array $options): array
{
if (!isset($options['spacing'])) {
$options['spacing'] = $this->_spacing;
}
if ($options['spacing'] === false) {
return $options;
}
if ($this->_align !== static::ALIGN_INLINE) {
$options['templates'] += [
'multicheckboxWrapper' => sprintf(
$this->templater()->getConfig('multicheckboxWrapper'),
$options['spacing']
),
];
}
return $options;
}
/**
* Modify the options for container templates.
*
* @param string|null $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _containerOptions(?string $fieldName, array $options): array
{
if (
$this->_align !== static::ALIGN_INLINE &&
isset($options['type']) &&
isset($options['spacing']) &&
$options['spacing'] !== false
) {
$options['container'] = $this->injectClasses($options['spacing'], (array)($options['container'] ?? []));
}
if (
$this->_align !== static::ALIGN_HORIZONTAL &&
isset($options['label']['floating']) &&
$options['label']['floating']
) {
$options['container'] = $this->injectClasses('form-floating', (array)($options['container'] ?? []));
}
if (!isset($options['container'])) {
return $options;
}
$containerOptions = $options['container'];
unset($options['container']);
if (isset($containerOptions['class'])) {
$options['templateVars']['containerClass'] = $containerOptions['class'] . ' ';
unset($containerOptions['class']);
}
if (!empty($containerOptions)) {
$options['templateVars']['containerAttrs'] = $this->templater()->formatAttributes($containerOptions);
}
return $options;
}
/**
* Modify options for date time controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _dateTimeOptions(string $fieldName, array $options): array
{
$options = $this->_labelOptions($fieldName, $options);
// group IDs are no longer required for date/time controls,
// this is just kept for backwards compatibility
$groupId =
$options['templateVars']['groupId'] =
$this->_domId($fieldName . '-group-label');
if ($options['label'] !== false) {
$options['label']['templateVars']['groupId'] = $groupId;
}
$options['templates']['label'] = $this->templater()->get('datetimeLabel');
$options['templates']['inputContainer'] = $this->templater()->get('datetimeContainer');
$options['templates']['inputContainerError'] = $this->templater()->get('datetimeContainerError');
return $options;
}
/**
* Modify options for checkbox controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _checkboxOptions(string $fieldName, array $options): array
{
if ($options['label'] !== false) {
$options['label'] = $this->injectClasses('form-check-label', (array)$options['label']);
}
if ($this->_align === static::ALIGN_HORIZONTAL) {
$options['inline'] = false;
}
if (
$options['inline'] ||
$this->_align === static::ALIGN_INLINE
) {
$checkboxContainer = $this->templater()->get('checkboxInlineContainer');
$checkboxContainerError = $this->templater()->get('checkboxInlineContainerError');
$options['templates']['checkboxContainer'] = $checkboxContainer;
$options['templates']['checkboxContainerError'] = $checkboxContainerError;
}
if ($options['nestedInput']) {
$options['templates']['nestingLabel'] = $this->templater()->get('nestingLabelNestedInput');
}
if ($options['switch']) {
$options['templateVars']['variant'] = ' form-switch';
}
return $options;
}
/**
* Modify options for radio controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _radioOptions(string $fieldName, array $options): array
{
$options = $this->_labelOptions($fieldName, $options);
$options = $this->injectClasses('form-check-input', $options);
$groupId =
$options['templateVars']['groupId'] =
$this->_domId($fieldName . '-group-label');
if ($options['label'] !== false) {
$options['label']['templateVars']['groupId'] = $groupId;
$options['label']['id'] = $groupId;
}
if ($options['label'] !== false) {
$labelClasses = [];
if ($this->_align !== static::ALIGN_INLINE) {
$labelClasses[] = 'd-block';
}
if ($this->_align === static::ALIGN_HORIZONTAL) {
$labelClasses[] = 'pt-0';
}
if ($labelClasses) {
$options['label'] = $this->injectClasses($labelClasses, (array)$options['label']);
}
}
$options['templates']['label'] = $this->templater()->get('radioLabel');
if (
$options['inline'] ||
$this->_align === static::ALIGN_INLINE
) {
$options['templates']['radioWrapper'] = $this->templater()->get('radioInlineWrapper');
}
if ($options['nestedInput']) {
$options['templates']['nestingLabel'] = $this->templater()->get('nestingLabelNestedInput');
}
return $options;
}
/**
* Modify options for select controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _selectOptions(string $fieldName, array $options): array
{
$options = $this->_labelOptions($fieldName, $options);
$labelClasses = [];
if (isset($options['multiple']) && $options['multiple'] === 'checkbox') {
$options['type'] = 'multicheckbox';
$groupId =
$options['templateVars']['groupId'] =
$this->_domId($fieldName . '-group-label');
if ($options['label'] !== false) {
$options['label']['templateVars']['groupId'] = $groupId;
$options['label']['id'] = $groupId;
}
if ($options['label'] !== false) {
if ($this->_align !== static::ALIGN_INLINE) {
$labelClasses[] = 'd-block';
}
if ($this->_align === static::ALIGN_HORIZONTAL) {
$labelClasses[] = 'pt-0';
}
}
$options['templates']['label'] = $this->templater()->get('multicheckboxLabel');
$options = $this->injectClasses('form-check-input', $options);
if (
$options['inline'] ||
$this->_align === static::ALIGN_INLINE
) {
$wrapper = $this->templater()->get('checkboxInlineWrapper');
$options['templates']['checkboxWrapper'] = $wrapper;
}
if ($options['nestedInput']) {
$options['templates']['nestingLabel'] = $this->templater()->get('nestingLabelNestedInput');
}
if ($options['switch']) {
$options['templateVars']['variant'] = ' form-switch';
}
}
if (
$this->_align === static::ALIGN_INLINE &&
$options['label'] !== false &&
!$options['label']['floating']
) {
$labelClasses[] = 'visually-hidden';
}
if ($labelClasses) {
$options['label'] = $this->injectClasses($labelClasses, (array)$options['label']);
}
return $options;
}
/**
* Modify options for range controls.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _rangeOptions(string $fieldName, array $options): array
{
$options = $this->_labelOptions($fieldName, $options);
$options['injectFormControl'] = false;
if (
$options['label'] !== false &&
$this->_align === static::ALIGN_HORIZONTAL
) {
$options['label'] = $this->injectClasses('pt-0', (array)$options['label']);
}
return $this->injectClasses('form-range', $options);
}
/**
* Modify the options for labels.
*
* @param string|null $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _labelOptions(?string $fieldName, array $options): array
{
if ($options['label'] !== false) {
$options['label'] = (array)$options['label'] + [
'floating' => false,
];
$labelClasses = [];
if ($options['label']['floating']) {
$options['templates']['formGroup'] = $this->templater()->get('formGroupFloatingLabel');
}
if (
$this->_align !== static::ALIGN_HORIZONTAL &&
!$options['label']['floating']
) {
$labelClasses[] = 'form-label';
}
if ($this->_align === static::ALIGN_HORIZONTAL) {
if (!$options['label']['floating']) {
$size = $this->_gridClass(static::GRID_COLUMN_ONE);
$labelClasses[] = "col-form-label $size";
} else {
$labelClasses[] = 'ps-4';
}
}
if (
$this->_align === static::ALIGN_INLINE &&
!$options['label']['floating']
) {
$labelClasses[] = 'visually-hidden';
}
if ($labelClasses) {
$options['label'] = $this->injectClasses($labelClasses, (array)$options['label']);
}
}
return $options;
}
/**
* Modify templates based on error style.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _feedbackStyleOptions(string $fieldName, array $options): array
{
$formGroupPosition = $options['formGroupPosition'] ?: $this->getConfig('formGroupPosition');
$feedbackStyle = $options['feedbackStyle'] ?: $this->getConfig('feedbackStyle');
if (
$this->_align === static::ALIGN_INLINE &&
$feedbackStyle === null
) {
$feedbackStyle = static::FEEDBACK_STYLE_TOOLTIP;
}
if ($feedbackStyle === static::FEEDBACK_STYLE_TOOLTIP) {
$options['templates']['error'] = $this->templater()->get('errorTooltip');
}
if (
$formGroupPosition === null &&
$feedbackStyle === static::FEEDBACK_STYLE_TOOLTIP
) {
$formGroupPosition = static::POSITION_RELATIVE;
}
if ($formGroupPosition !== null) {
$options['templateVars']['formGroupPosition'] = 'position-' . $formGroupPosition . ' ';
}
return $options;
}
/**
* Modify options for aria attributes.
*
* @param string $fieldName Field name.
* @param array $options Options. See `$options` argument of `control()` method.
* @return array
*/
protected function _ariaOptions(string $fieldName, array $options): array
{
if (
$options['type'] === 'hidden' ||
(
isset($options['aria-required']) &&
isset($options['aria-describedby']) &&
isset($options['aria-invalid'])
)
) {
return $options;
}
$isError =
$options['error'] !== false &&
$this->isFieldError($fieldName);