我有多形式symfony(包含)的问题.
我在同一页面中有多篇文章,用户可以为每篇文章添加评论.我想在表单中添加评论.
我的问题是:当我提交我的评论添加表单时,Symfony不会将信息保存在数据库中(没有错误消息).
我试图添加一个类来更改我的表单的名称,但它是相同的.
我在控制器中的表单操作(通过每个文章的twig视图调用)
public function formAction($post_id, Request $request) {
$user = $this->getUser();
$em = $this->getDoctrine()->getManager();
$post = $em->getRepository('AppBundle:Post')->find($post_id);
$comment = new Comment();
$comment -> setPost($post);
$comment -> setAuthor($user);
$form_comment = $this->createForm(CommentType::class, $comment);
$form_comment->handleRequest($request);
if ($form_comment->isSubmitted()) {
$em = $this->getDoctrine()->getManager();
$em->persist($comment);
$em->flush();
$request->getSession()->getFlashBag()->add('msg_success', 'Votre contribution a bien été ajoutée');
}
return $this->render('account/form-comment.html.twig', array(
'form_comment' => $form_comment->createView(),
'post_id' => $post_id
));
}
Run Code Online (Sandbox Code Playgroud)
在表单类型中获取名称(对于唯一ID)
public function getName() {
return self::NAME . '_' . uniqid();
}
Run Code Online (Sandbox Code Playgroud)
感谢您的答复 !
终于,问题不在这里了。我在评论表单中使用了特定的树枝视图form-comment.html.twig(包含在索引中),但没有指定特定的路由路径。
两个响应元素:
第一:我使用{{ render(url('comment-add')) }}而不是render(controller("AppBundle:Default:method")包含树枝视图。
第二:我使用我的路线名称(评论添加)指定操作评论帖子的路径。{{ form_start(form_comment, {'attr': {'method' : 'post', 'action' : path('comment-add', {'post_id': post_id, 'groupe_id' : groupe_id})}}) }}
(很抱歉,因为我没有指定我使用了包含树枝视图的表单。)
| 归档时间: |
|
| 查看次数: |
1013 次 |
| 最近记录: |