Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

fixes disabled attribute for multiple checkboxes

  • Loading branch information...
commit a208eb6cb1e5489c722639fac0460e09d335191b 1 parent 22c1ac9
@dereuromark dereuromark authored markstory committed
View
72 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
@@ -2340,6 +2340,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.
*
* @return void
View
16 lib/Cake/View/Helper/FormHelper.php
@@ -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;
Please sign in to comment.
Something went wrong with that request. Please try again.