Php 表格';行不通
我正在尝试自定义使用formBuilder创建的表单 当我用Php 表格';行不通,php,forms,symfony,twig,Php,Forms,Symfony,Twig,我正在尝试自定义使用formBuilder创建的表单 当我用{{form(comment)}}显示我的表单时,它工作得很好,但用{{form_widget(comment.MYMETHOD)}} 我的表单如下所示: <form class="commentForm"> <ul> <li class="commentFormArea">{{ form_widget(comment.commentaires) }} <
{{form(comment)}}
显示我的表单时,它工作得很好,但用{{form_widget(comment.MYMETHOD)}}
我的表单如下所示:
<form class="commentForm">
<ul>
<li class="commentFormArea">{{ form_widget(comment.commentaires) }}
</li>
<li class="commentFormsubmit">{{ form_widget(comment.save) }}</li>
</ul>
</form>
- {{form_小部件(comment.commentaries)}
- {{form_小部件(comment.save)}
我的控制器
public function postsAction(Request $request)
{
$repository = $this
->getDoctrine()
->getManager()
->getRepository('NastycodeFrontBundle:Publication')
;
$posts = $repository->findBy(array(), array(), 10);
$commentaires = new Commentaires();
$comment = $this->get('form.factory')->createBuilder('form', $commentaires)
->add('commentaires', 'textarea')
->add('save', 'submit')
->getForm()
;
// On fait le lien Requête <-> Formulaire
// À partir de maintenant, la variable $commentaires contient les valeurs entrées dans le formulaire par le visiteur
$comment->handleRequest($request);
// On vérifie que les valeurs entrées sont correctes
// (Nous verrons la validation des objets en détail dans le prochain chapitre)
if ($comment->isValid()) {
// On l'enregistre notre objet $commentaires dans la base de données, par exemple
$em = $this->getDoctrine()->getManager();
$em->persist($commentaires);
$em->flush();
$request->getSession()->getFlashBag()->add('notice', 'Annonce bien enregistrée.');
// On redirige vers la page de visualisation de l'annonce nouvellement créée
return $this->redirect($this->generateUrl('nastycode_comment_code', array('id' => $commentaires->getId())));
}
$user = $this->getUser();
return $this->render('NastycodeFrontBundle:Posts:posts.html.twig', array(
'user' => $user,
'posts' => $posts,
'comment' => $comment->createView(),
));
}
公共功能后处理(请求$Request)
{
$repository=$this
->getDoctrine()
->getManager()
->getRepository('NastycodeFrontBundle:Publication')
;
$posts=$repository->findBy(array(),array(),10);
$commentaries=新评论();
$comment=$this->get('form.factory')->createBuilder('form',$commentaries)
->添加('评论','文本区域')
->添加('保存','提交')
->getForm()
;
//论不动产需求公式
//第二部分维护者,La变量$评论员
$comment->handleRequest($request);
//关于vérifie que les valeurs entrées sont纠正的问题
//(这是一个验证过程)
如果($comment->isValid()){
//在L'EnReistNo.Objut.$DunsDaDeNeNeS;
$em=$this->getDoctrine()->getManager();
$em->persist($commentals);
$em->flush();
$request->getSession()->getFlashBag()->add('notice','Annonce bien enregistre');
//关于新生事物视觉化的重新定义
返回$this->redirect($this->generateUrl('nastycode_comment_code',array('id'=>$commentaries->getId()));
}
$user=$this->getUser();
返回$this->render('NastycodeFrontBundle:Posts:Posts.html.twig',数组(
“用户”=>$user,
“posts”=>$posts,
'comment'=>$comment->createView(),
));
}
当我提交表单时,它会生成此url
/web/app_dev.php/nastycodes?表单[注释]=MYCOMMENT和表单[保存]=
而不是这个
/web/app_dev.php/nastycodes?id=1
当我没有显示所有表单时,我不知道为什么我的提交不起作用
你们知道问题出在哪里吗?我怎么解决
感谢尝试将method=“post”
添加到
标记中,表单的默认方法是GET
当您使用
{{form(comment)}}
时,它会在模板中生成表单的所有字段。尝试在表单结束标记之前添加form\u rest(comment)
,以确保没有遗漏任何内容。不确定您使用的是哪个版本的Symfony2
,但由于v2.3
有表单开始()
和表单结束()
Twig
可用于打印表单打开和表单关闭标记的函数,包括其属性
但是,您需要这样做:
$comment = $this->get('form.factory')->createBuilder('form', $commentaires)
->setMethod("POST") // <--- THIS
->add('commentaires', 'textarea')
->add('save', 'submit')
->getForm()
;
$comment=$this->get('form.factory')->createBuilder('form',$commentaries)
->setMethod(“POST”)//添加('commentaries','textarea')
->添加('保存','提交')
->getForm()
;
然后:
{{ form_start(comment) }}
<ul>
<li class="commentFormArea">{{ form_widget(comment.commentaires) }}
</li>
<li class="commentFormsubmit">{{ form_widget(comment.save) }}</li>
</ul>
{{ form_end(comment) }}
{{form_start(comment)}
- {{form_小部件(comment.commentaries)}
- {{form_小部件(comment.save)}
{{格式(注释)}
默认情况下,
form\u end
自动调用form\u rest(form)
…我正在使用Symfony 2.6.4,我已经测试了您的方法,这项工作也很好,谢谢。