I have 2 Domain classes like ParentClass
and ChildClass
.
(ParentClass contains a list of Child class objects in Array list.)
I created separate Validator classes for ParentClass and ChildClass to do the custom validations.
Example validator class for the Parent class:
public class ParentClassValidator implements
ConstraintValidator<ValidatedByParentClassInterface, ParentClass> {
and then override the isValid()
method of ConstraintValidator
,
@Override
public boolean isValid(final ParentClass parentClass,
final ConstraintValidatorContext constraintValidatorContext) {
Example validator class for the Child class:
public class ChildClassValidator implements
ConstraintValidator<ValidatedByChildClassInterface, ChildClass> {
and then override the isValid()
method of ConstraintValidator
,
@Override
public boolean isValid(final ChildClass childClass,
final ConstraintValidatorContext constraintValidatorContext) {
//No option to pass Parent class object here?
One of the fields in my Child class need to be validated based on the value of the field in Parent class. But the isValid()
method of ConstraintValidator(https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html) has only 2 arguments so how do I pass the ParentClass object here in my ChildClassValidator?
or Should I use a for
loop in my ParentClassValidator to validate this scenario?