Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Allow configuring grouped input types.
Closes #11850.
  • Loading branch information
ADmad committed Apr 2, 2018
1 parent 7037d74 commit aae9471
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/View/Helper/FormHelper.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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']) {
Expand Down
16 changes: 16 additions & 0 deletions tests/TestCase/View/Helper/FormHelperTest.php
Expand Up @@ -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('<label for="when">When</label>', $result);
}

/**
* Test registering a new widget class and rendering it.
*
Expand Down

0 comments on commit aae9471

Please sign in to comment.