Skip to content

Commit

Permalink
Fix array('disabled') and array('disabled' => true) working differently.
Browse files Browse the repository at this point in the history
The shortform should work the same as the longer form with regards to
disabling field locking with SecurityComponent.

Fixes #3734
  • Loading branch information
markstory committed Apr 3, 2013
1 parent 1fa7d90 commit d8d8e96
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
21 changes: 21 additions & 0 deletions lib/Cake/Test/Case/View/Helper/FormHelperTest.php
Expand Up @@ -1307,6 +1307,27 @@ public function testFormSecuredRadio() {
$this->assertEquals($expected, $this->Form->fields);
}

/**
* Test that when disabled is in a list based attribute array it works.
*
* @return void
*/
public function testFormSecuredAndDisabledNotAssoc() {
$this->Form->request['_Token'] = array('key' => 'testKey');

$this->Form->select('Model.select', array(1, 2), array('disabled'));
$this->Form->checkbox('Model.checkbox', array('disabled'));
$this->Form->text('Model.text', array('disabled'));
$this->Form->textarea('Model.textarea', array('disabled'));
$this->Form->password('Model.password', array('disabled'));
$this->Form->radio('Model.radio', array(1, 2), array('disabled'));

$expected = array(
'Model.radio' => ''
);
$this->assertEquals($expected, $this->Form->fields);
}

/**
* test that forms with disabled inputs + secured forms leave off the inputs from the form
* hashing.
Expand Down
9 changes: 9 additions & 0 deletions lib/Cake/View/Helper/FormHelper.php
Expand Up @@ -2797,6 +2797,9 @@ protected function _generateOptions($name, $options = array()) {
* Disabling the field using the `disabled` option, will also omit the field from being
* part of the hashed key.
*
* This method will convert a numerically indexed 'disabled' into a associative
* value. FormHelper's internals expect associative options.
*
* @param string $field Name of the field to initialize options for.
* @param array $options Array of options to append options into.
* @return array Array of options for the input.
Expand All @@ -2809,6 +2812,12 @@ protected function _initInputField($field, $options = array()) {
$secure = (isset($this->request['_Token']) && !empty($this->request['_Token']));
}

$disabledIndex = array_search('disabled', $options, true);
if ($disabledIndex !== false) {
unset($options[$disabledIndex]);
$options['disabled'] = true;
}

$result = parent::_initInputField($field, $options);
if ($this->tagIsInvalid() !== false) {
$result = $this->addClass($result, 'form-error');
Expand Down

0 comments on commit d8d8e96

Please sign in to comment.