Symfony Turbo-UX:成功后重定向表单

问题描述 投票:0回答:1

我正在尝试 Symfony UX Turbo,但提交表单时遇到问题。

如果存在验证错误,没有问题,它会在我的框架中刷新,但成功后(我想重定向到新页面),会出现重定向问题,并显示错误“内容丢失”。如果你有解决办法的话,谢谢^^

这是我的文件:

控制器

    #[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(),
        ]);
    }

查看:

    <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>

我在文档中找到了

// 🔥 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]);
            }
symfony turbo
1个回答
0
投票

我看不出有什么用处

$request->setRequestFormat(TurboBundle::STREAM_FORMAT);

在你的代码中,Turbo 如何正常工作?

您能提供重定向中的内容吗?这样我们就可以检查可能出了什么问题

© www.soinside.com 2019 - 2024. All rights reserved.