2017-06-21 91 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 
    ]); 
} 
相关问题