Skip to content

Commit f070631

Browse files
authored
CollectionType alterFieldValues() items indexes/keys bugfix (#694)
1 parent 61bb1a2 commit f070631

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Kris/LaravelFormBuilder/Fields/CollectionType.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,13 @@ public function getAllAttributes()
7979
*/
8080
public function alterFieldValues(array &$values)
8181
{
82-
foreach ($this->children as $i => $child) {
82+
$stripLeft = strlen($this->getName()) + 1;
83+
$stripRight = 1;
84+
foreach ($this->children as $child) {
8385
if (method_exists($child, 'alterFieldValues')) {
84-
if (isset($values[$i])) {
85-
$child->alterFieldValues($values[$i]);
86+
$itemKey = substr($child->getName(), $stripLeft, -$stripRight);
87+
if (isset($values[$itemKey])) {
88+
$child->alterFieldValues($values[$itemKey]);
8689
}
8790
}
8891
}

tests/FormTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -352,8 +352,8 @@ public function it_returns_altered_field_values()
352352
$this->request['name'] = 'lower case';
353353
$this->request['subcustom'] = ['title' => 'Bar foo', 'body' => 'Foo bar'];
354354
$this->request['subcustom_collection'] = [
355-
['title' => 'Item 1 title', 'body' => 'Item 1 body'],
356-
['title' => 'Item 2 title', 'body' => 'Item 2 body'],
355+
5 => ['title' => 'Item 1 title', 'body' => 'Item 1 body'],
356+
17 => ['title' => 'Item 2 title', 'body' => 'Item 2 body'],
357357
];
358358

359359
$customForm = $this->formBuilder->create('CustomNesterDummyForm');
@@ -364,8 +364,8 @@ public function it_returns_altered_field_values()
364364
'options' => ['x'],
365365
'subcustom' => ['title' => 'Bar foo', 'body' => 'FOO BAR'],
366366
'subcustom_collection' => [
367-
['title' => 'Item 1 title', 'body' => 'ITEM 1 BODY'],
368-
['title' => 'Item 2 title', 'body' => 'ITEM 2 BODY'],
367+
5 => ['title' => 'Item 1 title', 'body' => 'ITEM 1 BODY'],
368+
17 => ['title' => 'Item 2 title', 'body' => 'ITEM 2 BODY'],
369369
],
370370
],
371371
$customForm->getFieldValues()

0 commit comments

Comments
 (0)