8000 [Form] FixCheckboxInputListener loses sort order of submitted values · Issue #10575 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[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

Closed
Richtermeister opened this issue Mar 28, 2014 · 3 comments
Closed
Labels

Comments

@Richtermeister
Copy link
Contributor

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.

@Richtermeister Richtermeister changed the title FixCheckboxInputListener loses sort order of submitted values [Form] FixCheckboxInputListener loses sort order of submitted values Mar 28, 2014
@jakzal jakzal added the Form label Mar 29, 2014
@webmozart
Copy link
Contributor

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.

@webmozart
Copy link
Contributor

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 Form::submit(), which forwards the individual entries to the children of the form. Then, the normalized data is read back from the children in order to construct the array of submitted choices.

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! :)

@Richtermeister
Copy link
Contributor Author

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants
0