我尝试在Symfony中编辑表单
public function editAction()
{
$id = 5;
$em = $this->getDoctrine()->getEntityManager();
$request = $this->get('request');
$entity = $em->getRepository('SurgeryPatientBundle:Patients')->find($id);
$form = $this->createForm(new SurgeryPatientBundleFormPatientsType(), $entity);
$form->bindRequest($request);
return $this->render('SurgeryPatientBundle:Patient:newpatient.html.twig',array('form'=>$form->createView()));
if($form->isValid())
{
//do someting
}
else
{
//do something
}
}
我仍然得到没有数据的空表单,变量$entity
是isset。我确实插入了表单,但我无法处理编辑动作。我不想用终端和php app/console
创建表单因为我想知道怎么做
假设您正在使用symfony 2.0。x :
你不应该在发送数据给视图之前绑定你的请求,也不应该在测试提交的数据之前渲染视图(我猜你给我们看测试代码)
public function editAction()
{
$id = 5;
$em = $this->getDoctrine()->getEntityManager();
$request = $this->get('request');
$entity = $em->getRepository('SurgeryPatientBundle:Patients')->find($id);
$form = $this->createForm(new SurgeryPatientBundleFormPatientsType(), $entity);
if ($request->getMethod() == 'POST') {
$form->bindRequest($request);
if ($form->isValid()) {
return $this->redirect($this->generateUrl('task_success'));
}
}
return $this->render('SurgeryPatientBundle:Patient:newpatient.html.twig',array('form'=>$form->createView()));
}