2017-06-21 114 views
0

我正在嘗試使用FOSRest提交表單。到目前爲止這麼好,但我只想知道choiceType接受哪種格式?它是一個關聯數組嗎?要麼 ..?Symfony FOSRest ChoiceType

FormType

/** 
* {@inheritdoc} 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('name', TextType::class); 
    $builder->add('roles', ChoiceType::class, [ 
     'multiple' => true, 
     'expanded' => true 
    ]); 
} 

提交的數據:

{"group":{"name":"esdfgh","roles":["ROLE_VIEW_ALL_CATEGORIES","ROLE_ADD_RECEIPTS","ROLE_EDIT_RECEIPTS","ROLE_VIEW_ALL_RECEIPTS"]}} 

但是給人的錯誤: 「此值無效」 的ChoiceType。

回答

0

您需要提供有效的choices

The choices option is an array, where the array key is the item's label and the array value is the item's value

根據你的榜樣,應該是這樣的:

/** 
* {@inheritdoc} 
*/ 
public function buildForm(FormBuilderInterface $builder, array $options) 
{ 
    $builder->add('name', TextType::class); 
    $builder->add('roles', ChoiceType::class, [ 
     'choices' => [ 
     "ROLE_VIEW_ALL_CATEGORIES" => "ROLE_VIEW_ALL_CATEGORIES", 
     "ROLE_ADD_RECEIPTS" => "ROLE_ADD_RECEIPTS" 
     ] 
     'multiple' => true, 
     'expanded' => true 
    ]); 
}