-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Form] FixCheckboxInputListener loses sort order of submitted values #10575
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
The reason for that is that ChoicesToBooleanArrayTransformer uses the choice indices to relate submitted checkboxes to the actual choices. I'm not quite sure right now though why we don't set the "value" attribute in the checkboxes and use the value instead of the index. The latter would allow to keep the submitted order. |
Hi @Richtermeister, I investigated this issue now. In fact, it doesn't really matter which order the FixCheckboxInputListeners uses to set up the data array, because this data array is later passed to The key to solving your problem seems to be to add another event listener that reorders the checkboxes in the choice field according to the order of the submitted values. However, I don't really want to do this in core, since this also comes with a performance impact and I'm not sure how many people actually need this functionality. I hope this helps! :) |
@webmozart I really appreciate the time you took to investigate. This makes me understand this better. I agree, this is not a core-default issue. I think it might make a nice option, though (preserve_order?), that can be set on a choice widget. I will try to create a PR to this effect. Thanks for your feedback! |
I am trying to make a checkbox list sortable, and retain the order of the submitted values on the server, but I noticed that the FixCheckboxInputListener always forces the submitted values back into the order of the original choices. In fact, this seems to be its specific purpose, but I'm not quite understanding why.
The specific code in question is this:
https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Form/Extension/Core/EventListener/FixCheckboxInputListener.php#L57
Also, a bit earlier in the code the comments indicate that due to performance considerations we would want to work with the array of submitted values, rather than all available choices, but the loop I'm referencing loops over the long array instead..
Please let me know if I am not understanding the purpose of this class right.. otherwise I would like to fix it to retain the original order of submitted values. Happy to submit the PR to this effect.
Thank you.
The text was updated successfully, but these errors were encountered: