-->
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.  [ 4 posts ] 
Author Message
 Post subject: HANDLING TYPECAST EXCEPTION IN HIBERNATE VALIDATOR
PostPosted: Fri Apr 01, 2011 8:32 am 
Newbie

Joined: Fri Apr 01, 2011 7:53 am
Posts: 2
Hello,

I have a date field on my form. The date field has constraints like @Past on it. I have configured validation properties file to have validation messages for my constraints. However I cannot figure how do do i get nice custom messages for typecast errors on my properties. If there is a type cast error a nasty message like -

"Failed to convert property value of type java.lang.String to required type java.util.Date for property joinDate; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value "asdsa" from type java.lang.String to type java.util.Date; nested exception is java.lang.IllegalArgumentException: Unable to parse asdsa"

Displays on the UI. How do I replace this with something nice like "Enter valid date"

Thank you for your help!


Top
 Profile  
 
 Post subject: Re: HANDLING TYPECAST EXCEPTION IN HIBERNATE VALIDATOR
PostPosted: Fri Apr 01, 2011 9:01 am 
Hibernate Team
Hibernate Team

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

I think there is some misunderstanding here, Bean Validation and for this reason Hibernate Validator is about validating properties of certain types. It does not do any type conversion at all. The exception you see is not coming out of the Validator framework, but out of spring.

Per default the constraint @Past is only defined on java.util.Date and java.util.Calendar. The latest version of Hibernate Validator also supports YodaTime. So if you want to use @Past you need a Date instance. It looks to me that Spring tries to do exactly this. It tried to convert into a date. How and why it does it you have to look up in the Spring documentation.

Technically you can define a custom ConstraintValidator<Past, String> and register it w/ Hibernate Validator via the xml configuration. This custom implementation could take care of conversion from string to date and behave as you seem to wish.

--Hardy


Top
 Profile  
 
 Post subject: Re: HANDLING TYPECAST EXCEPTION IN HIBERNATE VALIDATOR
PostPosted: Fri Apr 01, 2011 9:06 am 
Newbie

Joined: Fri Apr 01, 2011 7:53 am
Posts: 2
Thanks for the reply!!
Oh. I am a little disappointed. So even for fields that expect integer input, if I want to make sure that when someone enters an invalid string and I do not want to show a type cast exception on the UI, I will have to create a custom constraint and then display the appropriate message using the custom constraint?.... Also is there any example of how I could reuse the built in validators for default constraints like @Past in the custom constraint validator?

Thanks


Top
 Profile  
 
 Post subject: Re: HANDLING TYPECAST EXCEPTION IN HIBERNATE VALIDATOR
PostPosted: Fri Apr 01, 2011 3:49 pm 
Hibernate Team
Hibernate Team

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

Not quite right. Some of the number based validators also accept strings. You can check the Bean Validation spec or the Hibernate Validator documentation on which data types each constraint can handle. Have a look at the online docs - http://docs.jboss.org/hibernate/validat ... ml_single/

--hardy


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