Skip to content
Permalink
Browse files

fix for nested input fields that end with brackets

  • Loading branch information...
leighmackay committed Dec 15, 2015
1 parent 875c4fa commit 203c69d0a69a410a7efb51bc9a7ce1638bcf705a
Showing with 29 additions and 1 deletion.
  1. +6 −1 src/View/Helper/FormHelper.php
  2. +23 −0 tests/TestCase/View/Helper/FormHelperTest.php
@@ -2349,9 +2349,14 @@ protected function _initInputField($field, $options = [])
}
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'])) {
@@ -2512,6 +2512,29 @@ public function testInputWithTemplateFile()
$this->assertHtml($expected, $result);
}
/**
* 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
*

0 comments on commit 203c69d

Please sign in to comment.
You can’t perform that action at this time.