2016-07-04 124 views
1

我正在使用symfony形式工作,並在選擇中選擇我想要的選項。Symfony通過defaut選擇各種選項

我已經嘗試:

->add('idgroupe', 
     ChoiceType::class, 
     array('label' => 'Groupes', 
      'attr' => array('expanded'=> false, 
          'data'=>$g, 
          'multiple'=>'true', 
          'class' => 'form-control' 
          ), 
      'choices' => $groupes 
      ) 
    ) 

其中$ g是一個數組,如:

array(1) { [0]=> string(18) "Vue1",[1]=> string(18) "Vue2" } 

但我得到這個錯誤:

An exception has been thrown during the rendering of a template ("Notice: Array to string conversion")

+2

'expanded','multiple'和'data'不應該放在options-array的'attr'部分,而應該放在與'attr'本身相同的層上。也是'「true」!== true'(刪除引號)。 – Yoshi

回答

0

嘗試用:

->add('idgroupe', 
     ChoiceType::class, 
     array('label' => 'Groupes', 
      'attr' => array('class' => 'form-control' 
          ), 
      'expanded'=> false, 
      'data'=>$g, 
      'multiple'=>true, 
      'choices' => $groupes 
      ) 
    ); 
+3

如果您使用Symfony> = 2.7,請設置選項'choices_as_values'=> true。 http://symfony.com/blog/new-in-symfony-2-7-choice-form-type-refactorization – Elyass

+0

我在表格中犯了一個錯誤,但是您的解決方案有效。謝謝 – kimpa2007

+0

嗨@ kimpa2007隨時接受我的答案,如果你看起來不錯,謝謝 – Matteo