fixes disabled attribute for multiple checkboxes

This commit is contained in:
euromark 2012-07-23 16:39:46 +02:00 committed by mark_story
parent 22c1ac9622
commit a208eb6cb1
2 changed files with 86 additions and 2 deletions

View file

@ -2339,6 +2339,78 @@ class FormHelperTest extends CakeTestCase {
}
}
/**
* 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.
*

View file

@ -1801,7 +1801,8 @@ class FormHelper extends AppHelper {
'secure' => true,
'empty' => '',
'showParents' => false,
'hiddenField' => true
'hiddenField' => true,
'disabled' => false
);
$escapeOptions = $this->_extractOption('escape', $attributes);
@ -1874,7 +1875,8 @@ class FormHelper extends AppHelper {
'name' => $attributes['name'],
'value' => $attributes['value'],
'class' => $attributes['class'],
'id' => $attributes['id']
'id' => $attributes['id'],
'disabled' => $attributes['disabled'],
)
));
@ -2427,6 +2429,16 @@ class FormHelper extends AppHelper {
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;