i want to put set of standart constraints (like not null alphanumeric string with length from 3 to 240 chars) on the fields (String in this case) and want to know is there a way to override some of this constraint in the model code. Also is this will be an overriding, or just validating two times for overrided annotation?
it should be something like this
@AlphanumericString
@Size(min=100, max=150) //override standart values from AlphanumericString annotation
thanks for you answers
ok, answer myself. there is @OverridesParameter wich helps to reassign nested annotation parameter
@Numerical
@Size //arbitrary parameter values
@ConstraintValidator(FrenchZipcodeValidator.class)
@Documented
@Target({ANNOTATION_TYPE, METHOD, FIELD})
@Retention(RUNTIME)
public @interface FrenchZipCode {
String message() default "Wrong zipcode";
String[] groups() default {};
@OverridesParameters( {
@OverridesParameter(constraint=Size.class, parameter="min")
@OverridesParameter(constraint=Size.class, parameter="max") } )
int size() default 5;
@OverridesParameter(constraint=Size.class, parameter="message")
String sizeMessage() default "{error.zipcode.size}";
@OverridesParameter(constraint=Numerical.class, parameter="message")
String numericalMessage() default "{error.zipcode.numerical}";
}
It’s a nice question. The JSR 303 Bean Validation specification describes validation routine in the section 3.5.
In your case, you deal with validation of a simple
Stringfield where the targeted group isDefault. You have two validation constraints (@AlphanumericString and @Size) which according to the documentation will be validated/processed separately in no particular order.So to answer your question. No, there will be no override applied to your
@AlphanumericStringwhen you use@Sizeadditionaly. To be able to achieve what I think you attempt to do, you may create a constraint composition where you overridde attributes from composing annotations like that:and use it like that: