diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 4b3522ec..7bad9b8b 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -174,9 +174,9 @@ class FormHelper extends CoreFormHelper 'checkboxFormGroup' => '{{input}}{{label}}', 'checkboxWrapper' => - '
{{content}}
', 'inputGroupContainer' => @@ -453,7 +453,6 @@ public function control(string $fieldName, array $options = []): string 'prepend' => null, 'append' => null, 'inline' => null, - 'nestedInput' => false, 'switch' => null, 'type' => null, 'label' => null, @@ -469,6 +468,7 @@ public function control(string $fieldName, array $options = []): string 'spacing' => null, ]; $options = $this->parseOptions($fieldName, $options); + $options['nestedInput'] ??= $this->getConfig('nestedCheckboxAndRadio'); $newTemplates = $options['templates']; if ($newTemplates) { @@ -508,6 +508,7 @@ public function control(string $fieldName, array $options = []): string $options['injectErrorClass'] = $this->getConfig('templates.errorClass'); } + $nestedInput = $options['nestedInput']; unset( $options['formGroupPosition'], $options['feedbackStyle'], @@ -517,7 +518,16 @@ public function control(string $fieldName, array $options = []): string $options['switch'], ); - $result = parent::control($fieldName, $options); + // Core `control()` only honors the `nestedInput` option for checkboxes. + // Radio and multi-checkbox inputs read it from the `nestedCheckboxAndRadio` + // config, so set it for the duration of the call. + $nestedCheckboxAndRadio = $this->getConfig('nestedCheckboxAndRadio'); + $this->setConfig('nestedCheckboxAndRadio', $nestedInput); + try { + $result = parent::control($fieldName, $options); + } finally { + $this->setConfig('nestedCheckboxAndRadio', $nestedCheckboxAndRadio); + } $result = $this->postProcessElement($result, $fieldName, $options); } finally { diff --git a/tests/TestCase/View/Helper/FormHelper/DefaultAlign/MultipleCheckboxControlTest.php b/tests/TestCase/View/Helper/FormHelper/DefaultAlign/MultipleCheckboxControlTest.php index daa602c1..fbd5c6ea 100644 --- a/tests/TestCase/View/Helper/FormHelper/DefaultAlign/MultipleCheckboxControlTest.php +++ b/tests/TestCase/View/Helper/FormHelper/DefaultAlign/MultipleCheckboxControlTest.php @@ -2262,9 +2262,8 @@ public function testDefaultAlignMultipleCheckboxControlOptionsGroupsAndSingleEnt 'class' => 'custominputclass', ], 30 => [ - 'text' => 'option 5 without label', + 'text' => 'option 5', 'value' => 30, - 'label' => false, ], 40 => [ 'text' => 'option 6', @@ -2359,6 +2358,9 @@ public function testDefaultAlignMultipleCheckboxControlOptionsGroupsAndSingleEnt 'id' => 'users-30', 'value' => 30, ]], + ['label' => ['class' => 'form-check-label', 'for' => 'users-30']], + 'option 5', + '/label', '/div', ['div' => ['class' => 'form-check']], ['input' => [ @@ -2488,9 +2490,8 @@ public function testDefaultAlignMultipleCheckboxControlOptionsGroupsAndSingleEnt 'class' => 'custominputclass', ], 30 => [ - 'text' => 'option 5 without label', + 'text' => 'option 5', 'value' => 30, - 'label' => false, ], 40 => [ 'text' => 'option 6', @@ -2579,13 +2580,16 @@ public function testDefaultAlignMultipleCheckboxControlOptionsGroupsAndSingleEnt '/label', '/div', ['div' => ['class' => 'form-check']], - ['input' => [ - 'class' => 'form-check-input', - 'type' => 'checkbox', - 'name' => 'users[]', - 'id' => 'users-30', - 'value' => 30, - ]], + ['label' => ['class' => 'form-check-label', 'for' => 'users-30']], + ['input' => [ + 'class' => 'form-check-input', + 'type' => 'checkbox', + 'name' => 'users[]', + 'id' => 'users-30', + 'value' => 30, + ]], + 'option 5', + '/label', '/div', ['div' => ['class' => 'form-check']], ['label' => ['class' => 'customlabelclass', 'for' => 'users-40']], diff --git a/tests/TestCase/View/Helper/FormHelper/HorizontalAlign/MultipleCheckboxControlTest.php b/tests/TestCase/View/Helper/FormHelper/HorizontalAlign/MultipleCheckboxControlTest.php index 6590dcc3..99ce784f 100644 --- a/tests/TestCase/View/Helper/FormHelper/HorizontalAlign/MultipleCheckboxControlTest.php +++ b/tests/TestCase/View/Helper/FormHelper/HorizontalAlign/MultipleCheckboxControlTest.php @@ -2517,9 +2517,8 @@ public function testHorizontalAlignMultipleCheckboxControlOptionsGroupsAndSingle 'class' => 'custominputclass', ], 30 => [ - 'text' => 'option 5 without label', + 'text' => 'option 5', 'value' => 30, - 'label' => false, ], 40 => [ 'text' => 'option 6', @@ -2615,6 +2614,9 @@ public function testHorizontalAlignMultipleCheckboxControlOptionsGroupsAndSingle 'id' => 'users-30', 'value' => 30, ]], + ['label' => ['class' => 'form-check-label', 'for' => 'users-30']], + 'option 5', + '/label', '/div', ['div' => ['class' => 'form-check']], ['input' => [ @@ -2761,9 +2763,8 @@ public function testHorizontalAlignMultipleCheckboxControlOptionsGroupsAndSingle 'class' => 'custominputclass', ], 30 => [ - 'text' => 'option 5 without label', + 'text' => 'option 5', 'value' => 30, - 'label' => false, ], 40 => [ 'text' => 'option 6', @@ -2853,13 +2854,16 @@ public function testHorizontalAlignMultipleCheckboxControlOptionsGroupsAndSingle '/label', '/div', ['div' => ['class' => 'form-check']], - ['input' => [ - 'class' => 'form-check-input', - 'type' => 'checkbox', - 'name' => 'users[]', - 'id' => 'users-30', - 'value' => 30, - ]], + ['label' => ['class' => 'form-check-label', 'for' => 'users-30']], + ['input' => [ + 'class' => 'form-check-input', + 'type' => 'checkbox', + 'name' => 'users[]', + 'id' => 'users-30', + 'value' => 30, + ]], + 'option 5', + '/label', '/div', ['div' => ['class' => 'form-check']], ['label' => ['class' => 'customlabelclass', 'for' => 'users-40']],