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