Skip to content

[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

Merged
merged 1 commit into from
Jun 10, 2025

Conversation

MatTheCat
Copy link
Contributor

@MatTheCat MatTheCat commented Jun 2, 2025

Q A
Branch? 7.2
Bug fix? yes
New feature? no
Deprecations? no
Issues Fix part of #57430
License MIT

The CollectionType handles not only arrays but also ArrayAccess&Traversable. However when setting its keep_as_list option, array_values would 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. ArrayIterator would skip indexes.

Note that #57430 mentions another issue that would be fixed by #59910.

@MatTheCat MatTheCat requested review from xabbuh and yceruto as code owners June 2, 2025 15:04
@carsonbot carsonbot added this to the 7.2 milestone Jun 2, 2025
@MatTheCat MatTheCat force-pushed the keep_as_list-without-array branch from 945573e to 86b5597 Compare June 2, 2025 15:08
@stof
Copy link
Member

stof commented Jun 2, 2025

Speaking of ArrayIterator, it is a native candidate for ArrayAccess&Traversable so this PR uses it instead of Doctrine’s ArrayCollection which allows to drop the dependency to doctrine/collections.

keeping tests using doctrine/collections has the benefit of ensuring it works with doctrine/collections, which corresponds to a common use case in the ecosystem (not all ArrayAccess&Traversable might behave the same)

@MatTheCat
Copy link
Contributor Author

MatTheCat commented Jun 2, 2025

If we only want to test against Doctrine collections maybe the CollectionType should only accept them rather than ArrayAccess&Traversable?

@stof
Copy link
Member

stof commented Jun 2, 2025

@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 doctrine/collections

@MatTheCat MatTheCat force-pushed the keep_as_list-without-array branch from 86b5597 to 643592f Compare June 2, 2025 15:43
@MatTheCat
Copy link
Contributor Author

Okay kept the existing test against ArrayCollection.

@MatTheCat MatTheCat force-pushed the keep_as_list-without-array branch 2 times, most recently from 4433af8 to 1f4dd71 Compare June 6, 2025 16:50
$listener = new ResizeFormListener(TextType::class, keepAsList: true);
$listener->onSubmit($event);

$this->assertArrayNotHasKey(1, $event->getData());
Copy link
Member

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.

Copy link
Member

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?

Copy link
Contributor Author

@MatTheCat MatTheCat Jun 10, 2025

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.

@MatTheCat MatTheCat force-pushed the keep_as_list-without-array branch from 1f4dd71 to 6c964e7 Compare June 10, 2025 07:46
@xabbuh
Copy link
Member

xabbuh commented Jun 10, 2025

Thank you @MatTheCat.

@xabbuh xabbuh merged commit 455a7e2 into symfony:7.2 Jun 10, 2025
15 of 17 checks passed
@MatTheCat MatTheCat deleted the keep_as_list-without-array branch June 10, 2025 20:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants