简体   繁体   English

如何短路@CustomValidator?

[英]How to short-circuit a @CustomValidator?

Consider below sample, which checks if fromDate and toDate are valid dates and if fromDate is less than toDate :考虑下面的示例,它检查fromDatetoDate是否是有效日期以及fromDate是否小于toDate

@CustomValidator(type = "DateValidator", 
            fieldName = "fromDate",
         shortCircuit = true),

@CustomValidator(type = "DateValidator", 
            fieldName = "toDate",
         shortCircuit = true),

@CustomValidator(type = "CompareDatesValidator", 
              message = "validate.date.jalali.same.or.before",
         shortCircuit = true, 
           parameters = {
        @ValidationParameter(name = "fromDateParam", value = "${fromDate}"),
        @ValidationParameter(name = "toDateParam", value = "${toDate}") 
               })

The DateValidator extends the FieldValidatorSupport and the CompareDatesValidator extends the ValidatorSupport DateValidator扩展了FieldValidatorSupportCompareDatesValidator扩展了ValidatorSupport

Although I have shortCircuit the DateValidator s, but the CompareDatesValidator always run, which is not correct.虽然我有shortCircuit DateValidator s,但CompareDatesValidator总是运行,这是不正确的。 Can I fix this ?!我可以解决这个问题吗?!

As explained in the documentation .文档中所述

Plain validator takes precedence over field-validator.普通验证器优先于字段验证器。 They get validated first in the order they are defined and then the field-validator in the order they are defined.它们首先按照定义的顺序进行验证,然后按照定义的顺序进行字段验证器。 Failure of a particular validator marked as short-circuit will prevent the evaluation of subsequent validators and an error (action error or field error depending on the type of validator) will be added to the ValidationContext of the object being validated.标记为短路的特定验证器的失败将阻止对后续验证器的评估,并且错误(动作错误或字段错误取决于验证器的类型)将添加到正在验证的对象的 ValidationContext 中。

Then your actual execution order is:那么你的实际执行顺序是:

  1. CompareDatesValidator (plain)比较日期验证器(普通)
  2. DateValidator (field fromDate )日期验证器(字段fromDate
  3. DateValidator (field toDate ) DateValidator (字段toDate

The problem is that it will be executed first, but since its check is a composite check based on two fields, the atomic check on the fields itself should be performed first.问题是它会先执行,但由于它的检查是基于两个字段的复合检查,所以应该先对字段本身进行原子检查。

But this is how the framework works, so you need to workaround it .但这就是框架的工作方式,因此您需要解决

If your plain validator is still this one (even if with some modification ), you could avoid the check and ignore the error in case the input is not valid, letting this validation happen where it belongs, in the Field Validators:如果您的普通验证器仍然是这个(即使进行了一些修改),您可以避免检查并忽略错误,以防输入无效,在字段验证器中让此验证发生在它所属的地方:

public final class CompareDatesValidator extends ValidatorSupport {
    private String fromDate; // getter and setter
    private String toDate;   // getter and setter    

    @Override
    public void validate(Object o) throws ValidationException {
        Date d1 = (Date)parse(fromDate, Date.class);
        Date d2 = (Date)parse(toDate, Date.class);

        if (d1==null || d2==null){
            LOG.debug("Silently disabling Plain Validator. Check performed by Field ones");
        } else if (d2.before(d1)){
            addActionError(getDefaultMessage());
        }
    }
}

You only need to remember to always put the Field Validators in the same validation stack of the CompareDatesValidator , or the "Date not valid" errors will be silently swallowed.您只需要记住始终将字段验证器放在CompareDatesValidator的同一验证堆栈中,否则“日期无效”错误将被默默吞下。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM