Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Overwrite the NotBlank Constraint Message on symfony2.1 globally?

How can I overwrite the NotBlank constraint message This value should not be blank globally throughout the Symfony 2.1 application?

I've tried with validators.en.yml files, but this works only if I set for every NotBlank constraint in my validation.yml files to a constant like not.blank.field - but is too much to modify.

like image 602
user1236048 Avatar asked Oct 05 '22 23:10

user1236048


1 Answers

To replace This value should not be blank message showing up when the NotBlank constraint is triggered at your application level, simply create the right translation file in the right place.

To make it simple, your locale seems to be English (en), so create the file app/Resources/translations/validators.en.yml and write the following line in it:

# app/Resources/translations/validators.en.yml
This value should not be blank.: Y U NO SET THE VALUE?!

The first part has to match exactly the message you want to translate (in your case This value should not be blank.).

If you have a custom files/folders hierarchy, simply create the file under <kernel root directory>/Resources/translations.

If you want to have the same behavior for another language (french for example), simply create the corresponding file in the same folder:

# app/Resources/translations/validators.fr.yml
This value should not be blank.: La valeur doit etre specifiee.

Do not forget to clear your cache after adding the new file!

Why does it work? Because Symfony2 priorities the translation files by locations: http://symfony.com/doc/current/book/translation.html#translation-locations-and-naming-conventions

like image 190
cheesemacfly Avatar answered Oct 19 '22 11:10

cheesemacfly