I have this form type
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('field1', TextType::class)
->add('field2', TextType::class, [
'required' => true
])
;
}
I would like to change the field2 required attribute to false, depending on field1. What I am trying to do:
$builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
$data = $event->getData();
$form = $event->getForm();
if(array_key_exists('field1', $data) && $data['field1'] === 'some value') {
//here I need something to set field2 required option to false
}
});
in this way if I type "some value" in field1 and I submit the form, field2 is no longer mandatory.
Is it possible to achieve that?
Also, is it PRE_SUMBIT the correct event to use for this purpose?