Skip to content

Commit

Permalink
Fixing issue where FormHelper::select() with multiple = checkbox and …
Browse files Browse the repository at this point in the history
…a custom name attribute would not work correctly. Fixes #1078
  • Loading branch information
markstory committed Sep 5, 2010
1 parent 5c0fe1b commit 82fffe6
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 7 deletions.
7 changes: 4 additions & 3 deletions cake/libs/view/helpers/form.php
Expand Up @@ -1461,7 +1461,8 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
$hiddenAttributes = array(
'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'),
'secure' => false
'secure' => false,
'name' => $attributes['name']
);
$select[] = $this->hidden(null, $hiddenAttributes);
} else {
Expand Down Expand Up @@ -1495,7 +1496,7 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
$selected,
array(),
$showParents,
array('escape' => $escapeOptions, 'style' => $style)
array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'])
));

$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
Expand Down Expand Up @@ -2041,7 +2042,7 @@ function __selectOptions($elements = array(), $selected = null, $parents = array
$label['class'] = 'selected';
}

list($name) = array_values($this->_name());
$name = $attributes['name'];

if (empty($attributes['class'])) {
$attributes['class'] = 'checkbox';
Expand Down
52 changes: 48 additions & 4 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -3448,7 +3448,10 @@ function testSelectMultipleCheckboxSecurity() {
* @return void
*/
function testInputMultipleCheckboxes() {
$result = $this->Form->input('Model.multi_field', array('options' => array('first', 'second', 'third'), 'multiple' => 'checkbox'));
$result = $this->Form->input('Model.multi_field', array(
'options' => array('first', 'second', 'third'),
'multiple' => 'checkbox'
));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => 'ModelMultiField')),
Expand Down Expand Up @@ -3477,7 +3480,10 @@ function testInputMultipleCheckboxes() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.multi_field', array('options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'), 'multiple' => 'checkbox'));
$result = $this->Form->input('Model.multi_field', array(
'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
'multiple' => 'checkbox'
));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => 'ModelMultiField')),
Expand Down Expand Up @@ -3506,7 +3512,12 @@ function testInputMultipleCheckboxes() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.multi_field', array('options' => array('1' => 'first'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
$result = $this->Form->input('Model.multi_field', array(
'options' => array('1' => 'first'),
'multiple' => 'checkbox',
'label' => false,
'div' => false
));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
array('div' => array('class' => 'checkbox')),
Expand All @@ -3518,7 +3529,12 @@ function testInputMultipleCheckboxes() {
);
$this->assertTags($result, $expected);

$result = $this->Form->input('Model.multi_field', array('options' => array('2' => 'second'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
$result = $this->Form->input('Model.multi_field', array(
'options' => array('2' => 'second'),
'multiple' => 'checkbox',
'label' => false,
'div' => false
));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
array('div' => array('class' => 'checkbox')),
Expand All @@ -3531,6 +3547,34 @@ function testInputMultipleCheckboxes() {
$this->assertTags($result, $expected);
}

/**
* test that select() with multiple = checkbox works with overriding name attribute.
*
* @return void
*/
function testSelectCheckboxMultipleOverrideName() {
$result = $this->Form->input('category', array(
'type' => 'select',
'multiple' => 'checkbox',
'name' => 'data[fish]',
'options' => array('1', '2'),
'div' => false,
'label' => false,
));
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
array('label' => array('for' => 'Category0')), '1', '/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
array('label' => array('for' => 'Category1')), '2', '/label',
'/div'
);
$this->assertTags($result, $expected);
}

/**
* testCheckbox method
*
Expand Down

0 comments on commit 82fffe6

Please sign in to comment.