Skip to content

Commit d8d8e96

Browse files
committed
Fix array('disabled') and array('disabled' => true) working differently.
The shortform should work the same as the longer form with regards to disabling field locking with SecurityComponent. Fixes #3734
1 parent 1fa7d90 commit d8d8e96

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Cake/Test/Case/View/Helper/FormHelperTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1307,6 +1307,27 @@ public function testFormSecuredRadio() {
13071307
$this->assertEquals($expected, $this->Form->fields);
13081308
}
13091309

1310+
/**
1311+
* Test that when disabled is in a list based attribute array it works.
1312+
*
1313+
* @return void
1314+
*/
1315+
public function testFormSecuredAndDisabledNotAssoc() {
1316+
$this->Form->request['_Token'] = array('key' => 'testKey');
1317+
1318+
$this->Form->select('Model.select', array(1, 2), array('disabled'));
1319+
$this->Form->checkbox('Model.checkbox', array('disabled'));
1320+
$this->Form->text('Model.text', array('disabled'));
1321+
$this->Form->textarea('Model.textarea', array('disabled'));
1322+
$this->Form->password('Model.password', array('disabled'));
1323+
$this->Form->radio('Model.radio', array(1, 2), array('disabled'));
1324+
1325+
$expected = array(
1326+
'Model.radio' => ''
1327+
);
1328+
$this->assertEquals($expected, $this->Form->fields);
1329+
}
1330+
13101331
/**
13111332
* test that forms with disabled inputs + secured forms leave off the inputs from the form
13121333
* hashing.

lib/Cake/View/Helper/FormHelper.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2797,6 +2797,9 @@ protected function _generateOptions($name, $options = array()) {
27972797
* Disabling the field using the `disabled` option, will also omit the field from being
27982798
* part of the hashed key.
27992799
*
2800+
* This method will convert a numerically indexed 'disabled' into a associative
2801+
* value. FormHelper's internals expect associative options.
2802+
*
28002803
* @param string $field Name of the field to initialize options for.
28012804
* @param array $options Array of options to append options into.
28022805
* @return array Array of options for the input.
@@ -2809,6 +2812,12 @@ protected function _initInputField($field, $options = array()) {
28092812
$secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
28102813
}
28112814

2815+
$disabledIndex = array_search('disabled', $options, true);
2816+
if ($disabledIndex !== false) {
2817+
unset($options[$disabledIndex]);
2818+
$options['disabled'] = true;
2819+
}
2820+
28122821
$result = parent::_initInputField($field, $options);
28132822
if ($this->tagIsInvalid() !== false) {
28142823
$result = $this->addClass($result, 'form-error');

0 commit comments

Comments
 (0)