Description
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:
<?php
namespace SomeCompany\SomeBundle\Form\Type\Person\Author;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AlertWhenWrittenType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('checkbox', CheckboxType::class, [
'required' => false,
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['csrf_protection' => false]);
}
}
So I tried to add another field (hidden, not mapped and with empty_data set to true) just to see what happens:
<?php
namespace SomeCompany\SomeBundle\Form\Type\Person\Author;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CheckboxType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class AlertWhenWrittenType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('checkbox', CheckboxType::class, [
'required' => false,
]);
// if there is only a checkbox field in the form,
// it can't be unchecked, if you uncheck it and submit the form
// form validation won't pass
// magically, if I add any another field,
// you will be able to uncheck the checkbox and validation will pass
// this is needed until it is fixed in symfony core
$builder->add('uglyHack', HiddenType::class, [
'required' => false,
'mapped' => false,
'empty_data' => true,
]);
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(['csrf_protection' => false]);
}
}
also needed to add that field to my twig template
{{ form_widget(form.checkbox) }}
{{ form_widget(form.uglyHack) }}
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