Mintendo
Mintendo

Reputation: 567

Symfony 4 - Validate a form sent from Vue

How can I validate (server side) a form created and submitted from Vue?

I have a simple contact form, and I want to validate it both client and server side. For the client mode I'm using vee-validate (and it works), for the server side mode I want to use the Symfony's form validation.

So in this "special" case, the form is not rendered using the methods from Symfony, the form in this case is used only for validation. I have already created the form (it isn't linked to an Entity object), but when I send a http post request from my Vue's component, the validation on server side doesn't work. Seems that doesn't "read" the constraints created in the FormTypeclass.

My function:

public function sendContactUsEmailAction(Request $request, Mailer $mailer, TranslatorInterface $translator)
{
    try {
        $form = $this->createForm(ContactUsType::class);

        $form->submit($request->request->all());

        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            $data = $form->getData();

            $emailParams = new ContactUsParams();
            $emailParams->setName($data['name']);
            $emailParams->setEmail($data['email']);
            $emailParams->setSubject($data['subject']);
            $emailParams->setMessage($data['message']);

            $email = new ContactUsMail($mailer, $emailParams);
            $email->send();

            return new JsonResponse($translator->trans('send_contact_us_email_response'));
        }

        return new JsonResponse($this->getFirstFormError($form), 400);
    } catch (\Exception $e) {
        return new JsonResponse($e->getMessage(), 500);
    }
}

Edited

This is the dump of my request data:

array:4 [
  "name" => "Example"
  "email" => "[email protected]"
  "subject" => "Test subject"
  "message" => "Test message"
]

enter image description here

This is my FormType class. To test it I added for the name property the constraint "Email", but from the request I didn't send a valid email.

    class ContactUsType extends AbstractType
    {

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('name', TextType::class, array(
                'constraints' => array(
                    new NotBlank(),
                    new Email(),
                ),
            ))
            ->add('email', EmailType::class, array(
                'constraints' => array(
                    new NotBlank(),
                    new Email(),
                ),
            ))
            ->add('subject', TextType::class, array(
                'constraints' => new NotBlank(),
            ))
            ->add('message', TextareaType::class, array(
                'constraints' => new NotBlank(),
            ));
    }

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'csrf_protection' => false,
            'validation_groups' => false,
        ]);
    }
}

Upvotes: 3

Views: 2135

Answers (1)

Mintendo
Mintendo

Reputation: 567

I've found the solution. The problem was caused by the option "validation_groups" setted to false. I removed it and now works!

Upvotes: 1

Related Questions