如何设置JSR303 Bean验证的检查顺序 [英] How to set check order for JSR303 bean validation

查看:151
本文介绍了如何设置JSR303 Bean验证的检查顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用JSR303 Bean验证来检查表单输入.

I use the JSR303 Bean Validation to check the form input.

@NotBlank
@Size(min = 4, max = 30)
private String name;

@NotBlank
@Size(max = 100)
@Email
private String mail;

当name =''和email ="时,@NotBlank,@Size为@NotBlank, @ size,@ Email会被检查.

when then name = '' and email = '',the @NotBlank, @Size at name, @NotBlank, @Size, @Email at mail will be checked.

我要设置检查顺序,例如,当上一个顺序无效时,不检查下一个.

I want set the check order, when the previous order is invalid, the next is not checked, for example.

@NotBlank(order = 1)
@Size(min = 4, max = 30, order = 2)
private String name;

(JSR303不支持以上功能)

(above is not support by JSR303)

是否可以使用JSR303来实现它? (我认为自定义注释会完成,但我不喜欢为每个属性添加自定义注释)

is there a way to implement it for using JSR303? (i think the custom annotation will done, but i don't like to add a custom annotation for every property)

我也考虑了验证组和验证组的顺序,但是我认为我的需求有所不同.

And I also consider the validation group and group sequence, but i think there are some difference for my need.

@NotBlank(groups = First.class)
@Size(min = 4, max = 30, groups = Second.class)
private String name;

@NotBlank(groups = First.class)
@Size(max = 100, groups = Second.class)
@Email(groups = Third.class)
private String mail;

当名称为空并且邮件为"abc"时. JSR303将只检查名称上的@NotBlank和邮件上的@NotBlank,因为名称上的@NotBlank无效,因此不会检查第二和第三组.

When the name is blank and the mail is "abc". Only the @NotBlank at name, and @NotBlank at mail will be checked by JSR303, Because the @NotBlank at name is invalid, so the Second and Third group is not be checked.

但是我需要的是@NotBlank的名称,并且@ NotBlank,@ Size,@ Email的邮件应进行检查.由于name处的@NotBlank无效,因此不会检查name处的@Size,而mail处的@ NotBlank,@ Size是有效的,因此会检查mail处的@Email.

But my need is @NotBlank at name and @NotBlank, @Size, @Email at mail should be checked. Because the @NotBlank at name is invalid, so the @Size at name is not be checked, and the @NotBlank,@Size at mail is valid, so the @Email at mail is be checked.

也许,下面的伪代码很清楚地描述了我的要求.

Perhaps, the below pseudocode will be clear to describe my requirement.

// retrieves all fields and check one by one.
for (Field field : fields) {
  List annotations = getJsr303Annotations(field);
  // retrieves all annotations on field and check one by one.
  for (Annotation annotation : annotations) {
    CheckResult result = check(annotation, field);
    if (result.isValid()) {
       continue;
    } else {
       errors.add(result);
       break;
    }
  }
}

是否有一种很好的方法来实现JSR303的要求?

Is there a good way to implement my requirement by JSR303?

推荐答案

是的,Bean验证支持此功能.它称为验证组和组序列.组是简单的标记接口,例如,您可以创建两个接口 First Second 并将代码更改为以下形式:

Yes Bean Validation supports this feature. It is called validation groups and group sequences. Groups are simple marker interfaces, for example you could create the two interfaces First and Second and change your code to something like this:

@NotBlank(groups = First.class)
@Size(min = 4, max = 30, groups = Second.class)
private String name;

然后您可以定义一个组序列:

Then you can define a group sequence:

@GroupSequence({ First.class, Second.class})
interface All {
}

获取 Validator 并调用validateator.validate(myObject,All.class)

Get hold of the Validator and call validator.validate(myObject, All.class)

全部在文档中.

这篇关于如何设置JSR303 Bean验证的检查顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆