Browse Source

Merge pull request #7854 from leighmackay/ticket-7849

Fix for nested input fields that end with brackets
Mark Story 10 years ago
parent
commit
a947fc4963
2 changed files with 29 additions and 1 deletions
  1. 6 1
      src/View/Helper/FormHelper.php
  2. 23 0
      tests/TestCase/View/Helper/FormHelperTest.php

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

@@ -2349,9 +2349,14 @@ class FormHelper extends Helper
         }
 
         if (!isset($options['name'])) {
+            $endsWithBrackets = '';
+            if (substr($field, -2) === '[]') {
+                $field = substr($field, 0, -2);
+                $endsWithBrackets = '[]';
+            }
             $parts = explode('.', $field);
             $first = array_shift($parts);
-            $options['name'] = $first . ($parts ? '[' . implode('][', $parts) . ']' : '');
+            $options['name'] = $first . ($parts ? '[' . implode('][', $parts) . ']' : '') . $endsWithBrackets;
         }
 
         if (isset($options['value']) && !isset($options['val'])) {

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

@@ -2513,6 +2513,29 @@ class FormHelperTest extends TestCase
     }
 
     /**
+     * Test that nested inputs end with brackets
+     *
+     * @return void
+     */
+    public function testNestedInputsEndWithBrackets()
+    {
+        $result = $this->Form->text('nested.text[]');
+        $expected = [
+            'input' => [
+                'type' => 'text', 'name' => 'nested[text][]'
+            ],
+        ];
+
+        $result = $this->Form->file('nested.file[]');
+        $expected = [
+            'input' => [
+                'type' => 'file', 'name' => 'nested[file][]'
+            ],
+        ];
+        $this->assertHtml($expected, $result);
+    }
+
+    /**
      * Test id prefix
      *
      * @return void