-->
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: How to pass additional objects to a ConstraintValidator?
PostPosted: Sun Jul 26, 2009 1:01 pm 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
For example, consider a bean CostUnit that should have a unique Id.
Cost units are managed by a CostUnitManager, which knows about all used Ids.

So, to implement the uniqueness check for the CostUnit id,
the corresponding ConstraintValidator needs a reference to the CostUnitManager instance
(in addition to the cost unit instance).

How to pass such additional objects to a JSR303 Bean Validator?

Thanks,
Holger


Top
 Profile  
 
 Post subject: Re: How to pass additional objects to a ConstraintValidator?
PostPosted: Tue Jul 28, 2009 4:08 am 
Hibernate Team
Hibernate Team

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

I guess in your case you could use a custom ConstraintValidatorFactory. The default factory uses the public no-arg constructor to instantiate a validator implementation. Using a custom factory you can use other means to instantiate the validator. For example you could inject a CostUnitManager via dependency injection. You can determine which factory is used via the Configuration object, eg

Code:
Validation.byDefaultProvider().configure().constraintValidatorFactory(new MyFactory()).buildValidatorFactory()


See also section 2.5 of the specification.

--Hardy


Top
 Profile  
 
 Post subject: Re: How to pass additional objects to a ConstraintValidator?
PostPosted: Wed Jul 29, 2009 5:29 am 
Beginner
Beginner

Joined: Tue Dec 26, 2006 3:50 pm
Posts: 22
Thanks for the info, Hardy.

Assuming that major DI containers like Spring will integrate with JSR303 and provide their own ConstraintValidatorFactory, this appears to be a clean solution.

It gets more complicated, if you need to inject non-singleton-scoped beans.
In the example above, the CostUnitManager could be scoped to a particular customer.
If another customer is loaded by the client, another CostUnitManager instance gets relevant and needs to be provided to the ConstraintValidator.
In this case, either you would have to implement and apply a custom scope in Spring or do the injection manually by providing a self-made ConstraintValidatorFactory and recreating the ConstraintValidator for a different customer.
For the latter way, it could be problematic that ConstraintValidators may be cached:
From section 2.4:
"The life cycle of a constraint validation implementation instance is undefined. Compliant implementations are allowed to cache ConstraintValidator instances retrieved from the ConstraintValidatorFactory"

Holger


Top
 Profile  
 
 Post subject: Re: How to pass additional objects to a ConstraintValidator?
PostPosted: Tue Aug 04, 2009 5:33 pm 
Newbie

Joined: Tue Jul 21, 2009 2:57 pm
Posts: 8
i agree with this...this is almost an absolute necessity on any series of validation rules. sometimes it's simply not practical to re-lookup stuff in each rule/constraint. i've already run into the prospect of doing a db lookup across a large selection of items in a few different circumstances. these really should be different constraints, but i have to implement them as a single validator to make it performant, resolving that a few bean properties do not occur across this large selection of other beans. another good example of this might be user-specific validation, validating with the bean and the user in the validators.

another thing i've found is the need to customize metadata associated with a constraint violation. in most cases, i've been able to accomplish it by adding to my annotations.

there should be a validation heap/stack available to the validators, something like a map of anything you like, which can be looked up by name added to, subtracted from in the validators. additionally, i think someone suggested that the message template/raw message be converted to an object from a string.

i'd also like to see some kind of conversion of property to user-readable property name. it's common to add context to error messages such that if property X is causing the constraint violation, you need to add a user-readable, internationalized property as part of the message.
eg. message={user-friendly property name} is null.
for what should be generic messages, i've found us overriding every generic message that the annotations have.

...just got Beta2 and saw that it had some really good improvements.


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.