Exclude some fields from @Valid check

I am using Spring @Valid annotation to validate bean fields annotated with javax.constraints annotations.

But I had a problem when I need to exclude some fields from the scan (only for some cases).

I conducted an investigation and did not find any useful ways, and most of the answers were dated 2010-2011. This is rather unexpected since this situation is so common.

Are there any changes from now on for Spring 4. +? Or maybe someone can share their personal experience, how to defeat this?

Thanks.

+5
source share
1 answer

You can use the validation group and @Validated annotation.

Detailed explanation at http://www.javacodegeeks.com/2014/08/validation-groups-in-spring-mvc.html

The approach is based on hibernation validator groups and allows you to group a check based on the marker interface and apply it at the processor level, an example indicated 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"; } 
+6
source

All Articles