2

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();
    ...   
 }
...
}
MDP
  • 4,177
  • 21
  • 63
  • 119

1 Answers1

0

Could you please check if the following question can help as reference?

Can you change an annotation message at run time?

Community
  • 1
  • 1
  • Unfortunately not, the problem discussed in that post is different from mine (my code already solved the problem discussed in that post). – MDP Dec 25 '15 at 15:54