Symfony Turbo-UX : redirect form after success

209 Views Asked by At

I'm trying Symfony UX Turbo, but I've a problem when my form is submited.

No problem if there are validation errors, it refreshes in my frame, but on success (I want to redirect to a new page), there is a redirection problem with the error "Content missing". If you have a solution, thx ^^

Here are my files :

Controller

    #[Route('/register', name: 'security_register')]
    public function register(
        Request $request,
        TranslatorInterface $translator,
        UserPasswordHasherInterface $userPasswordHasher,
        EntityManagerInterface $entityManager, 
    ): Response
    {
        if ($this->isGranted('ROLE_USER'))
            return $this->redirectToRoute('security_login_check');

        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

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

            $user->setPassword(
                $userPasswordHasher->hashPassword(
                    $user,
                    $form->get('password')->getData()
                )
            );
            $entityManager->persist($user);
            $entityManager->flush();

            try {
                $this->emailVerifier->sendEmailConfirmation('security_verify_email', $user,
                    (new TemplatedEmail())
                        ->from(new Address($this->getParameter('param_mailBot'), $this->getParameter('param_mailBotName')))
                        ->to($user->getEmail())
                        ->subject('Confirmation ' . $this->getParameter('param_SiteNom'))
                        ->htmlTemplate('mail/registrationConfirm.html.twig')
                );
            } catch (VerifyEmailExceptionInterface $exception) {
                $this->addFlash('danger', $translator->trans($exception->getReason()));
                return $this->redirectToRoute('security_register');
            }

            $this->addFlash('success', 'Un email de confirmation vous a été envoyé.');
            return $this->redirectToRoute('security_login');
        }

        return $this->render('front_public/security/register.html.twig', [
            'registrationForm' => $form->createView(),
        ]);
    }

View :

    <turbo-frame id="the_frame_id">
        {{ form_start(registrationForm, {attr: {
            'novalidate':'novalidate',
            'id': 'myForm',
        }}) }}
        {{ form_row(registrationForm.email) }}
        {{ form_row(registrationForm.password) }}
        {{ form_row(registrationForm.mentionsCgu) }}
        <div class="form-footer">
            <button type="submit" class="btn btn-red w-100">S'enregistrer</button>
        </div>
        {{ form_end(registrationForm) }}
    </turbo-frame>

I've found that in the doc

//  The magic happens here! 
            if (TurboBundle::STREAM_FORMAT === $request->getPreferredFormat()) {
                // If the request comes from Turbo, set the content type as text/vnd.turbo-stream.html and only send the HTML to update
                $request->setRequestFormat(TurboBundle::STREAM_FORMAT);
                return $this->render('task/success.stream.html.twig', ['task' => $task]);
            }
0

There are 0 best solutions below