Hello i was trying to add comment in blog everything is fine it shows no error but when i click submit nothing happens i mean it doesn't add it in data base i dont know what im missing this is what i have in controller
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,
));
}
this is what i have in blog.yml
comment_new:
path: /{id}/details
defaults: { _controller: "BlogBundle:Blog:addComment" }
methods: [GET, POST]
and finally this is the twig page
<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>
i just fixed it i displaying the blog with detailsaction and the form is in addcomment action (i checked with using{{form(form)}} and it didnt work so i had to do all the work in details action now it looks like this
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,
));
}
and the twig looks like this :
{{ 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) }}
anyway thanks for for your help <3