简体   繁体   English

约束验证器根据父类中的另一个字段来验证子类中的一个字段

[英]Constraint validator to validate a field in Child class based on another field in Parent class

I have 2 Domain classes like ParentClass and ChildClass . 我有2个Domain类,例如ParentClassChildClass (ParentClass contains a list of Child class objects in Array list.) (ParentClass在“数组”列表中包含子类对象的列表。)

I created separate Validator classes for ParentClass and ChildClass to do the custom validations. 我为ParentClass和ChildClass创建了单独的Validator类来执行自定义验证。

Example validator class for the Parent class: Parent类的示例验证器类:

    public class ParentClassValidator implements 
ConstraintValidator<ValidatedByParentClassInterface, ParentClass> {

and then override the isValid() method of ConstraintValidator , 然后覆盖ConstraintValidatorisValid()方法,

    @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 , 然后覆盖ConstraintValidatorisValid()方法,

    @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. 需要根据Parent类中该字段的值来验证Child类中的一个字段。 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? 但是ConstraintValidator( https://docs.oracle.com/javaee/7/api/javax/validation/ConstraintValidator.html )的isValid()方法只有两个参数,因此如何在ChildClassValidator中传递ParentClass对象?

or Should I use a for loop in my ParentClassValidator to validate this scenario? 还是应该在ParentClassValidator中使用for循环来验证这种情况?

Looks like using a for loop in my ParentClassValidator to validate this scenario is the only possible solution 似乎在我的ParentClassValidator中使用for循环来验证这种情况是唯一可能的解决方案

So, instead of having 2 validators ie 1 for parent class and 1 for chid class, it is better to put all the custom validations(including child class validations) in the parent class validator 因此,最好不要将2个验证器(对于父类使用1个,对于chid类使用1个),最好将所有自定义验证(包括子类验证)放入父类验证器中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以覆盖子类中字段的Field约束 - Is it possible to override the Field constraint on a field in a child class 查找字段在 Java 中属于哪个类,父类还是子类? - Find field is of which class, parent or child in Java? 使用 hibernate-validator 基于另一个字段验证 Java 中的一个字段的最佳方法是什么? - What is the best way to Validate one field in Java based on another field using hibernate-validator? 从父类类型的字段访问子类方法 - Accessing child class method from parent-class-typed field 为什么子类对象在Java中调用父类字段变量? - Why is the child class object invoking the parent class field variable in Java? 动态验证基于另一个字段的json字段 - Validate a json field based on another field dynamically JAVA-扩展另一个类的子类的字段的设置值 - JAVA - Set value for field of child class extending another class 杰克逊注释基于另一个字段决定一个字段类 - Jackson Annotation decide on one field class based on another field 使用子类名访问父类静态字段不会加载子类? - Accessing parent class static field using child class name doesn't load the child class? 子类需要设置父类的唯一对象字段吗? - Child classes need to set unique object field of parent class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM