Symfony ChoiceType $choices - 交换标签和值
Symfony ChoiceType $choices - labels and values swapped
Symfony 2.8.2
根据 Symfony 文档 "The choices option is an array, where the array key is the item's label and the array value is the item's value"
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();
呈现如下:
文档有错吗?还是我没弄对?
在较新的 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 4 开始,不再支持 "choices_as_values":
https://github.com/symfony/symfony/issues/14951
您必须使用它来实现相同的目的:
'choice_label' => function ($value) {
return $value;
},
Symfony 2.8.2
根据 Symfony 文档 "The choices option is an array, where the array key is the item's label and the array value is the item's value"
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();
呈现如下:
文档有错吗?还是我没弄对?
在较新的 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 4 开始,不再支持 "choices_as_values": https://github.com/symfony/symfony/issues/14951
您必须使用它来实现相同的目的:
'choice_label' => function ($value) {
return $value;
},