我正在尝试 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]);
}
我看不出有什么用处
$request->setRequestFormat(TurboBundle::STREAM_FORMAT);
在你的代码中,Turbo 如何正常工作?
您能提供重定向中的内容吗?这样我们就可以检查可能出了什么问题