Symfony2:如何在表单类型中翻译自定义错误消息?

时间:2012-05-01 08:55:14

标签: forms internationalization symfony

我需要从表单类型中翻译错误消息。这是我的表格类型代码:

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中添加了翻译。但它没有得到翻译。任何想法如何做到这一点?

4 个答案:

答案 0 :(得分:38)

验证翻译转到validators.LANG.yml个文件 - 而不是messages.LANG.yml个。

答案 1 :(得分:2)

替换不是在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

答案 2 :(得分:1)

docs中有一个例子。

答案 3 :(得分:1)

很简单,请参阅http://symfony.com/doc/current/book/translation.html#translating-constraint-messages 并在/app/config/config.yml中设置default_locale或使用$ this-> get(' request') - > setLocale(' ru');