Search code examples
formssymfonyvalidationsymfony4

Symfony Dependance required field's form


I have a merged form type which contains multiple form types.

The required fields from the first form (callType) are mandatory, and I want that if one of the required field is filled the others have to be fill.

<?php
class MergedFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('callForm', CallType::class, $call);
        $builder->add('emailForm', EmailSendType::class);
        $builder->add('reminderForm', RappelType::class);
    }
}


<?php
class EmailSendType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
        ->add('email', EmailType::class, [
            'label' => 'form.email',
        ])
        ->add('object', TextType::class, [
            'label' => 'form.object',
        ])
        ->add('document', FileType::class, [
            'label' => 'form.document',
            'required' => false,
        ])
        ->add('content', TextareaType::class, [
            'label' => 'form.content',
        ]);
    }
}

I want to use the auto validation from symfony, I do not check in the Controller file.

Is there a way to achieve that ?


Solution

  • you can simply do a form validation check, but if you're talking about dynamic validation, you might need to do this with JavaScript.

    if ($form->isSubmitted() && $form->isValid()) { ... }
    

    If the fields are not verified statically, the form will not be sent anymore.