0

我正在使用 Symfony 2.7 开发一个博客。我有一个 ArticleBundle 和一个 CommentBundle。我的实体文章和评论通过多对一关系链接。

当我尝试发送评论时,它失败了。$form->isValid() 方法返回 false。

在这里,我的 add 方法发送评论:

public function addAction(Article $article, Request $request){
    $comment = new Comment();

    $form = $this->createForm(new CommentType(), $comment);
    $form->handleRequest($request);

    if($form->isValid()){
        $comment->setArticle($article);

        $em = $this->getDoctrine()->getManager();
        $em->persist($comment);
        $em->flush();

        return $this->redirect($this->generateUrl('esgi_article_view', array(
            'id' => $article->getId()
        )));
    } else{
        return $this->render('ESGICommentBundle:Comment:add.html.twig', array(
            'form' => $form->createView(),
            'article' => $article
        ));
    }
}

我的评论表包含在文章的视图中,如下所示:

{{ render(controller("ESGICommentBundle:Comment:add", { 'id' : article.id })) }}

这是我的表格:

{{ form_start(form) }}
<div class="form-group">
    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-8">
            {{ form_label(form.author, "Auteur") }}
            {{ form_widget(form.author, { 'attr' : { 'class' : 'form-control' } }) }}
            {{ form_errors(form.author) }}
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-8">
            {{ form_label(form.email, "Email (optionnel)") }}
            {{ form_widget(form.email, { 'attr' : { 'class' : 'form-control' } }) }}
            {{ form_errors(form.email) }}
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12 col-sm-6 col-md-8">
            {{ form_label(form.content, "Commentaire") }}
            {{ form_widget(form.content, { 'attr' : { 'class' : 'form-control' } }) }}
            {{ form_errors(form.content) }}
        </div>
    </div>
</div>

<button class="btn btn-success" type="submit">Poster</button>
{{ form_end(form) }} 

谢谢,祝你有美好的一天!

4

1 回答 1

0

如果您提交表单,则分析器栏中应该有所有错误(有一个表单图标,您可以单击以查看提交表单中的错误)

于 2015-09-11T08:48:18.323 回答