I'm trying to have one validator use multiple constraints withtout success. The @Constraint annotations take an array as input; and as described in this post, it should be possible: Multiple validation options in Annotation.
When I try to validate a Resource, I get the following error
HV000150: The constraint NirValidator defines multiple validators for the type NirResource: NirCalculCleConstraint, NirBrutLongueurConstraint. Only one is allowed
Here is my interface:
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy = {NirCalculCleConstraint.class, NirBrutLongueurConstraint.class})
public @interface NirValidator {
String message() default "";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
and those 2 constraints classes:
public class NirBrutLongueurConstraint implements ConstraintValidator<NirValidator, NirResource> {
@Override
public boolean isValid(NirResource value, ConstraintValidatorContext constraintValidatorContext) {
return value == null || value.toString().length() == LONGUEUR_MIN_NIR || value.toString().length() == LONGUEUR_MAX_NIR;
}
}
public class NirCalculCleConstraint implements ConstraintValidator<NirValidator, NirResource> {
private int calculerCle(NirResource value) {
return (int) (CONSTANTE_CALCUL_CLE_NIR - ((Long.parseLong(value.toString(false))) % CONSTANTE_CALCUL_CLE_NIR));
}
@Override
public boolean isValid(NirResource value, ConstraintValidatorContext constraintValidatorContext) {
return Integer.parseInt(value.getCleNir()) != calculerCle(value);
}
}
All I could find online concerning thie error code is this link but it's not relevant in my situation as the lib mentionned is not on my classpath.