Is there some method (other than foreach, but good foreach could be sufficient), to return messages from validator.valid
from custom ConstraintValidator
?
Simple example (yes, I'm trying to do conditional validation)
@TestFormConstraint
public class TestForm {
@NotEmpty
String foo;
boolean checkBar = false;
@NotEmpty(groups=CheckBarGroup.class)
String bar;
//getters & setters ...
}
public class TestFormValidator
implements ConstraintValidator<TestFormConstraint, TestForm> {
@Autowired
private Validator validator;
@Override
public void initialize(ValidTestForm constraintAnnotation) {}
@Override
public boolean isValid(TestForm form, ConstraintValidatorContext context) {
if(form.isCheckBar()){
Set<ConstraintViolation<TestForm>> con =
validator.validate(form, CheckBarGroup.class);
if(!con.isEmpty()){
//ok, so what to do here? iterate over con and for each msg
//call context.buildConstraintViolationWithTemplate ?
return false;
}
}
return true;
}
}
Any idea?