문제

I try make edit form in 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
  }
} 

I still get empty form without data, variable $entity is isset. I did insert form but I can't handle with edit action. I don't want to create forms using terminal and php app/console because I want to know how do it.

도움이 되었습니까?

해결책

Assuming you are using symfony 2.0.x :

You should not bind your request before sending data to the view, neither render the view before testing for submitted data (I guess you show us test code)

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()));
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top