formssymfony7

Symfony: Error: Form responses must redirect to another location


In my controller I build a form and I return it to the user. Everything's fine so far.

#[Route('/quotation', name: 'app_quote')]
    public function quotation(Request $request, EntityManagerInterface $em, MailerInterface $mailer): Response
    {
        $quote = new Quote();
        $form = $this->createForm(QuoteType::class, $quote);

        $form->handleRequest($request);


        if ($form->isSubmitted() && $form->isValid()) {
            $em->persist($quote);
            $em->flush();


            return $this->render('front/quote-confirmation.html.twig', [
            ]);
        }

        return $this->render('front/quote.html.twig', [
            'form' => $form,
        ]);
    }

When I submit the form my object is well persisted in the database but the page "quote-confirmation" is not returned. Instead I have a console error in my browser "Error: Form responses must redirect to another location".

In the documentation they don't render a view after form's submition but they use a redirection. I guess it works but I can't figure it out why my solution doesn't work.

Any idea ?


Solution

  • Some browsers ads double submitted behaviour protection. Which causes this error. In particular Firefox browser.

    You can try to use redirectToRoute() function with 303 HTTP code like this :

    return $this->redirectToRoute('my_best_route', [], Response::HTTP_SEE_OTHER);