{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/20179","repository_url":"https://api.github.com/repos/symfony/symfony","labels_url":"https://api.github.com/repos/symfony/symfony/issues/20179/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/20179/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/20179/events","html_url":"https://github.com/symfony/symfony/issues/20179","id":181630943,"number":20179,"title":"Cannot uncheck checkbox field mapped to the entity","user":{"login":"ttibensky","id":5850762,"avatar_url":"https://avatars.githubusercontent.com/u/5850762?v=3","gravatar_id":"","url":"https://api.github.com/users/ttibensky","html_url":"https://github.com/ttibensky","followers_url":"https://api.github.com/users/ttibensky/followers","following_url":"https://api.github.com/users/ttibensky/following{/other_user}","gists_url":"https://api.github.com/users/ttibensky/gists{/gist_id}","starred_url":"https://api.github.com/users/ttibensky/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ttibensky/subscriptions","organizations_url":"https://api.github.com/users/ttibensky/orgs","repos_url":"https://api.github.com/users/ttibensky/repos","events_url":"https://api.github.com/users/ttibensky/events{/privacy}","received_events_url":"https://api.github.com/users/ttibensky/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/symfony/symfony/labels/Form","name":"Form","color":"DDDDDD"}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":6,"created_at":"2016-10-07T10:06:52Z","updated_at":"2016-10-12T18:20:55Z","closed_at":null,"body":"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:\r\n\r\n```php\r\n<?php\r\n\r\nnamespace SomeCompany\\SomeBundle\\Form\\Type\\Person\\Author;\r\n\r\nuse Symfony\\Component\\Form\\AbstractType;\r\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\CheckboxType;\r\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\HiddenType;\r\nuse Symfony\\Component\\Form\\FormBuilderInterface;\r\nuse Symfony\\Component\\OptionsResolver\\OptionsResolver;\r\n\r\nclass AlertWhenWrittenType extends AbstractType\r\n{\r\n    /**\r\n     * @param FormBuilderInterface $builder\r\n     * @param array $options\r\n     */\r\n    public function buildForm(FormBuilderInterface $builder, array $options)\r\n    {\r\n        $builder->add('checkbox', CheckboxType::class, [\r\n            'required' => false,\r\n        ]);\r\n    }\r\n\r\n    /**\r\n     * @param OptionsResolver $resolver\r\n     */\r\n    public function configureOptions(OptionsResolver $resolver)\r\n    {\r\n        $resolver->setDefaults(['csrf_protection' => false]);\r\n    }\r\n}\r\n```\r\n\r\nSo I tried to add another field (hidden, not mapped and with empty_data set to true) just to see what happens:\r\n\r\n```php\r\n<?php\r\n\r\nnamespace SomeCompany\\SomeBundle\\Form\\Type\\Person\\Author;\r\n\r\nuse Symfony\\Component\\Form\\AbstractType;\r\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\CheckboxType;\r\nuse Symfony\\Component\\Form\\Extension\\Core\\Type\\HiddenType;\r\nuse Symfony\\Component\\Form\\FormBuilderInterface;\r\nuse Symfony\\Component\\OptionsResolver\\OptionsResolver;\r\n\r\nclass AlertWhenWrittenType extends AbstractType\r\n{\r\n    /**\r\n     * @param FormBuilderInterface $builder\r\n     * @param array $options\r\n     */\r\n    public function buildForm(FormBuilderInterface $builder, array $options)\r\n    {\r\n        $builder->add('checkbox', CheckboxType::class, [\r\n            'required' => false,\r\n        ]);\r\n\r\n        // if there is only a checkbox field in the form,\r\n        // it can't be unchecked, if you uncheck it and submit the form\r\n        // form validation won't pass\r\n        // magically, if I add any another field,\r\n        // you will be able to uncheck the checkbox and validation will pass\r\n        // this is needed until it is fixed in symfony core\r\n        $builder->add('uglyHack', HiddenType::class, [\r\n            'required' => false,\r\n            'mapped' => false,\r\n            'empty_data' => true,\r\n        ]);\r\n    }\r\n\r\n    /**\r\n     * @param OptionsResolver $resolver\r\n     */\r\n    public function configureOptions(OptionsResolver $resolver)\r\n    {\r\n        $resolver->setDefaults(['csrf_protection' => false]);\r\n    }\r\n}\r\n```\r\n\r\nalso needed to add that field to my twig template\r\n\r\n```twig\r\n{{ form_widget(form.checkbox) }}\r\n{{ form_widget(form.uglyHack) }}\r\n```\r\n\r\nand now, miraculously, Im able to uncheck the checkbox and submit the form. Validation now passes and entity field value is now set to false.\r\nI 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\r\nand I think that might cause the problem to appear. Maybe empty $_POST is handled wrong?\r\n\r\nI'm using symfony 3.0.9"},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/253295264","html_url":"https://github.com/symfony/symfony/issues/20179#issuecomment-253295264","issue_url":"https://api.github.com/repos/symfony/symfony/issues/20179","id":253295264,"user":{"login":"xabbuh","id":1957048,"avatar_url":"https://avatars.githubusercontent.com/u/1957048?v=3","gravatar_id":"","url":"https://api.github.com/users/xabbuh","html_url":"https://github.com/xabbuh","followers_url":"https://api.github.com/users/xabbuh/followers","following_url":"https://api.github.com/users/xabbuh/following{/other_user}","gists_url":"https://api.github.com/users/xabbuh/gists{/gist_id}","starred_url":"https://api.github.com/users/xabbuh/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/xabbuh/subscriptions","organizations_url":"https://api.github.com/users/xabbuh/orgs","repos_url":"https://api.github.com/users/xabbuh/repos","events_url":"https://api.github.com/users/xabbuh/events{/privacy}","received_events_url":"https://api.github.com/users/xabbuh/received_events","type":"User","site_admin":false},"created_at":"2016-10-12T18:20:55Z","updated_at":"2016-10-12T18:20:55Z","body":"see #20210 for a PR"}}
{"actor":{"display_login":"xabbuh"}}