Just thought a bit more about this :)
If you're using HV 4.2 you could also create a reusable constraint @EmptyOrSize based on the
boolean constraint composition feature introduced with 4.2. This could roughly look like this:
Code:
@ConstraintComposition(CompositionType.OR)
@Pattern(regexp = "")
@Size
@ReportAsSingleViolation
@Target({ ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = { })
public @interface EmptyOrSize {
String message() default "{EmptyOrSize.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
@OverridesAttribute(constraint = Size.class, name = "min")
int min() default 0;
@OverridesAttribute(constraint = Size.class, name = "max")
int max() default Integer.MAX_VALUE;
}
This constraint could be used like this:
Code:
@EmptyOrSize(min=9)
String myString;
--Gunnar