-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Custom message in HibernateValidator
PostPosted: Thu Jul 30, 2009 7:14 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Hi all.

I have annotated some POJO's fields with validation tags. It works fine, but the message I receive when the exception is raised is not enough intuitive. Is (easy) possible to specify a text message for each field annotation?.

E. g.:
Code:
public class AnnotatedPojo implements Serializable {

   // other fields and getter/setter...

   private String date = null;

   @Pattern(regex = Utils.DATE_REGEXP)
   public String getDate() {
      return date;
   }

   public void setDate(String date) {
      this.date = date;
   }
}


In this case, if the text written in 'date' field does not match the regular expression, Hibernate raises an error but it does not specify which is exactly the problem. I would like to indicate, at least, the field name and the source of the problem ("date format" or something similar).

Any idea?

_________________
Born to lose... live to win!


Top
 Profile  
 
 Post subject: Re: Custom message in HibernateValidator
PostPosted: Wed Aug 05, 2009 11:22 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

Use the 'message' parameter. All constraints have to support this parameter. It allows you to customize the error messages. You can add the full error message as value to parameter or you can use a token like {my.date.pattern.message} in which case you have to also create a ValidationMessages.properties file and add it to the classpath. In the properties file you use the token as key and the actual value as message.

The current distribution of Hibernate Validator contains a (not yet complete) user guide. The usage of messages is already explained. Otherwise the JSR-303 spec itself is a good place for some examples.

--Hardy


Top
 Profile  
 
 Post subject: Re: Custom message in HibernateValidator
PostPosted: Thu Aug 06, 2009 4:56 am 
Regular
Regular

Joined: Tue Feb 17, 2009 5:13 am
Posts: 59
Ok, I have used the message parameter. It works fine.

Thank you!

_________________
Born to lose... live to win!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.