Browse Source

Always output the radio hidden field if its enabled.

Always include hidden input on radio buttons if the option is set. There
can exist conditions where the value is not in the available options. In
these situations, the generated inputs did not submit a value, which can
cause issues with SecurityComponent.

Refs #6771
Mark Story 10 years ago
parent
commit
1a324c8c06
2 changed files with 44 additions and 1 deletions
  1. 1 1
      src/View/Helper/FormHelper.php
  2. 43 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 1 - 1
src/View/Helper/FormHelper.php

@@ -1450,7 +1450,7 @@ class FormHelper extends Helper
         $radio = $this->widget('radio', $attributes);
 
         $hidden = '';
-        if ($hiddenField && (!isset($value) || $value === '')) {
+        if ($hiddenField) {
             $hidden = $this->hidden($fieldName, [
                 'value' => '',
                 'form' => isset($attributes['form']) ? $attributes['form'] : null,

+ 43 - 0
tests/TestCase/View/Helper/FormHelperTest.php

@@ -3802,6 +3802,29 @@ class FormHelperTest extends TestCase
         $result = $this->Form->input('test', [
             'type' => 'radio',
             'options' => ['A', 'B'],
+            'value' => '0'
+        ]);
+        $expected = [
+            ['div' => ['class' => 'input radio']],
+                '<label',
+                'Test',
+                '/label',
+                ['input' => ['type' => 'hidden', 'name' => 'test', 'value' => '']],
+                ['label' => ['for' => 'test-0']],
+                    ['input' => ['type' => 'radio', 'checked' => 'checked', 'name' => 'test', 'value' => '0', 'id' => 'test-0']],
+                    'A',
+                '/label',
+                ['label' => ['for' => 'test-1']],
+                    ['input' => ['type' => 'radio', 'name' => 'test', 'value' => '1', 'id' => 'test-1']],
+                    'B',
+                '/label',
+            '/div',
+        ];
+        $this->assertHtml($expected, $result);
+
+        $result = $this->Form->input('test', [
+            'type' => 'radio',
+            'options' => ['A', 'B'],
             'label' => false
         ]);
         $expected = [
@@ -3897,6 +3920,26 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * testRadio method
+     *
+     * Test radio element set generation
+     *
+     * @return void
+     */
+    public function testRadioOutOfRange()
+    {
+        $result = $this->Form->radio('Model.field', ['v' => 'value'], ['value' => 'nope']);
+        $expected = [
+            'input' => ['type' => 'hidden', 'name' => 'Model[field]', 'value' => ''],
+            'label' => ['for' => 'model-field-v'],
+            ['input' => ['type' => 'radio', 'name' => 'Model[field]', 'value' => 'v', 'id' => 'model-field-v']],
+            'value',
+            '/label'
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * testSelect method
      *
      * Test select element generation.