Skip to content

Commit

Permalink
Fix ResizeFormListener using integer keys
Browse files Browse the repository at this point in the history
When using CollectionType, the data is an array with integer keys
and the expected type is FormInterface|string.
  • Loading branch information
franmomu authored and jordisala1991 committed Nov 25, 2021
1 parent 7b5f2b7 commit 22472f2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/EventListener/ResizeFormListener.php
Expand Up @@ -105,6 +105,8 @@ public function preSetData(FormEvent $event): void
'data' => $value,
]);

$name = \is_int($name) ? (string) $name : $name;

$form->add($name, $this->type, $options);
}
}
Expand Down
21 changes: 21 additions & 0 deletions tests/EventListener/ResizeFormListenerTest.php
Expand Up @@ -53,6 +53,27 @@ public function testPreSetDataWithNullData(): void
$listener->preSetData($event);
}

public function testPreSetDataWithArrayData(): void
{
$listener = new ResizeFormListener('form', [], false, null);

$form = $this->createMock(Form::class);
$form
->method('getIterator')
->willReturn(new \ArrayIterator());
$form
->expects(static::exactly(2))
->method('add')
->withConsecutive(
['0'],
['1'],
);

$event = new FormEvent($form, ['foo', 'bar']);

$listener->preSetData($event);
}

public function testPreSetDataThrowsExceptionWithStringEventData(): void
{
$listener = new ResizeFormListener('form', [], false, null);
Expand Down

0 comments on commit 22472f2

Please sign in to comment.