vendredi 7 août 2020

Symfony FORM not treated because Checkboxes are unchecked (= request empty)

I have a form that has only two checkboxes. When both are unchecked the request is empty and so the submitted form is not treated in the controller.

Any idea, how I can post something in the request for 'unchecked' checkboxes?

MyChoiceFormType

    [...]

    public function buildForm(FormBuilderInterface $builder, array $options) {

        $builder
            ->add('voucher_buy', CheckboxType::class, [
                'label' => 'buy voucher',
                'data'  => false,               // un-checked as default
                'label_attr' => [
                    'class' => 'switch-custom'  // Bootstrap-toggle (=switch-button)
                ],
            ])
            ->add('voucher_use', CheckboxType::class, [
                'label' => 'use voucher',
                'data'  => false,               // un-checked as default
                'label_attr' => [
                    'class' => 'switch-custom'  // Bootstrap-toggle (=switch-button)
                ],
            ])
        ;
    }

    [...]

controller

    [...]

    // generate the FORM
    $form = $this->createForm(MyChoiceFormType::class);


    // handle the submitted FORM
    $form->handleRequest($request);

    if ( $form->isSubmitted() && $form->isValid() ) {
        dd($form->getData());   // <-- not getting here, when both checkboxes are unchecked

        // form shall be treated here and then
        // redirect to another page
    }

    [...]



Aucun commentaire:

Enregistrer un commentaire