评论博客创建不工作symfony

dhxwm5r4  于 2023-01-17  发布在  其他
关注(0)|答案(2)|浏览(118)

你好,我试图在博客中添加评论一切都很好,它显示没有错误,但当我点击提交什么都没有发生,我的意思是它不添加到数据库中,我不知道我错过了什么,这是我在控制器

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>
qyswt5oh

qyswt5oh1#

我刚刚修复了它,我用detailsaction显示博客,而表单处于addcomment操作(我使用{{form(form)}}检查,它不起作用,所以我必须在detailsaction中完成所有工作,现在它看起来像这样

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

anauzrmj

anauzrmj2#

可能存在打印错误:

->add('contenu', TextareaType::class) // Should be 'comment'

在您使用的形式中:

<textarea id="form_comment" name="form[comment]" required="required" class="form-control comments-textarea" placeholder="Comments*"></textarea>

似乎有Map错误,您可以通过在模板中渲染{{ form(form) }}来检查

相关问题