2014-11-22 127 views
0

我想设置一个表单,在我选择的字段HTML的顶部设置一个“preferred_choices”,该字段对应于用户选择的先前提交的数据。我想用常量列表构造一个实体字段,如果之前提交了表单,则使用preferred_choices顶层元素。preferred_choices实体字段形式永远不会运行

我从来没有在symfony2中正确运行这个函数。 你能帮助我正确构建我的领域形式吗?

为什么我的preferred_choices选项在表单构造时没有选择任何东西? 我使用之前在代码中设置的正确对象来设置它。

public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $defaultCQSsearch = new CqsProSansMarque(); 
    $defaultCQSsearch->setRayLibelle((!array_key_exists('ray_libelle', $options['attr'])) ? null : $options['attr']['ray_libelle']); 
    $defaultCQSsearch->setFamLibelle((!array_key_exists('fam_libelle', $options['attr'])) ? null : $options['attr']['fam_libelle']); 
    $defaultCQSsearch->setCaeLibelle((!array_key_exists('cae_libelle', $options['attr'])) ? null : $options['attr']['cae_libelle']); 

    $builder 
     ->add('ray_libelle', 'entity', array(
      'class' => 'ApplicationDriveBundle:CqsProSansMarque', 
      'data_class' => 'Application\DriveBundle\Entity\CqsProSansMarque', 
      'property' => 'ray_libelle', 
      'query_builder' => function(CqsProSansMarqueRepository $er){ 
       return $er->createQueryBuilder('a') 
       ->select('a') 
       ->groupBy('a.ray_libelle'); 
      }, 
      'preferred_choices' => array($defaultCQSsearch), 
      'label' => 'rayon', 
      'required' => false, 
     )) 

回答

0

preferred_choices选项预期值的阵列,但要传递对象的阵列(即$defaultCQSsearch

+0

symfony的的doc是短。你能告诉我什么代表这个选项的值或关键值吗? – darkomen 2014-11-22 16:40:33

+0

请参阅http://symfony.com/doc/current/reference/forms/types/choice.html#preferred-choices – Prakash 2014-11-22 16:50:35

+0

Symfony的文档太短,不能正确解释preferred_choices选项。你认为这个选项尝试值的数组和文档给出的字符串值的例子,当我使用字符串值我有一个警告错误。我发布了一个问题,因为不足以阅读正确运行的文档的这一小部分。我需要了解exaclty。 – darkomen 2014-11-23 17:29:20