如何将参数传递给已翻译的验证错误消息

时间:2014-03-11 08:59:29

标签: validation symfony internationalization translation

在Symfony2中,you can translate your validation error messages

验证文件

# src/Acme/BlogBundle/Resources/config/validation.yml
Acme\BlogBundle\Entity\Author:
    properties:
        name:
            - NotBlank: { message: "author.name.not_blank" }

翻译文件

# validators.en.yml
author.name.not_blank: Please enter an author name.

但是如何将参数传递给翻译文件,例如我想通过所需的最小或最大长度?

author.name.min_length: "Required length: %limit% characters."

1 个答案:

答案 0 :(得分:11)

怎么样,

Acme\BlogBundle\Entity\Author:
    properties:
        name:
            - NotBlank: { message: "author.name.not_blank" }
            - Length:
                min: 3
                minMessage: "author.name.min_length"

您的翻译文件应包含

# validators.en.yml
author.name.not_blank: Please enter an author name.
author.name.min_length: "Required length: {{ limit }} characters."

此处的{{ limit }}占位符将适合长度约束的min pamarater。