【问题标题】:Comment Blog creating not working symfony评论博客创建不工作的 symfony
【发布时间】:2020-06-01 08:50:34
【问题描述】:

你好我试图在博客中添加评论一切都很好它没有显示错误但是当我点击提交时没有任何反应我的意思是它没有将它添加到数据库中我不知道我缺少什么 这就是我在控制器中所拥有的

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>

【问题讨论】:

  • 博客和cmets有关系吗?如果是这样,沿着这条线的某个地方你将需要一个 $blog->addComment($comment) 线。

标签: forms symfony crud


【解决方案1】:

我刚刚修复了它,我用 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) }}

无论如何感谢您的帮助

【讨论】:

    【解决方案2】:

    可能有错别字:

    ->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) }}来检查

    【讨论】:

    • 它说既不是属性“comment”,也不是方法之一“getComment()”、“comment()”、“isComment()”、“hasComment()”、“__get()”在“BlogBu​​ndle\Entity\CommentaireBlog”类中存在并具有公共访问权限。那么我想我应该将 form[comment] 更改为 from[contenu] 吗?对吗?
    • 是的,正如预期的那样,存在映射错误。将 form[comment] 更改为 from[contenu] 会将数据设置到评论对象中的右侧字段。
    • 我遇到了无法在模板中呈现表单的问题,因为我使用 detailsaction 来显示并且表单处于添加注释操作中......所以我必须在 detailsaction 中完成所有工作,现在它工作正常
    • 很高兴它成功了。我不知道为什么它被否决,如果有人有更好的解释,最好贴出来!此外 OP 是一个新人,欢迎所有的编辑。继续学习!
    • 我不知道谁投了-1 ..我的意思是,如果有人有想法应该发布它,而不是对其他答案投反对票,谢谢你的时间伙伴
    猜你喜欢
    • 1970-01-01
    • 2021-04-16
    • 1970-01-01
    • 2016-07-26
    • 1970-01-01
    • 1970-01-01
    • 2013-04-16
    • 1970-01-01
    • 2013-11-21
    相关资源
    最近更新 更多