-->
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: Check if object passes validators at object construction
PostPosted: Thu May 27, 2010 6:58 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Hello,

I have an object which is annotated with @NotEmpty, @Email and other validators. I would like to validate the state of the object right after its construction. Is there a way I can do this without saving the object in the database?

Thanks


Top
 Profile  
 
 Post subject: Re: Check if object passes validators at object construction
PostPosted: Fri May 28, 2010 4:29 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Sure, just get hold of a ValidatorFactory and create your own Validator instance, then call Validator.validate. How you get hold of the ValidatorFactory depends on the environment you are running in. You can always just boostrap Bean Validation directly (check the Hibernate Validator online documentation) or you might get hold of the ValidatorFactory provided by your container (eg via JNDI) or dependency injection framework.

--Hardy


Top
 Profile  
 
 Post subject: Re: Check if object passes validators at object construction
PostPosted: Fri May 28, 2010 3:18 pm 
Regular
Regular

Joined: Fri Mar 26, 2004 11:45 am
Posts: 75
Thanks, that's exactly what I was looking for.

I was able to get this to work using

However, I wasn't able to configure validationFactory using Spring. The following did not work because ValidatorFactoryImpl does not have no-arg constructor.
<bean id="validatorFactory" class="org.hibernate.validator.engine.ValidatorFactoryImpl"/>

Which validation factory implementation should I inject?

Thanks


Top
 Profile  
 
 Post subject: Re: Check if object passes validators at object construction
PostPosted: Sat May 29, 2010 5:35 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Depending on which Spring version you are using you might be able to use the bean provided by Spring. I think there is a LocalValidatorFactoryBean, but I am not sure.

Otherwise this might help - HV and Spring

--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.