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?
Validation translations go to the validators.LANG.yml
files — not messages.LANG.yml
ones.
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
There is an example in the docs.
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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With