Is there a way to add message parameters using ConstraintValidatorContext?
In the below example I want to add 3 values to the ConstraintValidatorContext:
ValidationMessages.properties:
Code:
validator.year={0} is outside the acceptable range. Value must be between {1} and {2}.
The isValid method from my custom validator:
Code:
public boolean isValid(Long year, ConstraintValidatorContext context) {
boolean result = true;
if ( year == null ) {
return result;
}
int currentYear = Calendar.getInstance().get(Calendar.YEAR);
if ( year > currentYear + 1 || year < 1900 ) {
// disable the default constraint violation
context.disableDefaultConstraintViolation();
// I want to add message parameters here but I'm not sure how to do that.
// my own custom error message
context.buildConstraintViolationWithTemplate("{validator.year}").addConstraintViolation();
result = false;
}
return result;
}