From d8d8e9644d5f50c2588f39f1dde3051e51626d42 Mon Sep 17 00:00:00 2001 From: mark_story Date: Wed, 3 Apr 2013 12:46:03 -0400 Subject: [PATCH] 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 --- .../Test/Case/View/Helper/FormHelperTest.php | 21 +++++++++++++++++++ lib/Cake/View/Helper/FormHelper.php | 9 ++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php index 4988cd39fc1..3381f091905 100644 --- a/lib/Cake/Test/Case/View/Helper/FormHelperTest.php +++ b/lib/Cake/Test/Case/View/Helper/FormHelperTest.php @@ -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. diff --git a/lib/Cake/View/Helper/FormHelper.php b/lib/Cake/View/Helper/FormHelper.php index 852eb8816fc..beb191cd2c6 100644 --- a/lib/Cake/View/Helper/FormHelper.php +++ b/lib/Cake/View/Helper/FormHelper.php @@ -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. @@ -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');