Question

So, this WAS working correctly, until my last composer.phar update... My code hasn't changed, but obviously something else has. In my formbuilder, I have multiple option fields, and all of the fields (for the entire form) are translated. The labels for all option fields are translating and displaying correctly, however none of the values are being translated. Here's the form Type code snippets:

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
    ));
}

Here's what the HTML looks like:

<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>

Any ideas? Again, this was all working before (both labels and options were being translated correctly). Now, it is only the labels, not the values. I have this occurring on multiple forms and in multiple choice fields within the same form (same units are often used). Would appreciate any ideas or insights anyone has.

Was it helpful?

Solution

Well, technically as of Symfony 2.0, values were not passed through the translator. And there were no changes since that could have introduced such behaviour.

Actually, option values should not be passed through the translator, because they are about data and business logic, not user experience. Perhaps, you should replace the placeholder keys in your choices array with something more concise, like 'millimeters', 'centimeters' etc.

However, if for some strange reason you'll need these values to be translated (I can't really imagine such a case, but let's assume there is one), you could try overriding the choice_widget_options block in Symfony 2's form_div_layout.html.twig, like this:

{% 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 %}

For more info on overriding form templates you can check out Symfony 2's official documentation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top