【发布时间】:2016-03-29 22:28:22
【问题描述】:
考虑下面的示例,它检查fromDate 和toDate 是否为有效日期,以及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}")
})
DateValidator 扩展了 FieldValidatorSupport,CompareDatesValidator 扩展了 ValidatorSupport
虽然我有shortCircuitDateValidators,但CompareDatesValidator总是运行,这是不正确的。我可以解决这个问题吗?!
【问题讨论】:
-
这是否回答了您的问题?来自文档:
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.. -
@AleksandrM 谢谢我已经看到了,但不幸的是它没有帮助。验证应该是短路的,但不是!
-
Plain validator takes precedence over field-validator.- 意味着您的CompareDatesValidator将首先执行。这意味着只有在那之后才会发生短路。将其转换为字段验证器,它应该可以按照您的意愿工作。 -
@AleksandrM 评论少,回答多 FTW! :)
-
我想因为我想在验证期间使用多个字段,所以我应该使用扩展
ValidatorSupport。好吧,我将CompareDatesValidator更改为扩展FieldValidatorSupport,但我从strust 得到Null pointer你需要stacktarce 吗?!
标签: java validation struts2 struts-validation struts2-convention-plugin