我想在 symfony2 中预填表单域。网址看起来像这样
http://localhost/Symfony/web/app_dev.php/clearance/new?projectId=6
我现在想将表单中的 projectId 设置为 6。
这是我的控制器代码
public function newclearanceAction(){
$request = $this->getRequest();
$id = $request->query->get('projectId');
echo $id; //this works, but how to send it to the form?????
$clearance = new Clearance();
$form = $this->createForm(new ClearanceType(), $clearance);
if ($request->getMethod() == 'POST'){
$form->bindRequest($request);
if($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($clearance);
$em->flush();
return $this->redirect($this->generateUrl('MyReportBundle_project_list'));
}
}
return $this->render('MyReportBundle:Clearance:new.html.twig',array('form'=>$form->createView()));
这是表单视图的代码
<form action="{{ path('MyReportBundle_clearance_new') }}" method="post" >
{{ form_errors(form) }}
{{ form_rest(form) }}
<input type="submit" />
</form>
谢谢你的帮助!