Browse Source

Fix hiddenField for radio.

Make hiddenField options work as documented for radio buttons. This is
a forward port of #11021 to 3.x

Refs #11002
Mark Story 8 years ago
parent
commit
3f568e0716
2 changed files with 19 additions and 1 deletions
  1. 1 1
      src/View/Helper/FormHelper.php
  2. 18 0
      tests/TestCase/View/Helper/FormHelperTest.php

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

@@ -1586,7 +1586,7 @@ class FormHelper extends Helper
         $hidden = '';
         if ($hiddenField) {
             $hidden = $this->hidden($fieldName, [
-                'value' => '',
+                'value' => $hiddenField === true ? '' : $hiddenField,
                 'form' => isset($attributes['form']) ? $attributes['form'] : null,
                 'name' => $attributes['name'],
             ]);

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

@@ -4694,6 +4694,24 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test setting a hiddenField value on radio buttons.
+     *
+     * @return void
+     */
+    public function testRadioHiddenFieldValue()
+    {
+        $result = $this->Form->radio('title', ['option A'], ['hiddenField' => 'N']);
+        $expected = [
+            ['input' => ['type' => 'hidden', 'name' => 'title', 'value' => 'N']],
+            'label' => ['for' => 'title-0'],
+            ['input' => ['type' => 'radio', 'name' => 'title', 'value' => '0', 'id' => 'title-0']],
+            'option A',
+            '/label',
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * testControlRadio method
      *
      * Test that input works with radio types.