gpt4 book ai didi

Symfony ChoiceType $choices - 标签和值交换

转载 作者:行者123 更新时间:2023-12-02 10:09:25 25 4
gpt4 key购买 nike

Symfony 2.8.2

根据 Symfony 文档“选择选项是一个数组,其中数组键是项目的标签,数组值是项目的值”

http://symfony.com/doc/2.8/reference/forms/types/choice.html#choices

但通过以下表格,我看到的情况恰恰相反:

        $filterForm = $this->createFormBuilder()
->add('vendorName', ChoiceType::class, array(
'expanded' => true,
'multiple' => true,
'choices' => array('label' => 'value') // <-- HERE
))
->add('filter', SubmitType::class, array('label' => 'Filter'))
->getForm();

渲染如下:

Label is value and the value is label

文档有误吗?还是我没理解错?

最佳答案

在较新的 Symfony 版本中,选项 choices_as_values 已被弃用。

https://github.com/symfony/symfony/issues/14951

这里有一个解释。我认为在你的情况下,你必须切换它或尽可能长时间地使用该选项。

choices_as_values设置为true。如果您升级,则必须更改它。

@Soullivaneuh choices_as_values is not directly to choice_label. So you are talking about a different topic. choices_as_values controls where the choices are the keys or the values in the choices option. Symfony 2.0 shipped with choices as keys (and labels as values), which means that the easy syntax only works when your choices are integers or strings. Any other case (boolean choices for instance) required passing a ChoiceList object instead, making the usage more complex (especially for people forgetting that booleans cannot be used as keys as PHP just casts them to string silently). This is the reason why this option has been introduced in 2.7 to be able to flip the array (while maintaining BC). the advantage is that any type of data can be used in this way (strings, integers, floats, booleans, objects, arrays)

关于Symfony ChoiceType $choices - 标签和值交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44630752/

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