Symfony2 formbuilder选择字段标签翻译,值不

克里斯·亚历山大

因此,这一直可以正常工作,直到我的上一个composer.phar更新为止。我的代码没有更改,但显然还有其他更改。在我的formbuilder中,我有多个选项字段,并且所有字段(对于整个表单)都被翻译了。所有选项字段的标签都在正确翻译和显示,但是没有任何值被翻译。这是表单类型代码片段的形式:

public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder-> //...
  ->add('size_length','number', array(
    'label' => 'block.length',
    'max_length' => 50,
    'required' => false,
    'attr' => array(
       'placeholder' => '123...' )
  ))
  ->add('size_length_units','choice', array(
    'empty_value' => 'block.units.select',
    'choices'   => array(
      'block.units.millimeters' => 'block.units.millimeters',
      'block.units.centimeters' => 'block.units.centimeters',
      'block.units.meters'      => 'block.units.meters',
      'block.units.kilometers'  => 'block.units.kilometers',
      'block.units.inches'      => 'block.units.inches',
      'block.units.feet'        => 'block.units.feet',
      'block.units.yards'       => 'block.units.yards',
      'block.units.miles'       => 'block.units.miles',
      'block.units.furlongs'    => 'block.units.furlongs',
      'block.units.rods'        => 'block.units.rods'),
    'label' => 'block.length.units',
    'required' => false,
    'multiple' => false
    ));
}

/**
 * @param OptionsResolverInterface $resolver
 */

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'My\Entity\Block',
        'translation_domain' => 'block',
        'cascade_validation' => true
    ));
}

HTML如下所示:

<select id="blockEditForm_size_length_units" name="blockEditForm[size_length_units]">
<option value="">(Units)</option>
<option value="block.units.millimeters">Millimeters</option>
<option value="block.units.centimeters">Centimeters</option>
<option value="block.units.meters">Meters</option>
<option value="block.units.kilometers">Kilometers</option>
<option value="block.units.inches">Inches</option>
<option value="block.units.feet">Feet</option>
<option value="block.units.yards">Yards</option>
<option value="block.units.miles">Miles</option>
<option value="block.units.furlongs">Furlongs</option>
<option value="block.units.rods">Rods</option>
</select>

有任何想法吗?再次说明,这之前都是可行的(标签和选项均已正确翻译)。现在,它只是标签,而不是值。我在多种形式和同一形式的多个选择字段中经常发生这种情况(经常使用相同的单位)。将不胜感激任何人的任何想法或见解。

基克斯

好吧,从Symfony 2.0开始,从技术上讲,值没有通过转换器传递。而且没有任何变化,因为那样可能会引入这种行为。

实际上,选项值不应通过转换器传递,因为它们与数据和业务逻辑有关,而不是与用户体验有关。也许,您应该使用更简洁的内容(例如,'millimeters', 'centimeters'等)来替换choices数组中的占位键

但是,如果出于某种奇怪的原因而需要转换这些值(我无法想象这种情况,但是假设有一个情况),则可以尝试覆盖Symfony 2中的代码choice_widget_optionsform_div_layout.html.twig,如下所示:

{% block choice_widget_options %}
{% spaceless %}
    {% for group_label, choice in options %}
        {% if choice is iterable %}
            <optgroup label="{{ group_label|trans({}, translation_domain) }}">
                {% set options = choice %}
                {{ block('choice_widget_options') }}
            </optgroup>
        {% else %}
            <option value="{{ choice.value|trans({}, translation_domain) }}"{% if choice is selectedchoice(value) %} selected="selected"{% endif %}>{{ choice.label|trans({}, translation_domain) }}</option>
        {% endif %}
    {% endfor %}
{% endspaceless %}
{% endblock choice_widget_options %}

有关覆盖表单模板的更多信息,您可以查看Symfony 2的官方文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章