-->
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: Composite validator
PostPosted: Tue Mar 22, 2011 10:43 am 
Newbie

Joined: Tue Feb 01, 2011 6:04 am
Posts: 3
Hi all,
I have a rather simple validation scenario and cannot find out how to implement it using hibernate validator. I have a simple object, say User ; this object has an id (string), and this id may be an email address or a phone number. So i'd expected to find a simple composite "OrValidator" to wich i pass my "EmailValidator" and "MobilePhoneNumberValidator". The field is valid if at least one validator says it's valid.

The code could look like the following :

Code:
class User {
  @Or(OrValidator.class, EmailValidator.class)
  String id;

}


Has anyone encountered such a scenario ?

thanks

Hermann


Top
 Profile  
 
 Post subject: Re: Composite validator
PostPosted: Tue Mar 22, 2011 11:18 am 
Newbie

Joined: Mon Mar 21, 2011 7:59 pm
Posts: 2
I don't believe you can use the "OR" logic explicitly on a given attribute. However, if you are using the 4.2.0.Beta2 release, you can "compose" a custom constraint which will take either.

Hardy's blog post at http://in.relation.to/18074.lace demonstrates the creation of a "PatternOrSize" constraint. The example code taken from the blogpost is as follows:
Code:
@ConstraintComposition(OR)
@Pattern(regexp = "K")
@Size(min = 2, max = 10)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
   public abstract String message() default "OR";
   public abstract Class<?>[] groups() default { };
   public abstract Class<? extends Payload>[] payload() default { };
}


Using a similar concept, you should be able to fairly easily compose your own EmailOrMobilePhone constraint.

JRN


Top
 Profile  
 
 Post subject: Re: Composite validator
PostPosted: Tue Apr 05, 2011 9:41 am 
Newbie

Joined: Tue Feb 01, 2011 6:04 am
Posts: 3
Hi,
thanks for your answer! That's definitely a huge improvement of HV !!!
I'll test this feature in the 4.2.0.Beta2 !

Hermann


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.