Skip to content
Permalink
Browse files

Applying patch from 'Eärendil' to add option 'hiddenField' for FormHe…

…lper::select() for use with 'multiple'=>'checkbox'. Test cases added. Closes #1161
  • Loading branch information...
ADmad committed Mar 21, 2011
1 parent e277324 commit 69e5854f0aea5fc46d4004483536696288ca3c5d
Showing with 69 additions and 9 deletions.
  1. +13 −9 cake/libs/view/helpers/form.php
  2. +56 −0 cake/tests/cases/libs/view/helpers/form.test.php
@@ -1426,14 +1426,16 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
'escape' => true,
'secure' => null,
'empty' => '',
'showParents' => false
'showParents' => false,
'hiddenField' => true
);
$escapeOptions = $this->_extractOption('escape', $attributes);
$secure = $this->_extractOption('secure', $attributes);
$showEmpty = $this->_extractOption('empty', $attributes);
$showParents = $this->_extractOption('showParents', $attributes);
unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents']);
$hiddenField = $this->_extractOption('hiddenField', $attributes);
unset($attributes['escape'], $attributes['secure'], $attributes['empty'], $attributes['showParents'], $attributes['hiddenField']);
$attributes = $this->_initInputField($fieldName, array_merge(
(array)$attributes, array('secure' => false)
@@ -1456,13 +1458,15 @@ function select($fieldName, $options = array(), $selected = null, $attributes =
$style = ($attributes['multiple'] === 'checkbox') ? 'checkbox' : null;
$template = ($style) ? 'checkboxmultiplestart' : 'selectmultiplestart';
$tag = $this->Html->tags[$template];
$hiddenAttributes = array(
'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'),
'secure' => false,
'name' => $attributes['name']
);
$select[] = $this->hidden(null, $hiddenAttributes);
if ($hiddenField) {
$hiddenAttributes = array(
'value' => '',
'id' => $attributes['id'] . ($style ? '' : '_'),
'secure' => false,
'name' => $attributes['name']
);
$select[] = $this->hidden(null, $hiddenAttributes);
}
} else {
$tag = $this->Html->tags['selectstart'];
}
@@ -3755,6 +3755,62 @@ function testInputMultipleCheckboxes() {
);
$this->assertTags($result, $expected);
}
/**
* testSelectHiddenFieldOmission method
*
* test that select() with 'hiddenField' => false omits the hidden field
*
* @access public
* @return void
*/
function testSelectHiddenFieldOmission() {
$result = $this->Form->select('Model.multi_field',
array('first', 'second'),
null,
array('multiple' => 'checkbox', 'hiddenField' => false)
);
$expected = array(
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
array('label' => array('for' => 'ModelMultiField0')),
'first',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
array('label' => array('for' => 'ModelMultiField1')),
'second',
'/label',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Model.multi_field', array(
'options' => array('first', 'second'),
'multiple' => 'checkbox',
'hiddenField' => false
));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => 'ModelMultiField')),
'Multi Field',
'/label',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '0', 'id' => 'ModelMultiField0')),
array('label' => array('for' => 'ModelMultiField0')),
'first',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'data[Model][multi_field][]', 'value' => '1', 'id' => 'ModelMultiField1')),
array('label' => array('for' => 'ModelMultiField1')),
'second',
'/label',
'/div',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* test that select() with multiple = checkbox works with overriding name attribute.

0 comments on commit 69e5854

Please sign in to comment.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.