-->
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.  [ 2 posts ] 
Author Message
 Post subject: How to write OR condition in xml constraints
PostPosted: Mon Oct 22, 2012 9:26 am 
Newbie

Joined: Tue Jul 24, 2012 2:39 pm
Posts: 10
Hi, I want to know how can I write OR condition in Constraint XML.

<constraint annotation="org.hibernate.validator.constraints.Length">
<element name="max">30</element>
</constraint>

(OR)
<constraint annotation="javax.validation.constraints.Pattern">
<element name="regexp">\x28\d{3}?\x29\x20\d{3}?\x2D\d{4}?</element>
</constraint>

Please let me know.

Thanks.


Top
 Profile  
 
 Post subject: Re: How to write OR condition in xml constraints
PostPosted: Fri Nov 02, 2012 7:16 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
AFAICS that's not really possible. You could create a custom constraint using the boolean constraint composition feature of Hibernate Validator (see http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-specifics.html#d0e3701) and assign that via XML:

Code:
@ConstraintComposition(OR)
@Pattern(regexp = "\x28\d{3}?\x29\x20\d{3}?\x2D\d{4}?")
@Length(max = 30)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
   String message() default "{PatternOrSize.message}";
   Class<?>[] groups() default { };
   Class<? extends Payload>[] payload() default { };
}

...

<constraint annotation="com.mycompany.PatternOrSize"/>


If you want to have the length and regexp configurable for the composed constraint, you can use attribute overrides (see http://beanvalidation.org/1.1/spec/#constraintsdefinitionimplementation-constraintcomposition) like this:

Code:
@ConstraintComposition(OR)
@Pattern(regexp = "\x28\d{3}?\x29\x20\d{3}?\x2D\d{4}?")
@Length(max = 30)
@ReportAsSingleViolation
@Target({ METHOD, FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { })
public @interface PatternOrSize {
   String message() default "{PatternOrSize.message}";
   Class<?>[] groups() default { };
   Class<? extends Payload>[] payload() default { };
   
   @OverridesAttribute(constraint=Length.class, name="max")
   int max() default Integer.MAX_VALUE;

   @OverridesAttribute(constraint=Pattern.class, name="regexp")
   String regexp();
}

...

<constraint annotation="com.mycompany.PatternOrSize">
   <element name="max">30</element>
   <element name="regexp">\x28\d{3}?\x29\x20\d{3}?\x2D\d{4}?</element>
<constraint>


Hth,

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


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