Symfony FOSRest ChoiceType

时间:2017-06-21 20:17:49

标签: forms 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上。

1 个答案:

答案 0 :(得分:0)

您需要提供有效的choices

  

choices选项是一个数组,其中数组键是项目的   label和数组值是项的值

根据你的例子,应该是这样的:

/**
 * {@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
    ]);
}