Browse Source

Port fixes from #5616 to 3.0.

FormHelper shared the same issues in 3.0 as it did in 2.x. Given that
merging was going to be a nightmare, I've copy&paste merged these
changes on to 3.0.
Mark Story 11 years ago
parent
commit
5114ffe37b
2 changed files with 33 additions and 8 deletions
  1. 14 4
      src/View/Helper/FormHelper.php
  2. 19 4
      tests/TestCase/View/Helper/FormHelperTest.php

+ 14 - 4
src/View/Helper/FormHelper.php

@@ -1387,15 +1387,14 @@ class FormHelper extends Helper
      */
     public function radio($fieldName, $options = [], array $attributes = [])
     {
+        $attributes['options'] = $options;
+        $attributes['idPrefix'] = $this->_idPrefix;
         $attributes = $this->_initInputField($fieldName, $attributes);
 
         $value = $attributes['val'];
         $hiddenField = isset($attributes['hiddenField']) ? $attributes['hiddenField'] : true;
         unset($attributes['hiddenField']);
 
-        $attributes['options'] = $options;
-        $attributes['idPrefix'] = $this->_idPrefix;
-
         $radio = $this->widget('radio', $attributes);
 
         $hidden = '';
@@ -2318,7 +2317,18 @@ class FormHelper extends Helper
         if ($context->hasError($field)) {
             $options = $this->addClass($options, $this->_config['errorClass']);
         }
-        if (!empty($options['disabled'])) {
+        $isDisabled = false;
+        if (isset($options['disabled'])) {
+            $isDisabled = (
+                $options['disabled'] === true ||
+                $options['disabled'] === 'disabled' ||
+                (is_array($options['disabled']) &&
+                    !empty($options['options']) &&
+                    array_diff($options['options'], $options['disabled']) === array()
+                )
+            );
+        }
+        if ($isDisabled) {
             $options['secure'] = self::SECURE_SKIP;
         }
         if ($options['secure'] === self::SECURE_SKIP) {

+ 19 - 4
tests/TestCase/View/Helper/FormHelperTest.php

@@ -1544,10 +1544,13 @@ class FormHelperTest extends TestCase
 
             $result = $this->Form->fields;
             $expected = [
-            'ratio', 'population', 'published', 'other',
-            'stuff' => '',
-            'hidden' => '0',
-            'something'
+                'ratio',
+                'population',
+                'published',
+                'other',
+                'stuff' => '',
+                'hidden' => '0',
+                'something'
             ];
             $this->assertEquals($expected, $result);
 
@@ -1645,6 +1648,18 @@ class FormHelperTest extends TestCase
         $this->Form->radio('Test.test', $options);
         $expected = ['Test.test'];
         $this->assertEquals($expected, $this->Form->fields);
+
+        $this->Form->radio('Test.all', $options, [
+            'disabled' => ['option1', 'option2']
+        ]);
+        $expected = ['Test.test', 'Test.all' => ''];
+        $this->assertEquals($expected, $this->Form->fields);
+
+        $this->Form->radio('Test.some', $options, [
+            'disabled' => ['option1']
+        ]);
+        $expected = ['Test.test', 'Test.all' => '', 'Test.some'];
+        $this->assertEquals($expected, $this->Form->fields);
     }
 
     /**