diff --git a/src/EventListener/ResizeFormListener.php b/src/EventListener/ResizeFormListener.php index 1d4c3492..8f666a7d 100644 --- a/src/EventListener/ResizeFormListener.php +++ b/src/EventListener/ResizeFormListener.php @@ -88,8 +88,10 @@ public function preSetData(FormEvent $event): void } // First remove all rows except for the prototype row + // Type cast to string, because Symfony form can returns integer keys foreach ($form as $name => $child) { - $form->remove($name); + // @phpstan-ignore-next-line + $form->remove((string) $name); } // Then add all rows again in the correct order @@ -126,12 +128,15 @@ public function preSubmit(FormEvent $event): void } // Remove all empty rows except for the prototype row + // Type cast to string, because Symfony form can returns integer keys foreach ($form as $name => $child) { - $form->remove($name); + // @phpstan-ignore-next-line + $form->remove((string) $name); } // Add all additional rows foreach ($data as $name => $value) { + // Type cast to string, because Symfony form can returns integer keys if (!$form->has((string) $name)) { $buildOptions = [ 'property_path' => '['.$name.']', @@ -178,6 +183,7 @@ public function onSubmit(FormEvent $event): void } foreach ($data as $name => $child) { + // Type cast to string, because Symfony form can returns integer keys if (!$form->has((string) $name)) { unset($data[$name]); } diff --git a/tests/EventListener/ResizeFormListenerTest.php b/tests/EventListener/ResizeFormListenerTest.php index 9a6b5371..f175dbf7 100644 --- a/tests/EventListener/ResizeFormListenerTest.php +++ b/tests/EventListener/ResizeFormListenerTest.php @@ -190,23 +190,37 @@ public function testPreSubmitData(): void $listener = new ResizeFormListener('form', $typeOptions, true, null); - $options = [ + $options1 = [ 'property_path' => '[baz]', 'default' => 'option', ]; + $options2 = [ + 'property_path' => '[0]', + 'default' => 'option', + ]; + $form = $this->createMock(Form::class); $form->expects(static::once()) ->method('getIterator') - ->willReturn(new \ArrayIterator(['foo' => 'bar'])); - $form->expects(static::once()) + ->willReturn(new \ArrayIterator([ + 'foo' => 'bar', + 0 => 'daz', + ])); + $form->expects(static::exactly(2)) ->method('remove') - ->with('foo'); - $form->expects(static::once()) + ->withConsecutive( + ['foo'], + [0] + ); + $form->expects(static::exactly(2)) ->method('add') - ->with('baz', 'form', $options); + ->withConsecutive( + ['baz', 'form', $options1], + [0, 'form', $options2] + ); - $data = ['baz' => 'caz']; + $data = ['baz' => 'caz', 0 => 'daz']; $event = new FormEvent($form, $data); @@ -295,23 +309,26 @@ public function testOnSubmit(): void $form = $this->createMock(Form::class); $form - ->expects(static::exactly(3)) + ->expects(static::exactly(4)) ->method('has') ->withConsecutive( ['foo'], ['bar'], - ['baz'] + ['baz'], + [0] ) ->willReturnOnConsecutiveCalls( false, false, - true + true, + false, ); $data = [ 'foo' => 'foo-value', 'bar' => 'bar-value', 'baz' => 'baz-value', + 0 => '0-value', ]; $removedData = [