Reputation: 126
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.
Upvotes: 1
Views: 153
Reputation: 1543
findBy*()
returns an array of entities. Use findOneById()
, which returns exactly one entity.
Upvotes: 2