In Symfony2, you can translate your validation error messages:

Validation File

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

Translation File

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

But how can I pass a parameter to the translation file, if e.g. I want to pass the required min or max length?

author.name.min_length: "Required length: %limit% characters."
有帮助吗?

解决方案

What about,

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

While your translation file should contain,

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

The {{ limit }} placeholder here will then fit the min pamarater of the length constraint.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top