-->
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.  [ 2 posts ] 
Author Message
 Post subject: Displaying invalid value in validator error message
PostPosted: Tue Dec 08, 2009 8:31 am 
Newbie

Joined: Sun Oct 10, 2004 2:19 pm
Posts: 7
If I have a validation annotation like this:

Code:
@Size(min = 2, max = 50, message="{name.required}")


and a ValidationMessage.properties file like this:

Code:
name.required=Invalid name '{value}', should be from {min} to {max} characters


Is it possible to interpolate the invalid value (e.g. '{value}') of the ConstraintViolation into the message? It seems only annotation based parameters, e.g. '{min}' and '{max}' can be interpolated. The same question would apply to the path of the failing property, e.g. '{path}'.

The resulting error message should be
"Invalid name 'A', should be from 2 to 50 characters"

I'm using 4.0.2 GA.

Thanks,


Jeroen


Top
 Profile  
 
 Post subject: Re: Displaying invalid value in validator error message
PostPosted: Tue Dec 08, 2009 9:43 am 
Hibernate Team
Hibernate Team

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

The Bean Validation spec does not include value interpolation in the definition of the MessageInterpolator algorithm. Maybe partly because one cannot guarantee that the validated object has a valid toString implementation.

The only way to achieve what you want is to write your own MessageInterpolator and then use the MessageInterpolator.Context.getValidatedValue() to get hold of the validated value. You can then use this value to do your own message interpolation. Have a look at ResourceBundleMessageInterpolator to see how to write a MessageInterpolator.

Btw, this won't help you to get the path interpolated though.

--Hardy


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.