Skip to content

Commit

Permalink
Changing how 'id' attribute is treated for multiple checkboxes.
Browse files Browse the repository at this point in the history
Setting the id option, will now also override the generated id's for
checkboxes.
Fixes #1691
  • Loading branch information
markstory committed Jun 7, 2011
1 parent 71f7dcd commit 71e2c8e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 4 deletions.
47 changes: 47 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -3948,6 +3948,53 @@ public function testSelectCheckboxMultipleOverrideName() {
$this->assertTags($result, $expected);
}

/**
* Test that 'id' overrides all the checkbox id's as well.
*
* @return void
*/
public function testSelectCheckboxMultipleId() {
$result = $this->Form->select(
'Model.multi_field',
array('first', 'second', 'third'),
array('multiple' => 'checkbox', 'id' => 'CustomId')
);

$expected = array(
'input' => array(
'type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'CustomId'
),
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
'value' => '0', 'id' => 'CustomId0'
)),
array('label' => array('for' => 'CustomId0')),
'first',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
'value' => '1', 'id' => 'CustomId1'
)),
array('label' => array('for' => 'CustomId1')),
'second',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'data[Model][multi_field][]',
'value' => '2', 'id' => 'CustomId2'
)),
array('label' => array('for' => 'CustomId2')),
'third',
'/label',
'/div'
);
$this->assertTags($result, $expected);
}

/**
* testCheckbox method
*
Expand Down
18 changes: 14 additions & 4 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -1518,6 +1518,7 @@ public function select($fieldName, $options = array(), $attributes = array()) {
$showParents = $this->_extractOption('showParents', $attributes);
$hiddenField = $this->_extractOption('hiddenField', $attributes);
unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
$id = $this->_extractOption('id', $attributes);

$attributes = $this->_initInputField($fieldName, array_merge(
(array)$attributes, array('secure' => false)
Expand Down Expand Up @@ -1573,11 +1574,22 @@ public function select($fieldName, $options = array(), $attributes = array()) {
$options = array_reverse($options, true);
}

if (!$id) {
$attributes['id'] = Inflector::camelize($attributes['id']);
}

$select = array_merge($select, $this->__selectOptions(
array_reverse($options, true),
array(),
$showParents,
array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'], 'value' => $attributes['value'], 'class' => $attributes['class'])
array(
'escape' => $escapeOptions,
'style' => $style,
'name' => $attributes['name'],
'value' => $attributes['value'],
'class' => $attributes['class'],
'id' => $attributes['id']
)
));

$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
Expand Down Expand Up @@ -2119,9 +2131,7 @@ function __selectOptions($elements = array(), $parents = array(), $showParents =
if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name;

$tagName = Inflector::camelize(
$this->model() . '_' . $this->field().'_'.Inflector::slug($name)
);
$tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
$htmlOptions['id'] = $tagName;
$label = array('for' => $tagName);

Expand Down

0 comments on commit 71e2c8e

Please sign in to comment.