全局覆盖 symfony2.1 上的 NotBlank 约束消息?

use*_*048 5 php constraints messages symfony

如何在整个 Symfony 2.1 应用程序中全局覆盖NotBlank约束消息This value should not be blank

我已经尝试过使用validators.en.yml文件,但只有当我将validation.yml文件中的每个 NotBlank 约束设置为常量时才有效not.blank.field- 但修改太多。

che*_*fly 4

要替换在应用程序级别触发约束This value should not be blank时显示的消息NotBlank,只需在正确的位置创建正确的翻译文件即可。

为了简单起见,您的区域设置似乎是英语 (en),因此创建文件app/Resources/translations/validators.en.yml并在其中写入以下行:

# app/Resources/translations/validators.en.yml
This value should not be blank.: Y U NO SET THE VALUE?!
Run Code Online (Sandbox Code Playgroud)

第一部分必须与您要翻译的消息完全匹配(在您的情况下This value should not be blank.)。

如果您有自定义文件/文件夹层次结构,只需在<kernel root directory>/Resources/translations.

如果您希望其他语言(例如法语)具有相同的行为,只需在同一文件夹中创建相应的文件即可:

# app/Resources/translations/validators.fr.yml
This value should not be blank.: La valeur doit etre specifiee.
Run Code Online (Sandbox Code Playgroud)

添加新文件后不要忘记清除缓存!

为什么它有效?因为 Symfony2 按位置对翻译文件进行优先级排序:http://symfony.com/doc/current/book/translation.html#translation-locations-and-naming-conventions