Fixing issue where FormHelper::select() with multiple = checkbox and a custom name attribute would not work correctly. Fixes #1078

This commit is contained in:
Mark Story 2010-09-05 01:28:13 -04:00
parent 5c0fe1b16e
commit 82fffe6914
2 changed files with 52 additions and 7 deletions

View file

@ -1461,7 +1461,8 @@ class FormHelper extends AppHelper {
$hiddenAttributes = array(
'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'),
'secure' => false
'secure' => false,
'name' => $attributes['name']
);
$select[] = $this->hidden(null, $hiddenAttributes);
} else {
@ -1495,7 +1496,7 @@ class FormHelper extends AppHelper {
$selected,
array(),
$showParents,
array('escape' => $escapeOptions, 'style' => $style)
array('escape' => $escapeOptions, 'style' => $style, 'name' => $attributes['name'])
));
$template = ($style == 'checkbox') ? 'checkboxmultipleend' : 'selectend';
@ -2041,7 +2042,7 @@ class FormHelper extends AppHelper {
$label['class'] = 'selected';
}
list($name) = array_values($this->_name());
$name = $attributes['name'];
if (empty($attributes['class'])) {
$attributes['class'] = 'checkbox';

View file

@ -3448,7 +3448,10 @@ class FormHelperTest extends CakeTestCase {
* @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')),
@ -3477,7 +3480,10 @@ class FormHelperTest extends CakeTestCase {
);
$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')),
@ -3506,7 +3512,12 @@ class FormHelperTest extends CakeTestCase {
);
$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')),
@ -3518,7 +3529,12 @@ class FormHelperTest extends CakeTestCase {
);
$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')),
@ -3531,6 +3547,34 @@ class FormHelperTest extends CakeTestCase {
$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
*