Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 248
VishwaKumar Avatar asked May 01 '12 08:05

VishwaKumar


4 Answers

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

like image 114
Elnur Abdurrakhimov Avatar answered Nov 15 '22 22:11

Elnur Abdurrakhimov


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

like image 41
CoKe Avatar answered Nov 15 '22 22:11

CoKe


There is an example in the docs.

like image 38
1ed Avatar answered Nov 15 '22 23:11

1ed


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

like image 42
Lebnik Avatar answered Nov 15 '22 21:11

Lebnik