VishwaKumar
VishwaKumar

Reputation: 3433

Symfony2: How to translate custom error messages in form types?

I need to translate the error messages from my form type. Here is my form Type code:

class ReferFriendType extends AbstractType {

public function buildForm(FormBuilder $builder, array $options)
{
    $defaultSubject = "This is a default referral subject.";
    $defaultMessage = "This is a default referral message.";

    $builder->add('email1', 'email',array(
        'required' => true,
        'label' => 'Email 1* :',
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email2', 'email',array(
        'label' => 'Email 2 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email3', 'email',array(
        'label' => 'Email 3 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email4', 'email',array(
        'label' => 'Email 4 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('email5', 'email',array(
        'label' => 'Email 5 :',
        'required' => false,
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('subject', 'text', array(
        'data' => $defaultSubject,
        'required' => true,
        'label' => 'Subject* :',
        'attr' => array('class' => 'large_text'),
    ));
    $builder->add('message', 'textarea', array(
        'data' => $defaultMessage,
        'required' => true,
        'label' => 'Message* :',
        'attr' => array('rows' => '5', 'cols' => '40'),
    ));

}

public function getDefaultOptions(array $options)
{
    $collectionConstraint = new Collection( array(
        'fields' => array(
            'email1' => array(
                new Email(),
                new NotBlank(array(
                    'message' => 'You must enter atleast one email address for a valid submission',
                )),
            ),
            'subject' => new NotBlank(),
            'message' => new NotBlank(),
        ),
        'allowExtraFields' => true,
        'allowMissingFields' => true,
    ));

    return array(
        'validation_constraint' => $collectionConstraint,
        'csrf_protection' => false,
    );
}

public function getName()
{
    return 'referFriend';
}

}

I want to translate 'You must enter atleast one email address for a valid submission' in getDefaultOptions() method into french. I have added the translation in the messages.fr.yml. But it is not getting translated. Any ideas how this can be done?

Upvotes: 19

Views: 25956

Answers (4)

CoKe
CoKe

Reputation: 639

The replacements are not set in the validation.yml file but by the Validator.

validators.en.yml

noFirstnameMinLimit: Please provide at least {{ limit }} characters

validation.yml

Acm\AddressBundle\Entity\Address:
    properties:
        firstname:
            - Length:
                min: 3 
                minMessage: "noFirstnameMinLimit"

This works for me with Symfony 2.4

Upvotes: 3

Lebnik
Lebnik

Reputation: 636

It`s easy, see http://symfony.com/doc/current/book/translation.html#translating-constraint-messages And set default_locale in /app/config/config.yml or play with $this->get('request')->setLocale('ru');

Upvotes: 1

1ed
1ed

Reputation: 3668

There is an example in the docs.

Upvotes: 1

Elnur Abdurrakhimov
Elnur Abdurrakhimov

Reputation: 44831

Validation translations go to the validators.LANG.yml files — not messages.LANG.yml ones.

Upvotes: 38

Related Questions