如何创建编辑/更新表单

时间:2013-01-11 09:17:05

标签: forms entity-framework symfony symfony-2.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创建表单,因为我想知道它是如何做的。

1 个答案:

答案 0 :(得分: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()));
}