Question

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.

Était-ce utile?

La solution

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()));
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top