문제

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.

도움이 되었습니까?

해결책

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.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top