File tree Expand file tree Collapse file tree 2 files changed +22
-3
lines changed
src/Symfony/Component/Form
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
199
199
}
200
200
201
201
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
+ }
203
209
foreach ($ form as $ name => $ child ) {
204
210
$ formReindex [] = $ child ;
205
211
$ form ->remove ($ name );
@@ -208,8 +214,8 @@ public function onSubmit(FormEvent $event): void
208
214
$ form ->add ($ index , $ this ->type , array_replace ([
209
215
'property_path ' => '[ ' .$ index .'] ' ,
210
216
], $ this ->options ));
217
+ $ data [] = $ child ->getData ();
211
218
}
212
- $ data = array_values ($ data );
213
219
}
214
220
215
221
$ event ->setData ($ data );
Original file line number Diff line number Diff line change @@ -310,7 +310,7 @@ public function testOnSubmitDealsWithObjectBackedIteratorAggregate()
310
310
$ this ->assertArrayNotHasKey (2 , $ event ->getData ());
311
311
}
312
312
313
- public function testOnSubmitDealsWithArrayBackedIteratorAggregate ()
313
+ public function testOnSubmitDealsWithDoctrineCollection ()
314
314
{
315
315
$ this ->builder ->add ($ this ->getBuilder ('1 ' ));
316
316
@@ -323,6 +323,19 @@ public function testOnSubmitDealsWithArrayBackedIteratorAggregate()
323
323
$ this ->assertArrayNotHasKey (2 , $ event ->getData ());
324
324
}
325
325
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 ->assertArrayNotHasKey (1 , $ event ->getData ());
336
+ $ this ->assertArrayNotHasKey (2 , $ event ->getData ());
337
+ }
338
+
326
339
public function testOnSubmitDeleteEmptyNotCompoundEntriesIfAllowDelete ()
327
340
{
328
341
$ this ->builder ->setData (['0 ' => 'first ' , '1 ' => 'second ' ]);
You can’t perform that action at this time.
0 commit comments