简体   繁体   中英

{Symfony2} Populate form with data

i have an issue with forms in Symfony2. I try to pupulate my form with prefilled data but it always stays empty.

public function updateAction($id,Request $request)
{

    $contact = $this->getDoctrine()
        ->getManager()
        ->getRepository('HPContactBundle:Contact')
        ->findById($id);

    $form = $this->createForm(new ContactType(), $contact);

    return $this->render('HPContactBundle:Contact:update.html.twig',array(
        'form' => $form->createView()
   ));
}

Errors :

The form's view data is expected to be an instance of class HP\ContactBundle\Entity\Contact, but is a(n) array. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) array to an instance of HP\ContactBundle\Entity\Contact.

AND

at Form ->setData (array(object(Contact))) in vendor/symfony/symfony/src/Symfony/Component/Form/Form.php at line 488 

i don't know what i'm missing ? Do you have any clue ?

thx in advance.

findBy*() returns an array of entities. Use findOneById() , which returns exactly one entity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM