0

我尝试在 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 \Surgery\PatientBundle\Form\PatientsType(), $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因为我想知道怎么做。

4

1 回答 1

0

假设您使用的是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 \Surgery\PatientBundle\Form\PatientsType(), $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()));
}
于 2013-01-11T09:53:15.307 回答