I am in need to call validator.validateProperty() from my custom Validator and looking for a way to pass BOTH messageTemplate and interpolated message to ConstraintValidatorContext. What i want to achieve is that if given property has a particular value than fire validation of another property.
MyCustomClassLevelValidator implements ConstraintValidator<Foo, Bar>{
@Autowired
private Validator validator
public boolean isValid(Bar bar,
ConstraintValidatorContext constraintValidatorContext){
if(bar.isSth()){
Set<ConstraintViolation<Bar>> somePropViolations = validator.validateProperty(bar, "someprop", Conditional.class);
for (ConstraintViolation<Bar> propertyViolation : somePropViolations) {
constraintValidatorContext.disableDefaultConstraintViolation();
constraintValidatorContext.buildConstraintViolationWithTemplate(propertyViolation.getMessageTemplate()).addNode(propertyViolation.getPropertyPath().toString())
.addConstraintViolation();
}
}
}
}
So the problem with my code is that when Bar is validated, constraint violations on "someprop" are not fully interpolated (constraint annotations attributes are not resolved)
class Bar{
...
@Digits(groups=Conditional.class, integer=4,fraction=0)
String someProp;
}
So when validating Bar like
Bar bar = new Bar();
bar.setSomeProp("99.9");
Set<ConstraintViolation<Bar>> constraintViolations = validator.validate(bar);
i see numeric value out of bounds (<{integer} digits>.<{fraction} digits> expected) instead of numeric value out of bounds (<4 digits>.<0 digits> expected)
Is there any way i put BOTH message Template and message text (interpolated version) on constraintValidatorContext ?