排除@Valid验证某些领域 [英] exclude some fields from @Valid validation

查看:597
本文介绍了排除@Valid验证某些领域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的验证bean的领域它与 javax.constraints 的注解,Spring的 @Valid 注释。

I use Spring's @Valid annotation for validate bean's fields which annotated with javax.constraints annotations.

不过,我面临的一个问题,当我需要排除验证(仅适用于某些情况下)的一些领域。

But I faced a problem when I need to exclude some fields from validation (only for some cases).

我做了一个调查,并没有发现任何有用的方式和大多数的答案被作为日2010-2011。这是很令人惊讶,因为这situatuion是如此普遍。

I did an investigation didn't found any usefull ways and most of answers were dated as 2010-2011. It is quite surprisingly since this situatuion is so common.

是否有任何变化,时代对Spring 4 +?或者,也许任何人都可以分享个人的经验如何击败这个?

Is there any changes from that times for Spring 4.+? Or maybe anyone can share personal experience how to beat this?

感谢。

推荐答案

您可以使用验证组和<一个href=\"http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/validation/annotation/Validated.html\"相对=nofollow> @Validated 注释。

You can use validation group, and @Validated annotation.

有在 HTTP一个细节的解释://www.java$c$cgeeks.com/2014/08/validation-groups-in-spring-mvc.html

该方法是基于<一个href=\"https://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-usingvalidator.html#validator-usingvalidator-validationgroups\"相对=nofollow> Hibernate验证团体以及它使您能够将验证基于标记接口上并将其应用在处理器的水平,例如在链接给

The approach is based on hibernate validator's groups and it enables you to group the validation based on the marker interface and apply it on handler level, example as given in the link

@RequestMapping(value = "stepTwo", method = RequestMethod.POST)
public String stepTwo(@Validated(Account.ValidationStepTwo.class) Account account, Errors errors) {
  if (errors.hasErrors()) {
      return VIEW_STEP_TWO;
  }
  return "redirect:summary";
}

这篇关于排除@Valid验证某些领域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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