1

I have a DateType field on a form that is set to disabled=true . When a specific checkbox is checked by the user some jquery picks it up and removes the disabled property from the html but the value still doesn't get submitted by the form.

Just wondering if there is a better way to accomplish this in symfony? The date field needs to be disabled for the user unless they check the checkbox. The field is added like this:

$builder->add('overrideDate',DateType::class,[
            'required'=>false,
            'label'=>'Override Date',
            'disabled'=>true

        ]);

I did try changing it via a SUBMIT event so that symfony would recognise the field as enabled since the html doesn't have the disabled anymore the field should still be getting submitted so i'm assuming that it's symfony ignoring it because of the original disabled =true ?

Here is part of the builder, i am trying to change the date field when it's submitted to disabled = false but the overrideDate is still null

$builder->add('overrideDates',CheckboxType::class,[
            'label'=>'Override Dates',
            'required'=>false
        ]);

        $builder->add('overrideDate',DateType::class,[
            'required'=>false,
            'label'=>'Override Date',
            'disabled'=>true

        ]);


        $builder->addEventListener(FormEvents::SUBMIT,function(FormEvent $event){

            $form = $event->getForm();
            $data = $form->getData();


            if($data->isOverridingDates()){
                 $form->add('overrideDate',DateType::class,[
                     'required'=>false,
                     'label'=>'Override Date',
                     'disabled'=>false

                 ]);

            }
        });

SOLUTION

Ok this feels a bit clunky, but it works for what i want to do:

// event listener to enable/disable the overrideDate field if overrideDates === true
        $builder->addEventListener(FormEvents::PRE_SUBMIT,function(FormEvent $event){

            // get form and data
            $form = $event->getForm();
            $data = $event->getData();


            // add the overridedDate field back into the form
            $form->add('overrideDate',DateType::class,[
                'required'=>false,
                'label'=>'Override Date',
                'disabled'=>((bool)$data['overrideDates'] === true ? false : true)

            ]);

        });
Glen Elkins
  • 867
  • 9
  • 30

2 Answers2

1

You should take a look here : https://symfony.com/doc/current/form/dynamic_form_modification.html

It's the good way to do what you wan to do ! You should use FormEvents::PRE_SUBMIT instead of FormEvents::SUBMIT

// event listener to enable/disable the overrideDate field if overrideDates === true
$builder->addEventListener(FormEvents::PRE_SUBMIT,function(FormEvent $event){
    // get form and data
    $form = $event->getForm();
    $data = $event->getData();

    // add the overridedDate field back into the form
    $form->add('overrideDate',DateType::class,[
            'required'=>false,
            'label'=>'Override Date',
            'disabled'=>((bool)$data['overrideDates'] === true ? false : true)
    ]);
});
Nicolas
  • 167
  • 8
  • Hey, that's what i have done, using FormEvents::SUBMIT and re-added the field with disabled=false but it's still submitting NULL in the DateType field. I have updated my question to show the code. It does make the field enabled but it goes through the whole validation and re-displays the form with the modified field instead of allowing the submitted data through – Glen Elkins Mar 29 '19 at 13:23
  • What happens if you use FormEvents::PRE_SUBMIT ? – Nicolas Mar 29 '19 at 13:34
  • I've used that, updated my question with the solution. The only thing i don't like about it is the $data at this point isn't an entity object, it's an array, so the checkbox is '1' and not a boolean like the entity has it - so i had to type cast it. Marking this as correct answer. – Glen Elkins Mar 29 '19 at 13:36
  • @Nicolas please edit your answer and add more information to it. Links will get broken over time. – Lajos Arpad Mar 29 '19 at 14:31
0

I have a simpler solution, you have to use the HTML attribute "readOnly". This attribute enables the same function as "disabled" but when symfony get your form data, it takes the "readonly" input into account. You can add your input in your form builder like this :

$builder->add('overrideDate',DateType::class,[
        'required'=>false,
        'label'=>'Override Date',
        'attr' => ['readonly' => true],
    ]);
tCot
  • 307
  • 2
  • 7