Skip to content

Commit 82fffe6

Browse files
committed
Fixing issue where FormHelper::select() with multiple = checkbox and a custom name attribute would not work correctly. Fixes #1078
1 parent 5c0fe1b commit 82fffe6

File tree

2 files changed

+52
-7
lines changed

2 files changed

+52
-7
lines changed

cake/libs/view/helpers/form.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,7 +1461,8 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
14611461
$hiddenAttributes = array(
14621462
'value' => '',
14631463
'id' => $attributes['id'] . ($style ? '' : '_'),
1464-
'secure' => false
1464+
'secure' => false,
1465+
'name' => $attributes['name']
14651466
);
14661467
$select[] = $this->hidden(null, $hiddenAttributes);
14671468
} else {
@@ -1495,7 +1496,7 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
14951496
$selected,
14961497
array(),
14971498
$showParents,
1498-
array('escape' => $escapeOptions, 'style' => $style)
1499+
array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'])
14991500
));
15001501

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

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

20462047
if (empty($attributes['class'])) {
20472048
$attributes['class'] = 'checkbox';

cake/tests/cases/libs/view/helpers/form.test.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3448,7 +3448,10 @@ function testSelectMultipleCheckboxSecurity() {
34483448
* @return void
34493449
*/
34503450
function testInputMultipleCheckboxes() {
3451-
$result = $this->Form->input('Model.multi_field', array('options' => array('first', 'second', 'third'), 'multiple' => 'checkbox'));
3451+
$result = $this->Form->input('Model.multi_field', array(
3452+
'options' => array('first', 'second', 'third'),
3453+
'multiple' => 'checkbox'
3454+
));
34523455
$expected = array(
34533456
array('div' => array('class' => 'input select')),
34543457
array('label' => array('for' => 'ModelMultiField')),
@@ -3477,7 +3480,10 @@ function testInputMultipleCheckboxes() {
34773480
);
34783481
$this->assertTags($result, $expected);
34793482

3480-
$result = $this->Form->input('Model.multi_field', array('options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'), 'multiple' => 'checkbox'));
3483+
$result = $this->Form->input('Model.multi_field', array(
3484+
'options' => array('a' => 'first', 'b' => 'second', 'c' => 'third'),
3485+
'multiple' => 'checkbox'
3486+
));
34813487
$expected = array(
34823488
array('div' => array('class' => 'input select')),
34833489
array('label' => array('for' => 'ModelMultiField')),
@@ -3506,7 +3512,12 @@ function testInputMultipleCheckboxes() {
35063512
);
35073513
$this->assertTags($result, $expected);
35083514

3509-
$result = $this->Form->input('Model.multi_field', array('options' => array('1' => 'first'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
3515+
$result = $this->Form->input('Model.multi_field', array(
3516+
'options' => array('1' => 'first'),
3517+
'multiple' => 'checkbox',
3518+
'label' => false,
3519+
'div' => false
3520+
));
35103521
$expected = array(
35113522
'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
35123523
array('div' => array('class' => 'checkbox')),
@@ -3518,7 +3529,12 @@ function testInputMultipleCheckboxes() {
35183529
);
35193530
$this->assertTags($result, $expected);
35203531

3521-
$result = $this->Form->input('Model.multi_field', array('options' => array('2' => 'second'), 'multiple' => 'checkbox', 'label' => false, 'div' => false));
3532+
$result = $this->Form->input('Model.multi_field', array(
3533+
'options' => array('2' => 'second'),
3534+
'multiple' => 'checkbox',
3535+
'label' => false,
3536+
'div' => false
3537+
));
35223538
$expected = array(
35233539
'input' => array('type' => 'hidden', 'name' => 'data[Model][multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
35243540
array('div' => array('class' => 'checkbox')),
@@ -3531,6 +3547,34 @@ function testInputMultipleCheckboxes() {
35313547
$this->assertTags($result, $expected);
35323548
}
35333549

3550+
/**
3551+
* test that select() with multiple = checkbox works with overriding name attribute.
3552+
*
3553+
* @return void
3554+
*/
3555+
function testSelectCheckboxMultipleOverrideName() {
3556+
$result = $this->Form->input('category', array(
3557+
'type' => 'select',
3558+
'multiple' => 'checkbox',
3559+
'name' => 'data[fish]',
3560+
'options' => array('1', '2'),
3561+
'div' => false,
3562+
'label' => false,
3563+
));
3564+
$expected = array(
3565+
'input' => array('type' => 'hidden', 'name' => 'data[fish]', 'value' => '', 'id' => 'category'),
3566+
array('div' => array('class' => 'checkbox')),
3567+
array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '0', 'id' => 'Category0')),
3568+
array('label' => array('for' => 'Category0')), '1', '/label',
3569+
'/div',
3570+
array('div' => array('class' => 'checkbox')),
3571+
array('input' => array('type' => 'checkbox', 'name' => 'data[fish][]', 'value' => '1', 'id' => 'Category1')),
3572+
array('label' => array('for' => 'Category1')), '2', '/label',
3573+
'/div'
3574+
);
3575+
$this->assertTags($result, $expected);
3576+
}
3577+
35343578
/**
35353579
* testCheckbox method
35363580
*

0 commit comments

Comments
 (0)