mercredi 24 juin 2015

How do I stop Symfony 2 checkbox from returning null

On my Symfony 2 form I have 2 checkboxes. The documentation states

"if the box is checked, the field will be set to true, if the box is unchecked, the value will be set to false"

which is what I'd like to happen but when the checkbox is unchecked I'm actually getting nothing back. I realise a checkbox on a HTML form will normally return nothing if unchecked and I'd usually add some logic to the back end to handle it but it sounds like Symfony should be doing that for me and isn't.

What am I doing wrong or have I missed?

My formtype looks like this:

public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('fullname', 'text')
            ->add('screened', 'checkbox', array(
                'label'     => 'Enable screening emails?' ))
            ->add('dedupe', 'checkbox', array(
                'label'     => 'Enable live dedupeing?'))
 );)

and the relevant bits of the twig template look like this:

<div class="form-group">
    <div class="input-group col-sm-offset-2 col-sm-10">
        <div class="checkbox col-sm-6">
            {{ form_widget(form.screened) }}
            {{ form_label(form.screened) }}
        </div>
        <div class="checkbox col-sm-6">
            {{ form_widget(form.dedupe, {'attr': {'required': 'false'}}) }}
            {{ form_label(form.dedupe) }}
        </div>

    </div>
</div>




Aucun commentaire:

Enregistrer un commentaire