diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 12ba9a22..c1d43f96 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -483,6 +483,17 @@ public function submit(?string $caption = null, array $options = []): string 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} * @@ -704,7 +715,6 @@ protected function _checkboxOptions(string $fieldName, array $options): array if ($options['label'] !== false) { $options['label'] = $this->injectClasses('form-check-label', (array)$options['label']); } - $options = $this->injectClasses('form-check-input', $options); if ($this->_align === static::ALIGN_HORIZONTAL) { $options['inline'] = false; @@ -850,11 +860,6 @@ protected function _selectOptions(string $fieldName, array $options): array $options['label'] = $this->injectClasses($labelClasses, (array)$options['label']); } - if ($options['type'] !== 'multicheckbox') { - $options['injectFormControl'] = false; - $options = $this->injectClasses('form-select', $options); - } - return $options; } @@ -1166,6 +1171,16 @@ protected function _postProcessElement(string $html, ?string $fieldName, array $ return $html; } + /** + * @inheritDoc + */ + public function checkbox(string $fieldName, array $options = []) + { + $options = $this->injectClasses('form-check-input', $options); + + return parent::checkbox($fieldName, $options); + } + /** * @inheritDoc */ diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 143a40b5..763cc075 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -851,11 +851,11 @@ public function testTooltipWithDisabledLabel() } /** - * Test that "form-control" class is added when using methods for specific input. + * Test that "form-*" classes are added when using methods for specific input. * * @return void */ - public function testFormControlClassInjection() + public function testFormClassInjection() { $result = $this->Form->text('foo'); $this->assertStringContainsString('class="form-control"', $result); @@ -864,7 +864,8 @@ public function testFormControlClassInjection() $this->assertStringContainsString('class="custom form-control"', $result); $result = $this->Form->select('foo'); - $this->assertStringContainsString('class="form-control"', $result); + $this->assertStringNotContainsString('"form-control"', $result); + $this->assertStringContainsString('class="form-select"', $result); $result = $this->Form->textarea('foo'); $this->assertStringContainsString('class="form-control"', $result); @@ -877,9 +878,11 @@ public function testFormControlClassInjection() $result = $this->Form->checkbox('foo'); $this->assertStringNotContainsString('"form-control"', $result); + $this->assertStringContainsString('class="form-check-input"', $result); $result = $this->Form->radio('foo', ['1' => 'Opt 1', '2' => 'Opt 2']); $this->assertStringNotContainsString('"form-control"', $result); + $this->assertStringContainsString('class="form-check-input"', $result); $result = $this->Form->color('foo'); $this->assertStringContainsString('class="form-control form-control-color"', $result);