1

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.

1
  • What happens in Form.php at line 488? Commented Aug 27, 2015 at 18:46

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.