Skip to content
Permalink
Browse files

fixes disabled attribute for multiple checkboxes

  • Loading branch information...
dereuromark authored and markstory committed Jul 23, 2012
1 parent 22c1ac9 commit a208eb6cb1e5489c722639fac0460e09d335191b
Showing with 86 additions and 2 deletions.
  1. +72 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +14 −2 lib/Cake/View/Helper/FormHelper.php
@@ -2339,6 +2339,78 @@ public function testInputCheckboxesInLoop() {
}
}
/**
* Test generating checkboxes with disabled elements.
*
* @return void
*/
public function testInputCheckboxWithDisabledElements() {
$options = array(1 => 'One', 2 => 'Two', '3' => 'Three');
$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => 'disabled', 'options' => $options));
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => "ContactMultiple")),
'Multiple',
'/label',
array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'disabled' => 'disabled', 'id' => "ContactMultiple1")),
array('label' => array('for' => "ContactMultiple1")),
'One',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
array('label' => array('for' => "ContactMultiple2")),
'Two',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
array('label' => array('for' => "ContactMultiple3")),
'Three',
'/label',
'/div',
'/div'
);
$this->assertTags($result, $expected);
$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => true, 'options' => $options));
$this->assertTags($result, $expected);
$disabled = array('2', 3);
$expected = array(
array('div' => array('class' => 'input select')),
array('label' => array('for' => "ContactMultiple")),
'Multiple',
'/label',
array('input' => array('type' => 'hidden', 'name' => "data[Contact][multiple]", 'value' => '', 'id' => "ContactMultiple")),
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 1, 'id' => "ContactMultiple1")),
array('label' => array('for' => "ContactMultiple1")),
'One',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 2, 'disabled' => 'disabled', 'id' => "ContactMultiple2")),
array('label' => array('for' => "ContactMultiple2")),
'Two',
'/label',
'/div',
array('div' => array('class' => 'checkbox')),
array('input' => array('type' => 'checkbox', 'name' => "data[Contact][multiple][]", 'value' => 3, 'disabled' => 'disabled', 'id' => "ContactMultiple3")),
array('label' => array('for' => "ContactMultiple3")),
'Three',
'/label',
'/div',
'/div'
);
$result = $this->Form->input('Contact.multiple', array('multiple' => 'checkbox', 'disabled' => $disabled, 'options' => $options));
$this->assertTags($result, $expected);
}
/**
* test input name with leading integer, ensure attributes are generated correctly.
*
@@ -1801,7 +1801,8 @@ public function select($fieldName, $options = array(), $attributes = array()) {
'secure' => true,
'empty' => '',
'showParents' => false,
'hiddenField' => true
'hiddenField' => true,
'disabled' => false
);
$escapeOptions = $this->_extractOption('escape', $attributes);
@@ -1874,7 +1875,8 @@ public function select($fieldName, $options = array(), $attributes = array()) {
'name' => $attributes['name'],
'value' => $attributes['value'],
'class' => $attributes['class'],
'id' => $attributes['id']
'id' => $attributes['id'],
'disabled' => $attributes['disabled'],
)
));
@@ -2427,6 +2429,16 @@ protected function _selectOptions($elements = array(), $parents = array(), $show
if ($attributes['style'] === 'checkbox') {
$htmlOptions['value'] = $name;
if (!empty($attributes['disabled'])) {
if (is_array($attributes['disabled'])) {
if (in_array($htmlOptions['value'], $attributes['disabled'])) {
$htmlOptions['disabled'] = 'disabled';
}
} else {
$htmlOptions['disabled'] = $attributes['disabled'] === true ? 'disabled' : $attributes['disabled'];
}
}
$tagName = $attributes['id'] . Inflector::camelize(Inflector::slug($name));
$htmlOptions['id'] = $tagName;

0 comments on commit a208eb6

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