Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass parameter to translated validation error message

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."
like image 511
Gottlieb Notschnabel Avatar asked Mar 11 '14 08:03

Gottlieb Notschnabel


1 Answers

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.

like image 124
Ahmed Siouani Avatar answered Nov 10 '22 00:11

Ahmed Siouani