Filip Górny
Filip Górny

Reputation: 1769

Translate empty value in Symfony form

I have a form with select box, and that field has an empty value property. I want to have it translated but adding translation_domain doesn't change anything.

<?php

namespace Devell\HowFolderBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;

class NoteType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
    {
    $builder
        ->add('name', 'text')
        ->add('body', 'textarea');


    $categoryChoices = array();

    $builder->add('category', 'entity', array(
        'class'         => 'HowFolderBundle:Category',
        'empty_value'   => 'note.form.category.choose',
        'translation_domain' => 'HowFolderBundle'
    ));
    }

public function getName()
{
    return "note";
}
}

Upvotes: 1

Views: 2078

Answers (2)

Vitalius
Vitalius

Reputation: 127

I know I'm really late with the answer but maybe it will be useful for someone. Check if your translation does not contain any special characters that YAML might try to parse. For example, this line would be parsed as array and, if it's the last line in the file, will not render any errors, etc.:

inventory.select.default_text: [ please select ]

It's as if translation is not found. What you need to do, is to quote your translation strings:

inventory.select.default_text: '[ please select ]'.

Upvotes: 1

Ahmed Siouani
Ahmed Siouani

Reputation: 13891

This should perfectely work as it's natively supported since > [Form] made it possible to translate the empty value of Choice fields.

Then the problem is probably related to your translation configuration, Check if your Translation component is enabled and well configured.

Upvotes: 3

Related Questions