Browse Source

Port fixes in #7289 to 3.x

If a visible input is created after a hidden input was created, the
form would always blackhole unless the visible input had the same value
as the hidden input.
Mark Story 10 years ago
parent
commit
2173112e3a
2 changed files with 27 additions and 0 deletions
  1. 2 0
      src/View/Helper/FormHelper.php
  2. 25 0
      tests/TestCase/View/Helper/FormHelperTest.php

+ 2 - 0
src/View/Helper/FormHelper.php

@@ -653,6 +653,8 @@ class FormHelper extends Helper
             if (!in_array($field, $this->fields)) {
                 if ($value !== null) {
                     return $this->fields[$field] = $value;
+                } elseif (isset($this->fields[$field]) && $value === null) {
+                    unset($this->fields[$field]);
                 }
                 $this->fields[] = $field;
             }

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

@@ -1667,6 +1667,31 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test that a hidden field followed by a visible field
+     * undoes the hidden field locking.
+     *
+     * @return void
+     */
+    public function testSecuredInputDuplicate()
+    {
+        $this->Form->request->params['_Token'] = ['key' => 'testKey'];
+        $this->assertEquals([], $this->Form->fields);
+
+        $this->Form->input('text_val', [
+                'type' => 'hidden',
+                'value' => 'some text',
+        ]);
+        $expected = ['text_val' => 'some text'];
+        $this->assertEquals($expected, $this->Form->fields);
+
+        $this->Form->input('text_val', [
+                'type' => 'text',
+        ]);
+        $expected = ['text_val'];
+        $this->assertEquals($expected, $this->Form->fields);
+    }
+
+    /**
      * Tests that the correct keys are added to the field hash index
      *
      * @return void