Symfony ChoiceType初始化空白数组

时间:2017-02-14 12:20:23

标签: php symfony

我正在尝试在Symfony 3.0.9中定义一些ChoiceType,其中填充了ajax,具体取决于动态创建了多少个选项,但验证表单显示所选的选项无效。

我像这样定义ChoiceType:

->add('position', ChoiceType::class, [
    'placeholder' => 'Select position',
    'choices'  => [],
    'attr' => [
       'class' => 'form-control choice-position'
    ],
 ])

我得到的错误是:

Symfony\Component\Validator\ConstraintViolation
Object(Symfony\Component\Form\Form).children[lessonGroups].children[0].children[position] = 1

Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
Unable to reverse value for property path "position": The choice "1" does not exist or is not unique

Caused by:
Symfony\Component\Form\Exception\TransformationFailedException
The choice "1" does not exist or is not unique

我不知道是否需要任何进一步的信息。

谢谢!

1 个答案:

答案 0 :(得分:2)

因为您的定义不包含任何选择('choices' => [],)Symfony检测到比用户尝试提交的结果不在初始授权结果中。

您可以设置包含所有可用值的初始数组,也可以使用以下命令禁用该验证:

$builder->get('yourField')->resetViewTransformers();