-->
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: How do I include the field name in validation error messages
PostPosted: Wed Feb 09, 2011 5:25 am 
Newbie

Joined: Fri Mar 12, 2010 9:42 am
Posts: 19
Hi all
We are using JSF and2 <h:messages /> to display all validation errors above our forms. This means we need to include the field names (or labels) so that the user can see which field failed validation.

Right now we are inserting custom error messages on each validation constraint, e.g. @NotNull(message = "username is required"), but I am looking for a more generic approach. Since we know the field name at validation time, and that field name is tied to a input field and a label in the form, it must be possible to extract this info somehow. And JSF seems to do something clever here.. If a field is marked as required (<h:inputText required="true"... />) validation fails and the error message is: "Username: value is required". And the resource bundle property for this is:

javax.faces.component.UIInput.REQUIRED={0}: value is required

Where {0} is the field name.

Is there any way of achieving something like this for fields validated by Hibernate Validator? Ideally I would like to use a resource bundle message like the one above where the field name is avaliable.

Regards,

Anders


Top
 Profile  
 
 Post subject: Re: How do I include the field name in validation error messages
PostPosted: Wed Feb 09, 2011 9:21 am 
Hibernate Team
Hibernate Team

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

I am not sure what magic is applied in JSF, but I would assume they either post-process the validated messages or have a custom message interpolator. Using a custom message interpolator is also what I would recommend for your usecase.

--Hardy


Top
 Profile  
 
 Post subject: Re: How do I include the field name in validation error messages
PostPosted: Wed Feb 09, 2011 11:56 am 
Newbie

Joined: Fri Mar 12, 2010 9:42 am
Posts: 19
Hi
I found out that this can easily be changed by modifying the default JSF message format, which is configured in this message identifier:
javax.faces.validator.BeanValidator.MESSAGE

More info can be found here (the property is commented around line 89):
http://www.docjar.com/html/api/javax/fa ... .java.html

So, problem solved, great stuff :)

-Anders


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.