【发布时间】:2014-03-20 19:29:19
【问题描述】:
我在正确实施我自己的验证注释时遇到了一点问题。
这里是示例代码:
@Pattern(regexp="[0-9]*")
@Size(min=3, max=10)
@Constraint(validatedBy = SampleValidator.class)
@Documented
@Target({ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
public @interface MyAnnotation {
String message() default "Wrong!";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
@Documented
@interface List {
MyAnnotation[] value();
}
}
我的问题是,我希望我的注释能够以这样的方式工作,@Size 和 @Pattern 注释首先运行,如果它们有效,那么(并且只有在那时)运行由 @Constraint 注释提供的验证器(@ 987654325@)。
简单地说:
- 只有当
@Pattern和@Size有效时,我才想运行SampleValidator
(我将 SampleValidator 实现为仅应在某些特定情况下调用的附加验证器)
我怎样才能获得这种行为? 提前致谢!
显然,我不能这样做:
@GroupSequence({ Step1.class, Step2.class })
@Constraint(validatedBy = SampleValidator.class, groups = Step1.class) // no 'groups' attribute
@Size(min=3, max=10, groups = Step2.class)
@Pattern(regexp="[0-9]*", groups = Step2.class)
@Documented
@Target({ANNOTATION_TYPE, METHOD, FIELD, CONSTRUCTOR, PARAMETER})
@Retention(RUNTIME)
public @interface MyAnnotation {
...
}
【问题讨论】:
标签: bean-validation hibernate-validator