-->
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.  [ 3 posts ] 
Author Message
 Post subject: Class level validation duplicate validations
PostPosted: Mon May 17, 2010 10:03 am 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
Hi,

I'm wondering how to handle class level validation when there are two validations that are identical but configured for different fields. Apparently, that is illegal.

The following case: a registration form where users are required to enter an email address and password twice.

The class level validator should compare the values of the the fields and throw an error if they don't match.

The interface looks like this
Code:
@Target({TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {
   String message() default "error.fieldnomatch";
   Class<?>[] groups() default {};
   Class<? extends Payload>[] payload() default {};
   String[] value();
}


The value contains an array of fields that are supposed to match.

Now, in order to validate both email and password, I would either want to annotate this FieldMatch validation for both field combinations (which seems to be illegal), or I would want to pass both group of fields ([email, email2],[password,password2]) as a value array (which seems impossible because only 1-dimensional arrays are allowed, or I would want to list multiple values value1=[password,password2],value2=[email,email2]

The latter is possible but less elegant because you have to hardcode the number of fields you can potentially validate nad it is no longer possible to isolate the error to a particular field.

Any ideas on the best approach?

Kind regards,
Marc


Top
 Profile  
 
 Post subject: Re: Class level validation duplicate validations
PostPosted: Mon May 17, 2010 6:40 pm 
Pro
Pro

Joined: Wed Nov 05, 2003 7:22 pm
Posts: 211
This explains it
http://stackoverflow.com/questions/1972 ... or-jsr-303


Top
 Profile  
 
 Post subject: Re: Class level validation duplicate validations
PostPosted: Tue May 18, 2010 4:01 am 
Hibernate Team
Hibernate Team

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

A class level constraint is the way to go. As in the example on stackoverflow you would write a class level validator which takes care of one field combination and then use the the inner List annotation to allow the configuration of multiple instances of @FieldMatch

Code:
@Target({TYPE})
@Retention(RUNTIME)
@Constraint(validatedBy = FieldMatchValidator.class)
@Documented
public @interface FieldMatch {
   String message() default "error.fieldnomatch";
   Class<?>[] groups() default {};
   Class<? extends Payload>[] payload() default {};
   String[] value();
}

@Target({ TYPE })
@Retention(RUNTIME)
@interface List {
   FieldMatch[] value();
}


Also in connection with validating bean interdependencies you might want to check out the @ScriptAssert constraint which got added to the latest version of Hibernate Validator (4.1.x).

--Hardy


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