簡體   English   中英

{Symfony2}用數據填充表單

[英]{Symfony2} Populate form with data

我在Symfony2中遇到表格問題。 我嘗試用預填充的數據填充表單,但它始終保持空白。

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()
   ));
}

錯誤:

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.

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

我不知道我在想什么? 你有什么線索嗎?

提前。

findBy*()返回一個實體數組。 使用findOneById() ,它僅返回一個實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM