I implemented my own annotation and ConstraintValidator (JSR-303). During the validation process I want to create different error codes:
public class SomeValidator implements ConstraintValidator<ConstraintAnnotation,SomeObject> {
@Override
public void initialize (ConstraintAnnotation constraintAnnotation) {
}
@Override
public boolean isValid (SomeObject some, ConstraintValidatorContext context) {
int parameter = some.getParameter();
if (parameter==1){
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("SomeObject.code.first").addConstraintViolation();
return false;
}
if (parameter==2){
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("SomeObject.code.second").addConstraintViolation();
return false;
}
return true;
}
}
But context.buildConstraintViolationWithTemplate() adds an error message not a code. So spring ties to expand code like this:
ConstraintAnnotation.someObject
not
SomeObject.code.second
How add custome code instead of message?