Skip to content
Permalink
Browse files

Allow auto generating "id" attribute when generating individual inputs.

Closes #8189
  • Loading branch information...
ADmad committed Feb 8, 2016
1 parent 5952714 commit 5c21808aad24ba47afcbe05ff80116cfef095fa6
Showing with 64 additions and 0 deletions.
  1. +5 −0 src/View/Helper/FormHelper.php
  2. +59 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -2338,6 +2338,7 @@ public function date($fieldName, array $options = [])
* - `default` - mixed - The value to use if there is no value in the form's context.
* - `disabled` - mixed - Either a boolean indicating disabled state, or the string in
* a numerically indexed value.
* - `id` - mixed - If `true` it will be auto generated based on field name.
*
* This method will convert a numerically indexed 'disabled' into an associative
* array value. FormHelper's internals expect associative options.
@@ -2356,6 +2357,10 @@ protected function _initInputField($field, $options = [])
}
$context = $this->_getContext();
if (isset($options['id']) && $options['id'] === true) {
$options['id'] = $this->_domId($field);
}
$disabledIndex = array_search('disabled', $options, true);
if (is_int($disabledIndex)) {
unset($options[$disabledIndex]);
@@ -7442,4 +7442,63 @@ public function testContext()
$this->assertSame($mock, $this->Form->context($mock));
$this->assertSame($mock, $this->Form->context());
}
/**
* testAutoDomId
*
* @return void
*/
public function testAutoDomId()
{
$result = $this->Form->text('field', ['id' => true]);
$expected = [
'input' => ['type' => 'text', 'name' => 'field', 'id' => 'field'],
];
$this->assertHtml($expected, $result);
// Ensure id => doesn't cause problem when multiple inputs are generated.
$result = $this->Form->radio('field', ['option A', 'option B'], ['id' => true]);
$expected = [
'input' => ['type' => 'hidden', 'name' => 'field', 'value' => ''],
['label' => ['for' => 'field-0']],
['input' => ['type' => 'radio', 'name' => 'field', 'value' => '0', 'id' => 'field-0']],
'option A',
'/label',
['label' => ['for' => 'field-1']],
['input' => ['type' => 'radio', 'name' => 'field', 'value' => '1', 'id' => 'field-1']],
'option B',
'/label',
];
$this->assertHtml($expected, $result);
$result = $this->Form->select(
'multi_field',
['first', 'second'],
['multiple' => 'checkbox', 'id' => true]
);
$expected = [
'input' => [
'type' => 'hidden', 'name' => 'multi_field', 'value' => ''
],
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'multi-field-0']],
['input' => [
'type' => 'checkbox', 'name' => 'multi_field[]',
'value' => '0', 'id' => 'multi-field-0'
]],
'first',
'/label',
'/div',
['div' => ['class' => 'checkbox']],
['label' => ['for' => 'multi-field-1']],
['input' => [
'type' => 'checkbox', 'name' => 'multi_field[]',
'value' => '1', 'id' => 'multi-field-1'
]],
'second',
'/label',
'/div',
];
$this->assertHtml($expected, $result);
}
}

0 comments on commit 5c21808

Please sign in to comment.
You can’t perform that action at this time.