Spring 基于注解的验证中的 And/Or 条件
我正在使用基于 Spring 3 注解的验证。我想为字符串字段添加以下验证
字段可以为空或它应该包含一个非空字符串
我知道注释如 @Null
、@NotEmpty
但我如何将两者一起使用或者条件?
解决方案:
使用@Size(min=1)
有帮助,但它不能处理空格。因此添加了一个自定义注释 NotBlankOrNull
,它将允许 null
和非空字符串,并且它还会处理空格。非常感谢@Ralph。
这是我的注释
@Documented
@Constraint(validatedBy = { NotBlankOrNullValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface NotBlankOrNull {
String message() default "{org.hibernate.validator.constraints.NotBlankOrNull.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
验证器类
public class NotBlankOrNullValidator implements ConstraintValidator<NotBlankOrNull, String> {
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if ( s == null ) {
return true;
}
return s.trim().length() > 0;
}
@Override
public void initialize(NotBlankOrNull constraint) {
}
}
,我还在我的 网站< /a>.
I am using Spring 3 annotation based validation. I want to add a following validation for String fields
Field can be Null OR it should contain a non empty string
I know annotation like @Null
, @NotEmpty
but how I can use both with a OR condition?
Solution:
Using @Size(min=1)
helps but it don't handle spaces. So added a custom annotation NotBlankOrNull
which will allow null
and non empty strings also it takes care of blank spaces. Thanks a lot @Ralph.
Here is my Annotation
@Documented
@Constraint(validatedBy = { NotBlankOrNullValidator.class })
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER })
@Retention(RUNTIME)
public @interface NotBlankOrNull {
String message() default "{org.hibernate.validator.constraints.NotBlankOrNull.message}";
Class<?>[] groups() default { };
Class<? extends Payload>[] payload() default { };
}
Validator class
public class NotBlankOrNullValidator implements ConstraintValidator<NotBlankOrNull, String> {
public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
if ( s == null ) {
return true;
}
return s.trim().length() > 0;
}
@Override
public void initialize(NotBlankOrNull constraint) {
}
}
I have also updated it on my site.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,它不是基于 Spring 注解的验证,而是 JSR 303 Bean 验证,例如由 Hibernate Validation 实现。它确实与 spring 无关/
你不能以 OR 方式组合注释*。
但是对于 not null 约束有一个简单的解决方法,因为最基本的验证接受 null 作为有效输入(因此,如果您想要有一个“正常”行为,但您经常需要结合基本验证和额外的 @NotNull )不是你要求的)。
例如:
@javax.validation.constraints.Size
接受 null 作为有效输入。因此,在您的情况下,您需要使用
@Size(min=1)
而不是@NotEmpty
。顺便说一句:Not
@NotEmpty
只是@NotNull
和@Size(min = 1)
的组合*除非你自己实现它。
First of all, it is not Spring annotation based validation, it is JSR 303 Bean Validation, implemented for example by Hibernate Validation. It is really not spring related/
You can not combine the annotations in an OR way*.
But there is a simple workaround for the not null constraint, because the most basic validations accept null as an valid input (therefore you often need to combine the basic vaidations and an extra @NotNull, if you want to have a "normal" behavior but not what you asked for).
For Example:
@javax.validation.constraints.Size
accept null as an valid input.So what you need in your case is use
@Size(min=1)
instead of@NotEmpty
.BTW: Not
@NotEmpty
is just an combination of@NotNull
and@Size(min = 1)
*except you implement it by your self.