-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] Mismatched index during merge of params/files after submit form with files/collections/checkbox #53359
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
I think that it has been considered as a feature, the |
From my understanding and what I see in the feature code, I will say that is not related. The feature Whereas the issue here is about a collection of items is partially submitted due to checkboxs being unchecked while collection contains files that are all well submitted. |
Hey, thanks for your report! |
Friendly reminder that this issue exists. If I don't hear anything I'll close this. |
Hey, I didn't hear anything so I'm going to close it. Feel free to comment if this is still relevant, I can always reopen! |
Symfony version(s) affected
5.4
Description
Since PR #52255 (or #52347), merge of files/params after submitting a form seems to not result in a correct form data when using collection of files AND fields that are not POST for each element.
How to reproduce
I created a PR with a unit test to try to explain at best how we can reproduce this edge case. See #53353
We could certainty found a more concise example, but here is a form configuration where you could reproduce the bug:
We got the error "This form should not contain extra fields.".
PS: I didnt get time to create a small project to reproduce issue, sorry.
Possible Solution / Root cause
I dig in into this error, and here is what I found.
Merge of files data and params data are done by static function FormUtil::mergeParamsAndFiles.
Params
When we submit our form with only item n°2 checked, params will only contains thoses values:
Very important note here (and maybe the unique cause ?), fields from collections items number 1 and 3 are not submitted here. It's explained as checkbox are never submitted in form when they are unchecked.
Extracted from W3C :
Files
Then we have our files, where each item collection have at least one, so our data when submitting ($_FILES) will look like this:
Nothing anormal here.
Merge
Here it comes...
During the merge with the new code of FormUtil function, we will got a result like this:
😮
As you can see, index for root collection have changed. We lost index "0" and we have a new index "3".
Obviously, we will be in trouble here as Symfony will lost data from collection item with index 0 and will try to add a new item.
If we didn't authorize it, we will then get the error about extra fields.
Expected result was:
Workaround ?
For my use case, it has been simple to get a workaround by just adding a hidden field in the root collection. This way, my params array is always complete with all index and merge is done successfully.
Fix ?
I have tried a quick fix by changing
mergeParamsAndFiles
methods like this:It work fine in my use case but unfortunately, it cause regressions in the test function testMergeParamsAndFilesMultiple.
What to do next ?
So here is what I see for now:
Who can fix it ?
With some help I could produce a fix as I already created a unit test to reproduce the issue in this PR #53353.
Is this test seems a good start point for you ?
Is this issue enough impacting to trying to create a fix ?
Do you see some more use case where this issue could occur ?
Appreciate your feedbacks 🙂
The text was updated successfully, but these errors were encountered: