我正在開發一個使用Symfony 2.7的博客。我有一個ArticleBundle和一個CommentBundle。我的實體文章和評論通過ManyToOne關係鏈接。Symfony2.7表格無效
當我嘗試發送評論時,它失敗。 $ 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
));
}
}
我的評論形式inclued到文章的觀點是這樣的:
{{ 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) }}
謝謝,祝你有個愉快的一天!
也許你忘了添加動作到窗體如果您在不同網址上呈現此表單,則必須設置該值。 – malcolm
我之前嘗試過,但它沒有奏效。 –
@ L.Baptiste在安裝了DebuggerBundle的情況下添加'var_dump($ form-> getErrors(true))'('dump($ form-> getErrors(true))''後跟'die();''' $ form-> handleRequest($ request);' – xurshid29