评论博客创建不工作 symfony
Comment Blog creating not working symfony
你好,我试图在博客中添加评论,一切正常,没有显示任何错误,但是当我点击提交时,没有任何反应,我的意思是它没有将它添加到数据库中,我不知道我遗漏了什么
这就是我在控制器中的内容
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) }}
无论如何感谢你的帮助<3
你好,我试图在博客中添加评论,一切正常,没有显示任何错误,但是当我点击提交时,没有任何反应,我的意思是它没有将它添加到数据库中,我不知道我遗漏了什么 这就是我在控制器中的内容
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) }}
无论如何感谢你的帮助<3