Bean验证组序列不起作用 [英] Bean validation group sequence not working

查看:61
本文介绍了Bean验证组序列不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的项目中使用的是Spring 4.1,休眠验证器5.1.3.我一直在尝试让GroupSequence从最近2天开始工作.我已经提到了验证文档,博客以及在stackoverflow上发布的一些问题.

I'm using spring 4.1, hibernate validator 5.1.3 for my project. I've been trying to get the GroupSequence to work from last 2 days. I've referred the validation doc, blogs and a few questions posted on stackoverflow.

请参阅以下课程.当我从批注中删除GroupSequence和group时,所有验证消息会同时出现,即,对名称和其他字段的所有检查都会一起触发. 可以说一下名称字段-我希望首先验证@NotBlank和@Size,然后将名称与Pattern匹配,最后由于数据库调用而应检查@UniqueName.

Please see the below class. When I remove the GroupSequence and groups from the annotations, all the validation messages come up together, i.e, all the checks on name and other fields are fired together. Lets say for name field - I want @NotBlank and @Size to be validated first, then the name should be matched with the Pattern and at last it should be checked for @UniqueName because of the database calls.

为此,我按照文档和答案中的建议创建了GroupSequence.但是,当验证被触发时,只有@NotBlank和@Size被触发来命名.当我从其余注释中删除组值时,它们开始工作,但是所有错误消息都立即显示,这是我不希望的.

For that, I created the GroupSequence, as suggested in the docs and answers. But when the validations are fired, only @NotBlank and @Size are fired for name. When I remove the groups value from the remaining annotations, they start working but all the error messages are shown at once, which I don't want.

我希望用First.class组指定的注释一起触发并且在Second.class验证之前.我不明白为什么没有使用组指定的验证.

I want the annotation specified with groups First.class are fired together and before Second.class validations. I don't get why the validations specified with groups aren't getting fired.

有人可以引导我吗?

@GroupSequence({MyForm.class, OrderedChecks.class})
public class MyForm {

  @NotBlank
  @Size(min = 2, max = 40)
  @Pattern(regexp = "^[\\p{Alnum} ]+$", groups = First.class)
  @UniqueName(groups = Second.class)//Custom validation
  private String name;

  @NotBlank
  @Size(min = 2, max = 40)
  private String url;

  @NotBlank
  @Size(max = 100)
  private String imagePath;

  //Custom Validation
  @CheckContent(acceptedType = <someString>, allowedSize=<someval>, dimensions=<someval> groups = Second.class)
  private MultipartFile image

...
}

@GroupSequence(value = {Default.class, First.class, Second.class})
public interface OrderedChecks {}

@Controller
@RequestMapping(value = "/myForm")
public class MyFormController {

    @RequestMapping(method = POST)
    public String completeSignUp(@Valid @ModelAttribute("myForm") final MyForm myForm,
                            BindingResult result, RedirectAttributes redirectAttributes, Model model) {

        if(result.hasErrors()) {
            model.addAttribute(companyDetailsForm);
            //ERROR_VIEW="myForm";
            return ERROR_VIEW;
        }
       // Doing something else.
      return <success view>;
   }
}

推荐答案

使用Spring的@Validated代替@Valid.这将允许您指定组并控制顺序.

Use Spring's @Validated instead of @Valid. This will allow you specify the group and control the sequence.

将您的控制器方法更改为:

Change your controller method to:

@RequestMapping(method = POST)
public String completeSignUp(@Validated(OrderedChecks.class) @ModelAttribute("myForm") final MyForm myForm,
                        BindingResult result, RedirectAttributes redirectAttributes, Model model) {
...
}

请注意,在MyForm bean顶部不需要@GroupSequence({MyForm.class, OrderedChecks.class}).

Note that you don't need @GroupSequence({MyForm.class, OrderedChecks.class}) on top of MyForm bean.

这篇关于Bean验证组序列不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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