Forms 评论博客创建不工作symfony
你好,我试着在博客中添加评论,一切正常,没有显示错误,但当我点击提交时,什么都没有发生,我的意思是它没有添加到数据库中,我不知道我遗漏了什么 这是我在控制器里的东西Forms 评论博客创建不工作symfony,forms,symfony,crud,Forms,Symfony,Crud,你好,我试着在博客中添加评论,一切正常,没有显示错误,但当我点击提交时,什么都没有发生,我的意思是它没有添加到数据库中,我不知道我遗漏了什么 这是我在控制器里的东西 public function addCommentAction(Request $request, $id) { $user=$this->getUser(); if($user==null) return $this->redirectToRoute('
public function addCommentAction(Request $request, $id)
{
$user=$this->getUser();
if($user==null)
return $this->redirectToRoute('fos_user_security_login');
$add_comment = new CommentaireBlog();
$em = $this->getDoctrine()->getManager();
$blog = $em->getRepository(Blog::class)->find($id);
$add_comment->setBlog($blog);
$add_comment->setUser($user);
$add_comment->setDate( new \DateTime());
$form = $this->createFormBuilder($add_comment)
->add('contenu', TextareaType::class)
->getForm();
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$add_comment = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($add_comment);
$em->flush();
return $this->redirect($this->generateUrl('blog_details'));
}
}
return $this->render('blog/details.html.twig', array(
'form' => $form->createView(),
'comment' => $add_comment,
'blog' => $blog,
));
}
这就是我在blog.yml中看到的
comment_new:
path: /{id}/details
defaults: { _controller: "BlogBundle:Blog:addComment" }
methods: [GET, POST]
最后这是细枝页
<div class="comments-form">
<h4 class="comments-title">Leave A Reply</h4>
<!-- .row -->
<form action="{{ path('comment_new', { 'id': blog.id }) }}" method="post" >
<textarea id="form_comment" name="form[comment]" required="required" class="form-control comments-textarea" placeholder="Comments*"></textarea>
<input type="submit" class="btn btn-default" />
</form>
</div>
</div>
留话
可能有输入错误:
->add('contenu', TextareaType::class) // Should be 'comment'
以您使用的形式:
<textarea id="form_comment" name="form[comment]" required="required" class="form-control comments-textarea" placeholder="Comments*"></textarea>
似乎存在映射错误,您可以通过在模板中呈现
{{form(form)}
进行检查我刚刚修复了它,我用detailsaction显示博客,表单处于addcomment操作(我使用{form(form)}进行了检查,但它不起作用,所以我必须在details操作中完成所有工作,现在看起来是这样的
public function detailsAction(Request $request,Blog $blog){
$user=$this->getUser();
if($user==null)
return $this->redirectToRoute('fos_user_security_login');
$add_comment = new CommentaireBlog();
$em = $this->getDoctrine()->getManager();
$add_comment->setBlog($blog);
$add_comment->setUser($user);
$add_comment->setDate( new \DateTime());
$form = $this->createFormBuilder($add_comment)
->add('contenu', TextareaType::class)
->add('ajouter',SubmitType::class)
->getForm();
if ($request->getMethod() == 'POST') {
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$add_comment = $form->getData();
$em = $this->getDoctrine()->getEntityManager();
$em->persist($add_comment);
$em->flush();
}
}
return $this->render('blog/details.html.twig', array(
'form' => $form->createView(),
'comment' => $add_comment,
'blog' => $blog,
));
}
树枝看起来像这样:
{{ form_start(form) }}
<div class="row form-group">
<div class="col col-md-3"><label class=" form-control-label">Votre Commentaire </label></div>
<div class="col-12 col-md-9">{{ form_widget(form.description) }}<small class="form-text text-muted"></small></div>
<div class="col-12 col-md-9">
</div>
</div>
{{ form_end(form) }}
{{form_start(form)}
Votre评论
{{form_小部件(form.description)}
{{form_end(form)}}
无论如何,感谢您的帮助,它说属性“comment”和方法“getComment()”、“comment()”、“isComment()”、“hasComment()”、“\uu get()”都不存在,并且在类“BlogBundle\Entity\CommentaireBlog”中具有公共访问权限。然后我想我应该将形式[comment]改为形式[contentnu]?对吗?是的,正如预期的那样,存在映射错误。将表单[comment]更改为从[contenu]将数据设置为注释对象中的右字段。我在模板中没有呈现表单时遇到问题,因为我正在使用detailsaction显示表单,并且表单处于addcomment操作中。因此,我必须在detailsaction中完成所有工作,现在它工作正常,很高兴它工作正常。如果有人有更好的解释,我不知道为什么会被否决更好的帖子!除此之外,OP是新来者,欢迎所有编辑。继续学习!我不知道谁投了票-1.我的意思是,是的,如果有人有想法,就应该把它贴出来,不要对别人的答案投否定票。无论如何,谢谢你的时间,朋友。博客与评论有关系吗?如果是的话,你需要一个$blog->addC注释($comment)行。