Permalink
Browse files

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
  • Loading branch information...
1 parent 1fa7d90 commit d8d8e9644d5f50c2588f39f1dde3051e51626d42 @markstory markstory committed Apr 3, 2013
Showing with 30 additions and 0 deletions.
  1. +21 −0 lib/Cake/Test/Case/View/Helper/FormHelperTest.php
  2. +9 −0 lib/Cake/View/Helper/FormHelper.php
@@ -1308,6 +1308,27 @@ public function testFormSecuredRadio() {
}
/**
+ * 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.
*
@@ -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');

0 comments on commit d8d8e96

Please sign in to comment.