Symfony 3 callback validation not being called

Viewed 733

I have a form in Symfony 3 where I want the user to upload a file or fill in a comment (or both).

$form = $this->createFormBuilder(
        $approvalRequest,
        array(
            'validation_groups' => array('revocation_proposal')
        )
    )
        ->add(
                'deletionComment',
                TextareaType::class,
                array(
                    'attr' => array(
                        'cols' => 70,
                        'rows' => 10
                    ),
                    'required' => false //otherwise html will force it to be required
                )
            )
        ->add('deletionTemplate',
               ResearchTemplateType::class,
                array('label' => 'Deletion Form',
                    'required' => false)) //otherwise html will force it to be required

        ->add(
            'cancel',
            SubmitType::class,
            array(
                'label' => 'Cancel'
            )
        )
        ->add('revoke', SubmitType::class, array(
            'label' => 'Revoke Approval',
            'attr' => array('class' => 'btn-danger')
        ))

        ->getForm();
    $form->handleRequest($request);

I am trying to enforce that at least one of the two (the comment or the file) must be present in order for the form to be valid using callbacks.

In my entity I have:

/**
 * @Assert\Callback
 */
public function validate(ExecutionContextInterface $context)
{
    if (is_null($this->getDeletionComment()) && is_null($this->getDeletionTemplate())) {
        $context->buildViolation('You must write a deletion comment or upload a deletion template')
            ->atPath('deletionTemplate')
            ->addViolation();
    }
}

But it is not being called. Am I missing something?

Thanks

0 Answers
Related