Skip to content

Commit

Permalink
fixes disabled attribute for multiple checkboxes
Browse files Browse the repository at this point in the history
  • Loading branch information
euromark authored and markstory committed Aug 3, 2012
1 parent 22c1ac9 commit a208eb6
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
72 changes: 72 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -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.
*
Expand Down
16 changes: 14 additions & 2 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -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);
Expand Down Expand Up @@ -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'],
)
));

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit a208eb6

Please sign in to comment.