2014-11-22 22 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