gpt4 book ai didi

php - Symfony ChoiceType 动态填充选项并初始化空选项数组

转载 作者:行者123 更新时间:2023-12-05 09:19:06 24 4
gpt4 key购买 nike

我试图在 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

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

谢谢!

最佳答案

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

您可以设置包含所有可用值的初始选择数组,或者您可以使用以下方法禁用该验证:

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

关于php - Symfony ChoiceType 动态填充选项并初始化空选项数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42226036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com