Skip to content

Commit

Permalink
CollectionType alterFieldValues() items indexes/keys bugfix (#694)
Browse files Browse the repository at this point in the history
  • Loading branch information
rudiedirkx authored May 18, 2022
1 parent 61bb1a2 commit f070631
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 7 deletions.
9 changes: 6 additions & 3 deletions src/Kris/LaravelFormBuilder/Fields/CollectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ public function getAllAttributes()
*/
public function alterFieldValues(array &$values)
{
foreach ($this->children as $i => $child) {
$stripLeft = strlen($this->getName()) + 1;
$stripRight = 1;
foreach ($this->children as $child) {
if (method_exists($child, 'alterFieldValues')) {
if (isset($values[$i])) {
$child->alterFieldValues($values[$i]);
$itemKey = substr($child->getName(), $stripLeft, -$stripRight);
if (isset($values[$itemKey])) {
$child->alterFieldValues($values[$itemKey]);
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions tests/FormTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -352,8 +352,8 @@ public function it_returns_altered_field_values()
$this->request['name'] = 'lower case';
$this->request['subcustom'] = ['title' => 'Bar foo', 'body' => 'Foo bar'];
$this->request['subcustom_collection'] = [
['title' => 'Item 1 title', 'body' => 'Item 1 body'],
['title' => 'Item 2 title', 'body' => 'Item 2 body'],
5 => ['title' => 'Item 1 title', 'body' => 'Item 1 body'],
17 => ['title' => 'Item 2 title', 'body' => 'Item 2 body'],
];

$customForm = $this->formBuilder->create('CustomNesterDummyForm');
Expand All @@ -364,8 +364,8 @@ public function it_returns_altered_field_values()
'options' => ['x'],
'subcustom' => ['title' => 'Bar foo', 'body' => 'FOO BAR'],
'subcustom_collection' => [
['title' => 'Item 1 title', 'body' => 'ITEM 1 BODY'],
['title' => 'Item 2 title', 'body' => 'ITEM 2 BODY'],
5 => ['title' => 'Item 1 title', 'body' => 'ITEM 1 BODY'],
17 => ['title' => 'Item 2 title', 'body' => 'ITEM 2 BODY'],
],
],
$customForm->getFieldValues()
Expand Down

0 comments on commit f070631

Please sign in to comment.