2012-05-01 3 views
16

Мне нужно перевести сообщения об ошибках из моего типа формы. Вот моя форма Введите код:Symfony2: Как перевести пользовательские сообщения об ошибках в типы форм?

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

}

Я хочу, чтобы перевести «Вы должны ввести по крайней мере один адрес электронной почты для действительного представления» в getDefaultOptions() метод на французский. Я добавил перевод в messages.fr.yml. Но он не переводится. Любые идеи, как это можно сделать?

ответ

35

Подтверждение перевода перейдите к validators.LANG.yml файлам - не messages.LANG.yml.

+0

Я пытался создавать validators.LANG.yml и добавлять переводы, но это работает. – VishwaKumar

+6

Вы очистили кеш после добавления файла? –

+0

Мой плохой! Это сработало. Благодаря! – VishwaKumar

1

В примере docs приведен пример.

+0

Это очень ясно, но в моем случае я добавляю проверки в свой тип формы. – VishwaKumar

1

Замены не заданы в файле validation.yml, но с помощью Validator.

validators.en.yml

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

validation.yml

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

Это работает для меня с Symfony 2.4

+0

есть ли список всех доступных заполнителей a la '{{limit}}', '{{compare_value}}', '{{value}}' и так далее? –

Смежные вопросы