File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
Extension/Core/EventListener
Tests/Extension/Core/EventListener Expand file tree Collapse file tree 2 files changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -199,7 +199,13 @@ public function onSubmit(FormEvent $event): void
199199 }
200200
201201 if ($ this ->keepAsList ) {
202- $ formReindex = [];
202+ $ formReindex = $ dataKeys = [];
203+ foreach ($ data as $ key => $ value ) {
204+ $ dataKeys [] = $ key ;
205+ }
206+ foreach ($ dataKeys as $ key ) {
207+ unset($ data [$ key ]);
208+ }
203209 foreach ($ form as $ name => $ child ) {
204210 $ formReindex [] = $ child ;
205211 $ form ->remove ($ name );
@@ -208,8 +214,8 @@ public function onSubmit(FormEvent $event): void
208214 $ form ->add ($ index , $ this ->type , array_replace ([
209215 'property_path ' => '[ ' .$ index .'] ' ,
210216 ], $ this ->options ));
217+ $ data [$ index ] = $ child ->getData ();
211218 }
212- $ data = array_values ($ data );
213219 }
214220
215221 $ event ->setData ($ data );
Original file line number Diff line number Diff line change @@ -310,7 +310,7 @@ public function testOnSubmitDealsWithObjectBackedIteratorAggregate()
310310 $ this ->assertArrayNotHasKey (2 , $ event ->getData ());
311311 }
312312
313- public function testOnSubmitDealsWithArrayBackedIteratorAggregate ()
313+ public function testOnSubmitDealsWithDoctrineCollection ()
314314 {
315315 $ this ->builder ->add ($ this ->getBuilder ('1 ' ));
316316
@@ -323,6 +323,19 @@ public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
323323 $ this ->assertArrayNotHasKey (2 , $ event ->getData ());
324324 }
325325
326+ public function testKeepAsListWorksWithTraversableArrayAccess ()
327+ {
328+ $ this ->builder ->add ($ this ->getBuilder ('1 ' ));
329+
330+ $ data = new \ArrayIterator ([0 => 'first ' , 1 => 'second ' , 2 => 'third ' ]);
331+ $ event = new FormEvent ($ this ->builder ->getForm (), $ data );
332+ $ listener = new ResizeFormListener (TextType::class, keepAsList: true );
333+ $ listener ->onSubmit ($ event );
334+
335+ $ this ->assertCount (1 , $ event ->getData ());
336+ $ this ->assertArrayHasKey (0 , $ event ->getData ());
337+ }
338+
326339 public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete ()
327340 {
328341 $ this ->builder ->setData (['0 ' => 'first ' , '1 ' => 'second ' ]);
You can’t perform that action at this time.
0 commit comments