简体   繁体   English

{Symfony2}用数据填充表单

[英]{Symfony2} Populate form with data

i have an issue with forms in Symfony2. 我在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. findBy*()返回一个实体数组。 Use findOneById() , which returns exactly one entity. 使用findOneById() ,它仅返回一个实体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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