|
From: Daniel M. <mi...@pa...> - 2004-05-18 03:42:13
|
Seth,
I have approached this problem by creating my complex validator object and
wiring in the commons-validator like this:
<bean name="someValidator" class="yada.yada.SomeValidator">
<property name="validator"><ref bean="beanValidator"/></property>
</bean>
Then inside my SomeValidator class (in the validate() and supports()
methods), I just invoke the corresponding beanValidator methods to ensure
that it is supported. For example:
boolean supports(Class clazz) {
// Note: very simple
return beanValidator.supports(clazz);
}
void validate(Object obj, Errors errors) {
beanValidator.validate(obj, Errors);
// Add custom validation logic here
// It can even be conditional if you please...
if (!errors.hasErrors()) {
// Only execute these validations if there are no other errors.
}
}
You could add as many validator objects to your custom validation class as
you like and create your own custom chain mechanism inside that if you like.
In fact, you could even implement a Validator class with a property that
takes a list of validators to invoke. Something like this (very quickly
written in my email client, so be easy on the syntax):
private List validators;
public void setValidators(List vals) {
validators = vals;
}
public boolean supports(Class clazz) {
boolean result = true;
Iterator itr = validators.iterator();
while(itr.hasNext()) {
Validator val = (Validator) itr.next();
result = result && val.supports(clazz);
}
return result;
}
public void validate(Object obj, Errors errors) {
Iterator itr = validators.iterator();
while(itr.hasNext()) {
Validator val = (Validator) itr.next();
val.validate(obj, errors);
}
}
Wire it up with your list of validators and plug it into your controller
class and off you go.
In every case that I have come across a single beanValidator
(commons-validator adapter) wired into a custom validation class gives me
all the flexibility I need.
Hope that helped.
Daniel Miller
P.S. The guys who designed this framework were geniuses to be able to come
up with such a flexible implementation. Thanks dev's.
-----Original Message-----
From: spr...@li...
[mailto:spr...@li...]On Behalf
Of se...@eh...
Sent: Monday, May 17, 2004 3:18 AM
To: spr...@li...
Subject: [Springframework-developer] Multiple Validators per Handler?
Hello,
Much along the lines of multiple View Resolvers, I'd like to ask the
developers
if they believe allowing multiple Validators per Handler is a good idea.
I've
run into this situation a few times, and it seems like a good idea.
With the addition of new validators that automatically determine the
validation
rules (commons-validator, for example), there aren't any explicit validator
objects written. A handler currently takes one validator. I often need to
code some specific, hard to specify via attributes validation rules that go
above and beyond what simple declarative validation rules can specify. I'd
like the specific validation rules to run along with the commons-validator
derived rules.
The solution for multiple View Resolvers was to look into the
ApplicationContext
and find all View Resolvers. There was no explicit ViewResolverChain
object.
With Handlers, the API has a setValidator. Not wanting to break the API, a
suggestion would be to add a setValidators(List validators) method to
Handler.
This would avoid creating a ValidatorChain object and would keep in the
spirit
of the View Resolvers solution.
Any ideas on this? I would be more than happy to provide the patch for
this.
Of course, if people think the best way to solve this is to create some sort
of
Validator Chain object and leave the API for Handler alone, I would create
the
patch for that as well.
Your feedback is much appreciated. I look forward to helping out!
Thanks,
Seth
----------------------------------------------------------------
This message was sent using IMP, the Internet Messaging Program.
-------------------------------------------------------
This SF.Net email is sponsored by: SourceForge.net Broadband
Sign-up now for SourceForge Broadband and get the fastest
6.0/768 connection for only $19.95/mo for the first 3 months!
http://ads.osdn.com/?ad_id=2562&alloc_id=6184&op=click
_______________________________________________
Springframework-developer mailing list
Spr...@li...
https://lists.sourceforge.net/lists/listinfo/springframework-developer
|