diff --git a/src/View/Helper/FormHelper.php b/src/View/Helper/FormHelper.php index 857391bb141..29eb52ac12f 100644 --- a/src/View/Helper/FormHelper.php +++ b/src/View/Helper/FormHelper.php @@ -253,6 +253,13 @@ class FormHelper extends Helper */ protected $_valueSources = ['context']; + /** + * Grouped input types. + * + * @var array + */ + protected $_groupedInputTypes = ['radio', 'multicheckbox', 'date', 'time', 'datetime']; + /** * Construct the widgets and binds the default context providers * @@ -280,6 +287,11 @@ public function __construct(View $View, array $config = []) unset($config['widgets']); } + if (isset($config['groupedInputTypes'])) { + $this->_groupedInputTypes = $config['groupedInputTypes']; + unset($config['groupedInputTypes']); + } + parent::__construct($View, $config); if (!$locator) { @@ -1550,8 +1562,7 @@ protected function _inputLabel($fieldName, $label, $options) } $labelAttributes['for'] = $options['id']; - $groupTypes = ['radio', 'multicheckbox', 'date', 'time', 'datetime']; - if (in_array($options['type'], $groupTypes, true)) { + if (in_array($options['type'], $this->_groupedInputTypes, true)) { $labelAttributes['for'] = false; } if ($options['nestedInput']) { diff --git a/tests/TestCase/View/Helper/FormHelperTest.php b/tests/TestCase/View/Helper/FormHelperTest.php index 703ff547053..488d9a30e74 100644 --- a/tests/TestCase/View/Helper/FormHelperTest.php +++ b/tests/TestCase/View/Helper/FormHelperTest.php @@ -259,6 +259,22 @@ public function testSetAndGetWidgetLocator() $this->assertSame($locator, $helper->getWidgetLocator()); } + /** + * Test overridding grouped input types which controls generation of "for" + * attribute of labels. + * + * @return void + */ + public function testConstructWithGroupedInputTypes() + { + $helper = new FormHelper($this->View, [ + 'groupedInputTypes' => ['radio'], + ]); + + $result = $helper->control('when', ['type' => 'datetime']); + $this->assertContains('', $result); + } + /** * Test registering a new widget class and rendering it. *