I wrote a validation annotation done by a custom ConstraintValidator . I also want to generate very specific ConstraintViolation objects that use the values ββcomputed during the validation process during message interpolation.
public class CustomValidator implements ConstraintValidator<CustomAnnotation, ValidatedType> { ... @Override public boolean isValid(ValidatedType value, ConstraintValidatorContext context) {
Hypothetical error message in my message source:
CustomAnnotation.notValid = The supplied value {value} was not valid because {reason}.
The context passed to the isValid method provides an interface for creating a constraint violation and, finally, adding it to the context. However, I cannot figure out how to use it. According to this documention for the version used, I can add bean and property nodes to the violation. This is the only additional data that I can specify to determine the violation, but I do not understand how they can be displayed in the parameters in the error message.
Million-dollar question: how do I pass dynamic parameters to validation error messages using a custom validator? I would like to populate these {value} and {reason} fields using the ConstraintValidatorContext interface for breaking the rules.
Retrieving an instance of the message source and interpolating the message in the user-defined validator is not an option - messages that exit the check receive interpolation regardless of whatsoever, and interpolation inside will cause some messages to be interpolated twice, potentially destroying the shielded ones single quote screens or other characters with special meanings in the definition file of my messages.
Misha source share