-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] CollectionType 'keep_as_list' Option with Doctrine Collections #57430
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
Comments
Hello. I also encountered unexpected behavior of the new option: |
Happened on the same just now with CollectionType with If form submission is successful, then data on the entity is updated properly with a list. However if submission fails and submitted data has an empty input you will end up with all empty inputs for the collection in the updated form. |
Can anyone of create a small example application that allows to reproduce the behaviour you observe? |
Will have to think about that. Right now it seems adding |
https://github.com/whataboutpereira/symfony-form-keep-as-list-issue Latest commit built on the symfony skeleton app. If you enter anything in the three collection fields, the data is wiped in the refreshed form when keep_as_list in on. The aforementioned adding $form->setData($data); at the end of onSubmit() after $event->setData($data); in ResizeFormListener.php fixes it. |
Now that I look at it more closely, |
Or even set the data right when adding new children. Unfortunately I'm not knowledgeable enough as to which is the best path. :)
|
Now I see both
|
I now saw this one as well when working with an The tests in So there seem to be at least two separate issues with
|
More info here I've just encountered a related problem with the If I switch the Seems to me this useful features is not fully functional. As someone mentioned above, I'm accustomed to reindex myself so a fix is not critical. Sorry I wish I could provide a link to my project but sorry, it's currently private on github because it's a mess PS : I don't use an ArrayCollection nor Doctrine, it's just a plain normal PHP array |
…eCat) This PR was merged into the 7.2 branch. Discussion ---------- [Form] Fix `keep_as_list` when data is not an array | 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](https://www.php.net/manual/en/arrayiterator.offsetunset.php). Note that #57430 mentions another issue that would be fixed by #59910. Commits ------- 6c964e7 [Form] Fix `keep_as_list` when data is not an array
… collection type is enabled (kells) This PR was merged into the 7.2 branch. Discussion ---------- [Form] Keep submitted values when `keep_as_list` option of collection type is enabled | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix #57430 | License | MIT When the `keep_as_list` option of `CollectionType` is true, its entries lose their values when re-displaying the form with submitted data. This fix only concerns the 2nd point of the [linked issue](#57430 (comment)). Commits ------- bac56de [Form] Keep submitted values when keep_as_list option of collection type is enabled
Uh oh!
There was an error while loading. Please reload this page.
Symfony version(s) affected
7.1.1
Description
When mapping a CollectionType (
'allow_delete' => true
) to an entity property of type ArrayCollection, the following exception is thrown when setting the new 'keep_as_list' optiontrue
:array_values(): Argument #1 ($array) must be of type array, Doctrine\ORM\PersistentCollection given
How to reproduce
Map a CollectionType to an ArrayCollection with
'keep_as_list' => true
.ExampleEntity:
Form with CollectionType:
Possible Solution
Before the 'keep_as_list' option was introduced in Symfony 7.1. I used to re-index my collections manually - which still works for collections of type ArrayCollection, too.
(https://symfonycasts.com/screencast/collections/embedded-validation#fixing-collectiontype-validation-bug).
Not a solution, but a quick fix: Check the type of
$data
in theonSubmit
method inResizeFormListener.php
and convert it into an array if it is an instance of ArrayCollection or PersistentCollection.EDIT: 'keep_as_list' also breaks embedded forms that contain unmapped file upload inputs. Model-, norm- and viewData return empty string/null instead of an UploadedFile object.
Additional Context
No response
The text was updated successfully, but these errors were encountered: