Reputation: 11896
How do I execute a ConstraintValidator
within a ConstraintValidator
?
I have a custom @NotEmpty
and have a custom ConstraintValidator
's for each type, and it works fine.
Now I want to create a class-level constraint that checks at least one of specified fields is not empty using those custom ConstraintValidator
's I already have. The constraint part(@ClassNotEmpty
) is done, but the problem is the ConstraintValidator
implementation. how do I obtain a ConstraintValidator
instance for a given constraint? i.e.
isValid(Object value, ConstraintValidatorContext constraintValidatorContext) {
String[] fieldValues = getFieldValues(value, this.classNotEmpty.fieldNames());
for (String fieldValue : fieldValues) {
<What do I put here?>.isValid(fieldValue, constraintValidatorContext);
}
}
Is there a way to do this without pulling up the validation routine to a helper class?
BTW, I'm using Spring and Hibernate Validator.
Upvotes: 1
Views: 1809
Reputation: 11896
After further research, it appears that there is no known solution to obtain a ConstraintValidator
instance for a given constraint, portable or not. A helper class seems to be the only way to do this.
Upvotes: 1
Reputation: 21883
I don't have the info in front of me, but I remember reading something about validators that are composed of other validators. So if you have two validators A and B. You can define a third 'C' which is composed of the other two. Hunt through the doco, you should find it.
Just did a search, read this: http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html/validator-customconstraints.html#validator-customconstraints-compound
It may be what you are looking for.
Upvotes: 0