表单数据不断添加与页面刷新| Symfony



每次刷新页面后,通过表单提交的数据不断添加到数据库中,我不知道为什么。我已经检查了Post/Redirect/Get模式(https://en.wikipedia.org/wiki/Post/Redirect/Get),但无法找出如何将其应用于我的情况。因为在我的AddComment()函数中,我使用if语句来检查表单是否提交,然后进行重定向。

/**
* @Route("/comments", name="app_comments")
*/
public function indexAction(Request $request, EntityManagerInterface $entityManager): Response
{
$allComments = $this->getComments($entityManager);
$sortedComments = $this->sortComments($allComments);
$user = $this->getUser();
$comment = new Comment();
$commentForm = $this->commentForm($request, $comment);
$this->addComment($commentForm, $comment, $user, $entityManager, $_POST['parent_id'] ?? null);

$isCommentAlreadyLiked =
$entityManager->getRepository(CommentLike::class)->countByCommentAndUser($comment, $user);
return $this->renderForm('comments/index.html.twig',[
'form' => $commentForm,
'sortedComments' => $sortedComments,
'isCommentAlreadyLiked' => $isCommentAlreadyLiked
]);
}
private function getComments(EntityManagerInterface $entityManager): array
{
return $allComments = $entityManager->getRepository(Comment::class)->findAll();
}
public function addComment($form, $comment, $user, EntityManagerInterface $entityManager, ?int $parentId = null)
{
if ($form->isSubmitted() && $form->isValid()) {
$comment->setUser($user);
$comment->setCreatedAt(new DateTime());
$comment->setParentId($_POST['parent_id'] ?? null);
$comment = $form->getData();
$entityManager->persist($comment);
$entityManager->flush();
return $this->redirectToRoute('app_comments');
}
return null;
}
public function commentForm(Request $request, $comment): FormInterface
{
$form = $this->createForm(CommentType::class, $comment);
$form->handleRequest($request);
return $form;
}

在Post/redirect/Get模式(https://en.wikipedia.org/wiki/Post/Redirect/Get)中指定的数据添加到DB后,我使用重定向,但它仍然不起作用

你的问题是,在addComment你返回一个调用重定向(),但返回从addComment函数,一旦你得到索引函数你不返回此响应,所以这段代码跳过,基本上你得到的文件的末尾,你调用renderForm()函数。

这会导致在没有实际重定向的情况下再次呈现视图,因此表单对象现在已经填充,当你按下F5时,每次都会触发表单的POST提交。

我通常使用相同的函数来完成表单的所有逻辑和所有内容,但如果您的代码很大,并且更喜欢将其拼接在函数中,它将接近如下:

indexAction

/**
* @Route("/comments", name="app_comments")
*/
public function indexAction(Request $request, EntityManagerInterface $entityManager): Response
{
$allComments = $this->getComments($entityManager);
$sortedComments = $this->sortComments($allComments);
$user = $this->getUser();
$comment = new Comment();
$commentForm = $this->commentForm($request, $comment);
if ($form->isSubmitted() && $form->isValid()) {
$this->addComment($commentForm, $comment, $user, $entityManager, $_POST['parent_id'] ?? null);
return $this->redirectToRoute('app_comments');
}

$isCommentAlreadyLiked =
$entityManager->getRepository(CommentLike::class)->countByCommentAndUser($comment, $user);
return $this->renderForm('comments/index.html.twig',[
'form' => $commentForm,
'sortedComments' => $sortedComments,
'isCommentAlreadyLiked' => $isCommentAlreadyLiked
]);
}

addComment

public function addComment($form, $comment, $user, EntityManagerInterface $entityManager, ?int $parentId = null)
{
$comment->setUser($user);
$comment->setCreatedAt(new DateTime());
$comment->setParentId($_POST['parent_id'] ?? null);
$comment = $form->getData();
$entityManager->persist($comment);
$entityManager->flush();
}

这样,您就不需要继续根据条件返回重定向或空值,将逻辑保留在主函数上,并且addComment纯粹管理注释。

您的表单是通过POST还是GET方法提交的?

您还可以将$form->handleRequest($request);封装在commentForm中,以检查请求方法是否为POST$request->isMethod(Request::METHOD_GET)

public function commentForm(Request $request, $comment): FormInterface
{
$form = $this->createForm(CommentType::class, $comment);
if ($request->isMethod(Request::METHOD_GET)){
$form->handleRequest($request);
}
return $form;
}

相关内容

  • 没有找到相关文章

最新更新