温馨提示:本文翻译自stackoverflow.com,查看原文请点击:forms - Comment Blog creating not working symfony
crud forms symfony

forms - 评论博客创建不起作用的symfony

发布于 2020-05-05 10:55:00

您好,我想在博客中添加评论,一切都很好,但没有显示错误,但是当我单击“提交”时,没有任何反应,我的意思是它没有在数据库中添加它,我不知道我在想什么,这是我在控制器中拥有的

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>

查看更多

提问者
Louay Gourrida
被浏览
30
Louay Gourrida 2020-02-17 23:25

我刚刚修复了它,我用detailsaction显示了博客,并且表单处于addcomment动作中(我使用{{form(form)}}进行了检查,但是它没有用,所以我现在不得不做所有的细节动作,现在看起来像这样)

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