如何在模板中呈现选择字段

时间:2014-02-07 10:41:13

标签: forms templates symfony twig

我使用Symfony2和twig作为模板引擎。

在我的控制器中,我有以下形式:

      $usr= $this->get('security.context')->getToken()->getUser();      
      $associatedmissions = array($usr->getMissions());

      $form = $this->createFormBuilder($product)        
       ->add('mission', 'choice', array(
            'choices'   => $associatedmission,
            'multiple'  =>  false,
            'expanded'  => true, ))

但是当我调用该页面时,出现错误:

Catchable Fatal Error: Object of class Doctrine\ORM\PersistentCollection could not be 
converted to string in 
C:\BitNami\wampstack-5.4.23-
0\frameworks\symfony\vendor\symfony\symfony\src\Symfony\Component\Translation\
IdentityTranslator.php line 65

在探查器中,我可以看到错误:

CRITICAL - Uncaught PHP Exception Twig_Error_Runtime: "An exception has been thrown 
during the rendering of a template ("") in "form_div_layout.html.twig" at line 99." at 
C:\BitNami\wampstack-5.4.23-0\frameworks\symfony\app\cache\dev\classes.php line 4372 

Context: {"exception":"Object(Twig_Error_Runtime)"}

发生了什么事?

这很奇怪,因为它运作正常

->add('mission', 'entity')

-UPDATE -

这是我的控制器:

public function createAction(Request $request)
   {                                           
       $product = new Product();

       $usr= $this->get('security.context')->getToken()->getUser();      
       $associatedmissions = array($usr->getMissions());

          $form = $this->createFormBuilder($product)                                      
           ->add('name', 'text')
           ->add('mission', 'choice', array(
                'choices'   => $associatedmissions,
                'multiple'  =>  false,
                'expanded'  => false, ))

           ->add('save', 'submit')
           ->getForm();    


       $form->handleRequest($request);
       if ($form->isValid()) {

         $em = $this->getDoctrine()->getManager();    
         $em->persist($product);
         $em->flush();

       return $this->render('AcmeGroundStationBundle:Product:tasksuccess.html.twig', array('product' => $product));
       }    

       return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
       'form' => $form->createView()
       ));
}

2 个答案:

答案 0 :(得分:1)

a你在你的控制器中有这个:

$usr= $this->get('security.context')->getToken()->getUser();      
$associatedmissions = array($usr->getMissions());

$usr->getMissions()正在返回ArrayCollection(基于您实体中的关系)。这种方式$associatedmissions是一个包含一个元素的数组 - [1] =相关任务对象的ArrayCollection。

在我看来,您必须添加到您的用户实体方法:

public function getMissionsArray()
{
    $missionArray = array();

    foreach ($this->missions as $key => $val)
    {
        $missionArray[$key] = $val->__toString();
    }
    return $missionArray;
}

在createFormBuilder语句中使用array作为选择参数。

验证表单时,必须在保留产品之前为每个选择创建Mission对象。

$this->getDoctrine()
            ->getRepository('AcmeGroundStationBundle:Mission')
            ->find($product->mission);

修改

// User Entity
// add this function - will return array of ( id => mission.name, )
public function getMissionsArray()
{
    $missionArray = array();

    foreach ($this->missions as $key => $val)
    {
                                // change this to whatever human readable getter
        $missionArray[$key] = $val->__toString();
    }
    return $missionArray;
}

// Controller    
public function createAction(Request $request)
   {                                           
       $product = new Product();

       $usr= $this->get('security.context')->getToken()->getUser();      
       $associatedmissions = $usr->getMissionsArray();

          $form = $this->createFormBuilder($product)                                      
           ->add('name', 'text')
           ->add('mission', 'choice', array(
                'choices'   => $associatedmissions,
                'multiple'  =>  false,
                'expanded'  => false, ))

           ->add('save', 'submit')
           ->getForm();    


       $form->handleRequest($request);
       if ($form->isValid()) {

         // save selected array item as a Mission object related to Product
         $product->setMission($this->getDoctrine()
            // check if Mission Entity is placed in good bundle ;)
            ->getRepository('AcmeGroundStationBundle:Mission')
            ->find($form->get('mission')->getData()));

         $em = $this->getDoctrine()->getManager();    
         $em->persist($product);
         $em->flush();

       return $this->render('AcmeGroundStationBundle:Product:tasksuccess.html.twig', array('product' => $product));
       }    

       return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
       'form' => $form->createView()
       ));
}

祝你好运!

答案 1 :(得分:0)

尝试使用重定向。它有用吗?

   if ($form->isValid()) {

       $em = $this->getDoctrine()->getManager();    
       $em->persist($product);
       $em->flush();

       return $this->redirect($this->generatePath('current_route_name'));
   }

   return $this->render('AcmeGroundStationBundle:Product:formupload.html.twig', array(
       'form' => $form->createView()
   ));

其中current_route_name替换为您当前页面的路线名称。