[Form] Fix keep_as_list when data is not an array#60638
Conversation
945573e to
86b5597
Compare
keeping tests using |
|
If we only want to test against Doctrine collections maybe the |
|
@MatTheCat I did not say "only" in my message. But keeping a test using ArrayCollection brings value to our testsuite, even if it implies a dev dependency on |
86b5597 to
643592f
Compare
|
Okay kept the existing test against |
4433af8 to
1f4dd71
Compare
| $listener = new ResizeFormListener(TextType::class, keepAsList: true); | ||
| $listener->onSubmit($event); | ||
|
|
||
| $this->assertArrayNotHasKey(1, $event->getData()); |
There was a problem hiding this comment.
I debugged the new code locally a bit but I am not sure that we get the expected result here. If I don't miss anything $event->getData() returns new \ArrayIterator(3 => null]); here.
There was a problem hiding this comment.
The null part can be ignored as we don't have a fully built form here, but is 3 really the index we expect here?
There was a problem hiding this comment.
Good catch, I was not aware arrays behave like this; index should have been 0 not 3.
I updated the code and test accordingly.
1f4dd71 to
6c964e7
Compare
|
Thank you @MatTheCat. |
The
CollectionTypehandles not only arrays but alsoArrayAccess&Traversable. However when setting itskeep_as_listoption,array_valueswould then be called and crash.To avoid this, this PR reindexes the data by getting its keys and unsetting them. This is because unsetting in a loop can produce counter-intuitive results; e.g.
ArrayIteratorwould skip indexes.Note that #57430 mentions another issue that would be fixed by #59910.