-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Cannot uncheck checkbox field mapped to the entity #20179
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 issue is that the Form component is not able to detect that the form was submitted as none of the fields is present (at least when using |
This reminds me of #17899 Maybe Symfony forms could set initial state for all the checkboxes (in case of POST) as boolean false and then continue to work with checkboxes internally as booleans, instead of expecting strict nulls. Both problems would be solved. |
@gadelat but thats not how html checkbox works. I agree with what you said but it would need to become a |
@ttibensky I'm aware that this mess is caused by how forms in web browsers were designed (wrong IMO), but it needs to be dealt with somehow. |
in Contao this issue is solved by including a hidden input in front of the checkbox input with the same name as the checkbox, this way if the checkbox is unchecked the POST data looks like |
see #20210 for a PR |
@xabbuh looks good, this solves the issue perfectly. Feel free to close this ticket after it is merged. |
@ttibensky Could you please confirm that #17771 is fixing your issue as well? Thanks! |
@HeahDude I'm not using PATCH method, but yeah, it does the thing. For me this ticket is done. |
Thank you @ttibensky for confirming this :) |
I'm having issues submitting a form which contains checkbox field. When the mapped field in the entity is true, checkbox is checked and I want to uncheck it and submit the form it won't pass through form validation. I found some issues mentioned this and Im aware of if checkbox is unchecked, its value isn't present in the POST request, so it should be interpreted as null. I have other checkboxes in other forms on my site, but all of the others works. So I was thinking and realized that the form witch doesn't work contains only the checkbox field and nothing else:
So I tried to add another field (hidden, not mapped and with empty_data set to true) just to see what happens:
also needed to add that field to my twig template
and now, miraculously, Im able to uncheck the checkbox and submit the form. Validation now passes and entity field value is now set to false.
I think that its happening because there is only the checkbox field in the form. If checkbox is not checked, its value is not send through POST, but there are no other fields so $_POST is empty
and I think that might cause the problem to appear. Maybe empty $_POST is handled wrong?
I'm using symfony 3.0.9
The text was updated successfully, but these errors were encountered: