JSR-303/Spring MVC-使用组有条件地进行验证 [英] JSR-303 / Spring MVC - validate conditionally using groups

查看:113
本文介绍了JSR-303/Spring MVC-使用组有条件地进行验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我提出了一个概念,可以有条件地使用JSR 303组进行验证. 有条件地"表示我有一些仅在另一个字段具有特定值时才相关的字段.

I worked out a concept to conditionally validate using JSR 303 groups. "Conditionally" means that I have some fields which are only relevant if another field has a specific value.

示例:有一个选项可以选择是注册为个人还是公司.选择公司时,用户必须填写包含公司名称的字段.

Example: There is an option to select whether to register as a person or as a company. When selecting company, the user has to fill a field containing the name of the company.

现在我想我为此使用分组:

Now I thought I use groups for that:

  class RegisterForm
  {
     public interface BasicCheck {}
     public interface UserCheck {}
     public interface CompanyCheck {}

     @NotNull(groups = BasicCheck.class)
     private Boolean isCompany

     @NotNull(groups = UserCheck.class)
     private String firstName;

     @NotNull(groups = UserCheck.class)
     private String lastName;

     @NotNull(groups = CompanyCheck.class)
     private String companyName;

     // getters / setters ...
  }

在我的控制器中,我会根据各自的选择逐步进行验证:

In my controller, I validate step by step depending on the respective selection:

  @Autowired
  SmartValidator validator;

  public void onRequest(@ModelAttribute("registerForm") RegisterForm registerForm, BindingResult result)
  {
     validator.validate(registerForm, result, RegisterForm.BasicCheck.class);
     if (result.hasErrors()
        return;
     // basic check successful => we can process fields which are covered by this check
     if (registerForm.getIsCompany())
     {
        validator.validate(registerForm, result, RegisterForm.CompanyCheck.class)
     }
     else
     {
        validator.validate(registerForm, result, RegisterForm.UserCheck.class);
     }
     if (!result.hasErrors())
     {
        // process registration
     }
  }

我只想验证必须验证的内容.如果用户选择公司",则使用无效内容填充字段,然后切换回用户",验证器必须忽略与公司相关的无效内容.一种解决方案是使用Javascript清除这些字段,但我也希望我的表单在禁用javascript的情况下工作.这就是为什么我完全喜欢上面显示的方法的原因.

I only want to validate what must be validated. If the user selects "company" fills a field with invalid content and then switches back to "user", the invalid company related content must be ignored by the validator. A solution would be to clear those fields using Javascript, but I also want my forms to work with javascript disabled. This is why I totally like the approach shown above.

但是由于数据绑定,Spring打破了这个想法.在验证开始之前,Spring会将数据绑定到registerForm.例如,如果类型不兼容(预期的int值,但用户用字母填充表格),则会在result中添加错误.这是一个问题,因为这些错误在JSP视图中由<form:errors />标记

But Spring breaks this idea due to data binding. Before validation starts, Spring binds the data to registerForm. It adds error to result if, for instance, types are incompatible (expected int-value, but user filled the form with letters). This is a problem as these errors are shown in the JSP-view by <form:errors /> tags

现在,我找到了一种方法,可以通过实现自定义的BindingErrorProcessor来防止Spring将这些错误添加到绑定结果中.如果字段包含null,我知道存在验证错误.在我的概念中,不允许null-每个字段都用@NotNull加上相应的验证组进行注释.

Now I found a way to prevent Spring from adding those errors to the binding result by implementing a custom BindingErrorProcessor. If a field contains null I know that there was a validation error. In my concept null is not allowed - every field gets annotated with @NotNull plus the respective validation group.

当我刚接触Spring和JSR-303时,我想知道自己是否完全走错了道路.我必须自己执行一些操作,这一事实使我不确定.这是一个干净的解决方案吗?我认为这是一个普遍的问题,对于相同的问题是否有更好的解决方案?

As I am new to Spring and JSR-303 I wonder, whether I am totally on the wrong path. The fact that I have to implement a couple of things on my own makes me uncertain. Is this a clean solution? Is there a better solution for the same problem, as I think this is a common problem?

编辑

如果您对我的解决方案感兴趣,请在这里查看我的答案: https://stackoverflow.com/a/30500985/395879

Please see my answer here if you are interested in my solution in detail: https://stackoverflow.com/a/30500985/395879

推荐答案

您是正确的,Spring MVC在这方面有点挑剔,这是一个普遍的问题.但是有解决方法:

You are correct that Spring MVC is a bit picky in this regard,and it is a common problem. But there are work-arounds:

  • 使所有后备字段成为字符串,并手动进行数字/日期等转换和空值检查.
  • 使用JavaScript将不相关的字段设置为null.
  • 输入字段时,使用JavaScript验证字段.这将解决您几乎所有的问题.

祝你好运!

这篇关于JSR-303/Spring MVC-使用组有条件地进行验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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