How to specify validation group for @Valid?

user2740224 picture user2740224 · Sep 20, 2013 · Viewed 13.2k times · Source

I get so parameters in @Controller @RequestMapping method:

@ModelAttribute("myCandidate") @Valid Candidate myCandidate,
BindingResult result

How can I explicit specify validation group for myCandidate ?

Answer

John Farrelly picture John Farrelly · Sep 20, 2013

The standard java @Valid annotation doesn't support groups. However, Spring now includes an @Validated annotation which does the same job as @Valid, but allows you to specify which groups to use in the validation:

@ModelAttribute("myCandidate") @Validated(UpdateGroup.class) Candidate myCandidate

Note that this annotation is only available in Spring 3.1 and newer.