Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to allow a checkbox to be empty in symfony?

Tags:

php

symfony

I have defined the following variable within an entity in my application. I have this, among other fields that can be updated via a form interface and I wish to be able to check and uncheck this box on that form.

I can check the box and submit the form fine, but it seems that when I uncheck the box, I can't submit the form. It gives me a Please check this box if you want to proceed.

/**
 * @var boolean $updatesNeeded
 *
 * @ORM\Column(name="updates_needed", type="boolean", nullable=false)
 */
private $updatesNeeded;

I tried changing nullable=false to nullable=true and updating the schema, but it doesn't seem to work. Any ideas would be much appreciated.

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;

class BlahType extends AbstractType
{
    /**
     * @param \Symfony\Component\Form\FormBuilder $builder
     * @param array                               $options
     */
    public function buildForm(FormBuilder $builder, array $options)
    {
        $builder
            ->add('stuff')
            ->add('updatesNeeded', 'choice', array('required' => false))
            ->add('anothervar');
    }
}
like image 257
Squazic Avatar asked Jun 28 '12 19:06

Squazic


Video Answer


1 Answers

This error is because the input is marked with the HTML5 attribute required="required". In your form type you can disable this by setting the required option to false on this checkbox.

$builder->add('updatesNeeded', 'choice', array('required' => false));

http://symfony.com/doc/current/book/forms.html#book-forms-html5-validation-disable

like image 153
MDrollette Avatar answered Oct 18 '22 01:10

MDrollette