Witam,
mam problem z umieszczeniem formularza z odpowiedziami na komentarze w twig'owej pętli for.
Zamysł był taki: Jak istnieje komentarz to pod komentarzem tworzy się formularz do odpowiedzi wszystko fajnie tylko, że pętla tylko raz tworzy mi formularz do pierwszego komentarz, a do następnych już nie.
Bardzo proszę o podpowiedź jak można rozwiać ten problem. Niżej dodaje kod z dotyczącym problemem.
/**
* @Route("/show/{slug}", name="blog_show")
* @param Request $request
* @param Post $post
* @param Comment $comment
* @return Response
*/
public function showAction(Request $request, Post $post, Comment $comment)
{
$newComment = new Comment();
$newComment->setPost($post);
$commentForm = $this->createForm(CommentType::class, $newComment);
$commentForm->handleRequest($request);
if ($commentForm->isSubmitted() && $commentForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($newComment);
$entityManager->flush();
return $this->redirectToRoute('blog_show', [
'slug' => $post->getSlug(),
]);
}
$newReplyComment = new ReplyComment();
$newReplyComment->setRComment($comment);
$replyCommentForm = $this->createForm(ReplyCommentType::class,$newReplyComment);
$replyCommentForm->handleRequest($request);
if ($replyCommentForm->isSubmitted() && $replyCommentForm->isValid()) {
$entityManager = $this->getDoctrine()->getManager();
$entityManager->persist($newReplyComment);
$entityManager->flush();
return $this->redirectToRoute('blog_show', [
'slug' => $post->getSlug(),
]);
}
return $this->render('blog/show.html.twig',[
'post' => $post,
'comment' => $comment,
'commentForm' => $commentForm->createView(),
'replyCommentForm' => $replyCommentForm->createView(),
]);
}
<div>
<h3>Komentarze:</h3>
{% for comment in post.comment %}
<h4>{{ comment.user }} || {{ comment.createdAt|date("d F Y | H:i:s") }}</h4>
<p>{{ comment.comment }}</p>
<div style="margin-left: 3rem;">
{% for replyComment in comment.replyComment %}
<h4>{{ replyComment.user }} || {{ replyComment.createdAt|date("d F Y | H:i:s") }}</h4>
<p>{{ replyComment.comment }}</p>
{% endfor %}
</div>
<hr>
<div style="margin-left: 3rem;">
<h5>odpowiedz</h5>
{{ form(replyCommentForm) }}
</div>
<hr>
{% endfor %}
</div>
class ReplyCommentType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('user', TextType::class)
->add('comment', TextType::class)
->add('submit', SubmitType::class)
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => ReplyComment::class,
]);
}
}