Hi,
I have a clarification with configuring bean validation for one scenario. Below is my compound constraint
Code:
@Target({ FIELD })
@Retention(RUNTIME)
@Constraint(validatedBy = { EmpIdValidator.class })
@NotNull(message = "Id cannot be null")
@Range(min = 0, max = 99, message = "Invalid EmpId Range")
public @interface ValidEmpId {
String message() default "Id does not exits";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
Code:
EmpValidator.isValid method
public boolean isValid(final Long id,
final ConstraintValidatorContext validatorContext) {
// query db and check the validity, for now just do plain validation
if ((id != null) && (id > 20) && (id < 80)) {
return true;
}
return false;
}
when i tested this setup, my bean is getting validated properly but the sequence is getting changed everytime,
- it is getting validated with both Range and EmpIdValidator above valid method interchanging.
How do I specify this in a proper sequence so that the validation occur in this order.
- NotNull
- Range
- EmpIdValidator
I am able to execute order properly this if I don't create compound constraint and directly specify the validations on the field directly, but I am supposed to re-use this validations in many pages. So, the only solution for me is to use compound constraint.
Can you please suggest how do I specify the sequence on above ValidEmpId to get problem solved?