允许在选择字段类型中添加新值 [英] Allow to add a new value in a choice Field Type

查看:57
本文介绍了允许在选择字段类型中添加新值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用表单组件,并且具有选择字段类型在呈现给选择字段的表单上. 在客户端,我使用 select2插件,该插件使用设置为tags: true的select进行初始化,该设置允许添加一个新的价值. 但是,如果我添加一个新值,则服务器上的验证将失败,并显示错误

I use Form Component and have a choice Field Type on a form which is rendered to a select field. On a client-side I use select2 plugin which initializes the select with the setting tags: true which allows to add a new value in it. But if I add a new value then a validation on a server will fail with error

该值无效.

因为新值不在选择列表中.

because the new value isn't in a choice list.

是否可以通过添加新值来选择字段类型?

Is there a way to allow adding of a new value to choice a Field Type?

推荐答案

问题出在选择转换器中,它会擦除​​选择列表中不存在的值.
禁用变压器的解决方法对我有帮助:

The problem is in a choice transformer, which erases values that don't exist in a choice list.
The workaround with disabling the transformer helped me:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('choiceField', 'choice', ['choices' => $someList]);

    // more fields...

    $builder->get('choiceField')->resetViewTransformers();
}

这篇关于允许在选择字段类型中添加新值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆