I'm building my own @annotation
that valids many fields of a class (so it's a class level annotation and not a field level annotation).
When there is an error I add a ConstraintViolation
and I print an error message taken from .properties file. The error is something like :
The field {1} must be less than the field {2}
What I need is the way to fill the variables {1} and {2} . And I have to do it inside the method isValid()
, since is there that I dinamically define what are values to show inside the error message in place of {1} and {2}
This is my annotations:
@EsempioAnnotationClassLevel(dateFromNomeCampo={"dataDiNascitaFrom","dataLavoroFrom",...})
This is my interface:
@Constraint(validatedBy = EsempioAnnotationClassLevelValidator.class)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface EsempioAnnotationClassLevel {
String[] dateFromNomeCampo();
String message() default "Errore FatherSearchInterface;
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
this is my class that implements ConstraintValidator
:
public class EsempioAnnotationClassLevelValidator implements ConstraintValidator<EsempioAnnotationClassLevel, Object>{
...
public boolean isValid(Object object, ConstraintValidatorContext cxt) {
...
cxt.buildConstraintViolationWithTemplate("errorMessage").addNode("field").addConstraintViolation();
...
}
...
}