Skip to content

Commit

Permalink
Applying patch from 'Eärendil' to add option 'hiddenField' for FormHe…
Browse files Browse the repository at this point in the history
…lper::select() for use with 'multiple'=>'checkbox'. Test cases added. Closes #1161
  • Loading branch information
ADmad committed Mar 21, 2011
1 parent e277324 commit 69e5854
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 9 deletions.
22 changes: 13 additions & 9 deletions cake/libs/view/helpers/form.php
Expand Up @@ -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)
Expand All @@ -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'];
}
Expand Down
56 changes: 56 additions & 0 deletions cake/tests/cases/libs/view/helpers/form.test.php
Expand Up @@ -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.
Expand Down

0 comments on commit 69e5854

Please sign in to comment.