Symfony 7 Dynamically Modified Forms

68 views Asked by At

i just dont get it to work :-(

I have a FormType as an example now where i have a Select Fierlt with Option1 and Option 2 Now i simple just want, that when i choose option 2 the builder add a new DateTimeType Field.

I want to do it with an AJAX Request if it is possible and i dont want to simple "hide" the fields and then when selected show up, i think thats not the way to go?

->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) {
            $form = $event->getForm();
            $data = $event->getData();

            if($data->getOption() == "2")
            $form->add('datum2', DateTimeType::class, [
            'label' => 'Datum 2',
            'mapped' => false,
        })
1

There are 1 answers

0
Simon On

If you don't want to just "hide" you'll need to use a mixture of PHP and Javascript. What you want to do is described in the "Dynamic Generation for Submitted Forms" chapter of this doc How to Dynamically Modify Forms Using Form Events