playframework setting custom message for @Required field globally

白昼怎懂夜的黑 提交于 2019-12-22 06:36:58

问题


I am looking for help with translating Validation messeges in Play framework 2.2

I have fields that are required: f.e.

@Required(message = "To pole jest wymagane")
public String miesiac;

@Required
public String miejsce;

@Required
public String oddzial;

But I would to have this message: "To pole jest wymagane" globally. How can I achive it? Should I use conf/messagess.pl file for translation To polish language. Please give me some help


回答1:


Yes, you should use the conf/messages file for your default/primary language text and then one or more of the conf/messages.xx files for your translations.

The built-in validators are already setup to use the messages files. For example, the Required validator will look for the key error.required in your messages and display that text. So just define that key in your message files with the text you want to use.

If you wanted to use something other than the default then just specify the key with the message attribute (instead of the full text like in your example).

Model class

@Required(message = "my.required.message")
public String miesiac;

conf/messages

my.required.message=Hey, you have to type something here.

Take a look at the documentation for more info:
Externalising messages and internationalization




回答2:


All what I found out. Here are my current custom messages in conf/messages

error.required=This field is required
error.invalid=You need to enter a number
constraint.required=Required*


来源:https://stackoverflow.com/questions/20471578/playframework-setting-custom-message-for-required-field-globally

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!