-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[Form] Fix keep_as_list when data is not an array
#60638
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.