Skip to content
Permalink
Browse files

Implement FormHelper::multiCheckbox()

Add the basic method and unskip some of the existing tests.
  • Loading branch information...
markstory committed Feb 19, 2014
1 parent 25c26ff commit 39962eb355a05d12944b780f465dafe479c0dd91
Showing with 82 additions and 77 deletions.
  1. +52 −6 src/View/Helper/FormHelper.php
  2. +30 −71 tests/TestCase/View/Helper/FormHelperTest.php
@@ -171,6 +171,7 @@ class FormHelper extends Helper {
protected $_defaultTemplates = [
'button' => '<button{{attrs}}>{{text}}</button>',
'checkbox' => '<input type="checkbox" name="{{name}}" value="{{value}}"{{attrs}}>',
'checkboxContainer' => '<div class="checkbox">{{input}}{{label}}</div>',
'file' => '<input type="file" name="{{name}}"{{attrs}}>',
'formstart' => '<form{{attrs}}>',
'formend' => '</form>',
@@ -1798,10 +1799,8 @@ public function submit($caption = null, $options = array()) {
* that string is displayed as the empty element.
* - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
* - `val` The selected value of the input.
* - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
* - `disabled` - Control the disabled attribute. When creating a select box, set to true to disable the
* select box. When creating checkboxes, `true` will disable all checkboxes. You can also set disabled
* to a list of values you want to disable when creating checkboxes.
* select box. Set to an array to disable specific option elements.
*
* ### Using options
*
@@ -1840,6 +1839,7 @@ public function submit($caption = null, $options = array()) {
* @param array $attributes The HTML attributes of the select element.
* @return string Formatted SELECT element
* @link http://book.cakephp.org/2.0/en/core-libraries/helpers/form.html#options-for-select-checkbox-and-radio-inputs
* @see Cake\View\Helper\FormHelper::multiCheckbox() for creating multiple checkboxes.
*/
public function select($fieldName, $options = [], $attributes = []) {
$attributes += [
@@ -1851,6 +1851,11 @@ public function select($fieldName, $options = [], $attributes = []) {
'empty' => isset($attributes['multiple']) ? false : true
];
if ($attributes['multiple'] === 'checkbox') {
unset($attributes['multiple'], $attributes['empty']);
return $this->multiCheckbox($fieldName, $options, $attributes);
}
// Secure the field if there are options, or its a multi select.
// Single selects with no options don't submit, but multiselects do.
if (
@@ -1868,17 +1873,58 @@ public function select($fieldName, $options = [], $attributes = []) {
$hidden = '';
if ($attributes['multiple'] && $attributes['hiddenField']) {
$hiddenAttributes = array(
'name' => $attributes['name'],
'value' => '',
'secure' => false,
);
$hidden = $this->hidden($fieldName, $hiddenAttributes);
}
unset($attributes['hiddenField']);
return $hidden . $this->widget('select', $attributes);
}
if ($attributes['multiple'] === 'checkbox') {
// TODO add multi-checkbox.
/**
* Creates a set of checkboxes out of options.
*
* ### Options
*
* - `escape` - If true contents of options will be HTML entity encoded. Defaults to true.
* - `val` The selected value of the input.
* - `class` - When using multiple = checkbox the class name to apply to the divs. Defaults to 'checkbox'.
* - `disabled` - Control the disabled attribute. When creating checkboxes, `true` will disable all checkboxes.
* You can also set disabled to a list of values you want to disable when creating checkboxes.
* - `hiddenField` - Set to false to remove the hidden field that ensures a value
* is always submitted.
*
* Can be used in place of a select box with the multiple attribute.
*
* @param string $fieldName Name attribute of the SELECT
* @param array $options Array of the OPTION elements (as 'value'=>'Text' pairs) to be used in the
* checkboxes element.
* @param array $attributes The HTML attributes of the select element.
* @return string Formatted SELECT element
* @see Cake\View\Helper\FormHelper::select() for supported option formats.
*/
public function multiCheckbox($fieldName, $options, $attributes = []) {
$attributes += [
'disabled' => null,
'escape' => true,
'hiddenField' => true,
'secure' => true,
];
$attributes = $this->_initInputField($fieldName, $attributes);
$attributes['options'] = $options;
$hidden = '';
if ($attributes['hiddenField']) {
$hiddenAttributes = array(
'name' => $attributes['name'],
'value' => '',
'secure' => false,
);
$hidden = $this->hidden($fieldName, $hiddenAttributes);
}
return $hidden . $this->widget('select', $attributes);
return $hidden . $this->widget('multicheckbox', $attributes);
}
/**
@@ -3880,37 +3880,44 @@ public function testInputsPluginModel() {
* @return void
*/
public function testSelectAsCheckbox() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->select('Model.multi_field', array('first', 'second', 'third'), array('multiple' => 'checkbox', 'value' => array(0, 1)));
$result = $this->Form->select(
'Model.multi_field',
array('first', 'second', 'third'),
array('multiple' => 'checkbox', 'value' => array(0, 1))
);
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'ModelMultiField0')),
array('label' => array('for' => 'ModelMultiField0', 'class' => 'selected')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '0', 'id' => 'model-multi-field-0')),
array('label' => array('for' => 'model-multi-field-0', 'class' => 'selected')),
'first',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'ModelMultiField1')),
array('label' => array('for' => 'ModelMultiField1', 'class' => 'selected')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'checked' => 'checked', 'value' => '1', 'id' => 'model-multi-field-1')),
array('label' => array('for' => 'model-multi-field-1', 'class' => 'selected')),
'second',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'ModelMultiField2')),
array('label' => array('for' => 'ModelMultiField2')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '2', 'id' => 'model-multi-field-2')),
array('label' => array('for' => 'model-multi-field-2')),
'third',
'/label',
'/div',
);
$this->assertTags($result, $expected);
$result = $this->Form->select('Model.multi_field', array('1/2' => 'half'), array('multiple' => 'checkbox'));
$result = $this->Form->select(
'Model.multi_field',
array('1/2' => 'half'),
array('multiple' => 'checkbox')
);
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'ModelMultiField'),
'input' => array('type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => ''),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'ModelMultiField1/22')),
array('label' => array('for' => 'ModelMultiField1/22')),
array('input' => array('type' => 'checkbox', 'name' => 'Model[multi_field][]', 'value' => '1/2', 'id' => 'model-multi-field-1-2')),
array('label' => array('for' => 'model-multi-field-1-2')),
'half',
'/label',
'/div',
@@ -5212,74 +5219,26 @@ public function testSelectHiddenFieldOmission() {
* @return void
*/
public function testSelectCheckboxMultipleOverrideName() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->input('category', array(
'type' => 'select',
$result = $this->Form->select('category', ['1', '2'], [
'multiple' => 'checkbox',
'name' => 'fish',
'options' => array('1', '2'),
'div' => false,
'label' => false,
));
]);
$expected = array(
'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => '', 'id' => 'category'),
'input' => array('type' => 'hidden', 'name' => 'fish', 'value' => ''),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'Category0')),
array('label' => array('for' => 'Category0')), '1', '/label',
array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '0', 'id' => 'fish0')),
array('label' => array('for' => 'fish0')), '1', '/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'Category1')),
array('label' => array('for' => 'Category1')), '2', '/label',
array('input' => array('type' => 'checkbox', 'name' => 'fish[]', 'value' => '1', 'id' => 'fish1')),
array('label' => array('for' => 'fish1')), '2', '/label',
'/div'
);
$this->assertTags($result, $expected);
}
/**
* Test that 'id' overrides all the checkbox id's as well.
*
* @return void
*/
public function testSelectCheckboxMultipleId() {
$this->markTestIncomplete('Need to revisit once models work again.');
$result = $this->Form->select(
'Model.multi_field',
array('first', 'second', 'third'),
array('multiple' => 'checkbox', 'id' => 'CustomId')
);
$expected = array(
'input' => array(
'type' => 'hidden', 'name' => 'Model[multi_field]', 'value' => '', 'id' => 'CustomId'
),
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'Model[multi_field][]',
'value' => '0', 'id' => 'CustomId0'
)),
array('label' => array('for' => 'CustomId0')),
'first',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'Model[multi_field][]',
'value' => '1', 'id' => 'CustomId1'
)),
array('label' => array('for' => 'CustomId1')),
'second',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array(
'type' => 'checkbox', 'name' => 'Model[multi_field][]',
'value' => '2', 'id' => 'CustomId2'
)),
array('label' => array('for' => 'CustomId2')),
'third',
'/label',
'/div'
);
$result = $this->Form->multiCheckbox('category', ['1', '2'], [
'name' => 'fish',
]);
$this->assertTags($result, $expected);
}

0 comments on commit 39962eb

Please sign in to comment.
You can’t perform that action at this time.