[2.0-java] i18n message with javax.validation.constraints (JSR 303)

782 views
Skip to first unread message

JuDe

unread,
Jul 5, 2012, 9:17:39 AM7/5/12
to play-fr...@googlegroups.com
Hi Play!ers,

Do you know how is it possible to modify the error message displayed when there is a validation error (package javax.validation.constraints) during a form binding ?
Consider a simple entity with a field like :

  @Past    // A date must be older than now
  @Formats.DateTime(pattern="dd/MM/yyyy")
  private Date birthday = new Date();

I would like to customize the message displayed when the date format is correct but newer than now (so the Past validation will fail). So I added the following line into the conf\messages file :
javax.validation.constraints.Past.message=Customized message

But it doesn't work... It is still the default message that is displayed. 
Do you have any idea to resolve that problem ?
  • Perhaps the message key is not javax.validation.constraints.Past.message (although it seems to be the one in the javadoc) ?
  • The play framework perhaps uses another i18n message mechanism than the resources bundle ?
Thanks in advance!

Jodson Leandro

unread,
Jul 5, 2012, 5:04:42 PM7/5/12
to play-fr...@googlegroups.com
do you have something like that?

if (filledForm.hasErrors()) {
  //you can take all the erros here
  Map<String, List<ValidationError>> errors = fiiledForm.erros();
  //now you can to work with the errors
  return badRequest(views.html.somewhere.show.render(filledForm));

JuDe

unread,
Jul 6, 2012, 5:39:16 AM7/6/12
to play-fr...@googlegroups.com
Thanks guys!

Simply adding a message parameter into the @Past just works!
@Past(message="my.message.key") 

In response to Jodson Leandro, I tried your suggestion but the validation error message was not a key but plain text. So it was not possible to handle it.
Reply all
Reply to author
Forward
0 new messages