Spring Framework
  1. Spring Framework
  2. SPR-6373

Support for validation groups in @Valid used by spring MVC

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Complete
    • Affects Version/s: 3.0 RC2
    • Fix Version/s: 3.1 GA
    • Component/s: Web
    • Labels:
      None
    • Last commented by a User:
      true

      Description

      It would be useful to be able to use validation groups in spring MVC as specified by JSR-303. With the improvement it should be possible to write something like:

      @RequestMapping("/foo", method=RequestMethod.POST)
      public void processFoo(@Valid(groups=

      {Default.class, MyValidationGroup.class}

      ) Foo foo)

      { ... }

        Issue Links

          Activity

          Hide
          Sam Brannen added a comment -

          Hi Pavla,

          That's a very good point. In fact, I had a similar idea.

          Note, however, that @javax.validation.Valid does not support the groups attribute you proposed.

          Thus, since @Valid is a part of the JSR 303 standard, Spring would have to provide a custom (Spring) annotation (or similar mechanism) to enable configuration of validation groups.

          cheers,

          Sam

          Show
          Sam Brannen added a comment - Hi Pavla, That's a very good point. In fact, I had a similar idea. Note, however, that @javax.validation.Valid does not support the groups attribute you proposed. Thus, since @Valid is a part of the JSR 303 standard, Spring would have to provide a custom (Spring) annotation (or similar mechanism) to enable configuration of validation groups. cheers, Sam
          Hide
          Pavla Nováková added a comment -

          Hi Sam,

          what you suggest - custom Spring annotation - is exactly what I mean. Yes, I'm aware that @javax.validation.Valid cannot be used for this purpose.

          Best regards, Pavla

          Show
          Pavla Nováková added a comment - Hi Sam, what you suggest - custom Spring annotation - is exactly what I mean. Yes, I'm aware that @javax.validation.Valid cannot be used for this purpose. Best regards, Pavla
          Show
          David J. M. Karlsen added a comment - You might want to observe https://issues.apache.org/jira/browse/BVAL-83 and http://opensource.atlassian.com/projects/hibernate/browse/HV-347 .
          Hide
          Cemo Koc added a comment -

          I started to feel that this issue never will be addressed.

          Show
          Cemo Koc added a comment - I started to feel that this issue never will be addressed.
          Hide
          Juergen Hoeller added a comment -

          A related discussion over there at the JSR-303 EG: https://hibernate.onjira.com/browse/BVAL-208
          We are watching these discussions closely, not least of it all since our use of @Valid in Spring MVC triggered them.

          As for a solution for declarative group validation in Spring itself, we intended to wait on the JSR-303 EG going some such way for Bean Validation 1.1... However, the path is still not clear there, except maybe for the fact that @Valid itself won't see a groups attribute any time soon.

          In any case, we do notice that this is a very popular Spring issue with many votes. Chances are that we'll sneak a solution into Spring 3.1 GA, or possibly 3.1.1.

          Juergen

          Show
          Juergen Hoeller added a comment - A related discussion over there at the JSR-303 EG: https://hibernate.onjira.com/browse/BVAL-208 We are watching these discussions closely, not least of it all since our use of @Valid in Spring MVC triggered them. As for a solution for declarative group validation in Spring itself, we intended to wait on the JSR-303 EG going some such way for Bean Validation 1.1... However, the path is still not clear there, except maybe for the fact that @Valid itself won't see a groups attribute any time soon. In any case, we do notice that this is a very popular Spring issue with many votes. Chances are that we'll sneak a solution into Spring 3.1 GA, or possibly 3.1.1. Juergen
          Hide
          Juergen Hoeller added a comment -

          Good news: This has finally been added in time for 3.1 GA, through a validation 'hint' mechanism in our SmartValidator interface and a custom @Valid annotation in our validation package. Validation groups can simply be specified through using that custom @Valid annotation like as follows:

          @Valid(MyGroup.class)

          Juergen

          Show
          Juergen Hoeller added a comment - Good news: This has finally been added in time for 3.1 GA, through a validation 'hint' mechanism in our SmartValidator interface and a custom @Valid annotation in our validation package. Validation groups can simply be specified through using that custom @Valid annotation like as follows: @Valid(MyGroup.class) Juergen
          Hide
          Ben Flint added a comment -

          In 3.1.0.RELEASE, I believe the spring-specific annotation is @Validated, not @Valid.

          Show
          Ben Flint added a comment - In 3.1.0.RELEASE, I believe the spring-specific annotation is @Validated, not @Valid.
          Hide
          Stefan Haberl added a comment -

          IMHO the new @Validated annotation works great, but I would have loved to be able to query the validation group that caused a bean validation to fail, which I think is still not possible: I would like to define a parameter in my method signature a long the lines of

          @Validated(Errors.class, Warnings.class) BindingResult result

          and then in the method body

          if (result.hasErrors(Warnings.class))

          {...}

          I know I can work around this using more BindingResults in my method signature, but this is verbose and the long parameter lists soon turn ugly.

          Show
          Stefan Haberl added a comment - IMHO the new @Validated annotation works great, but I would have loved to be able to query the validation group that caused a bean validation to fail, which I think is still not possible: I would like to define a parameter in my method signature a long the lines of @Validated(Errors.class, Warnings.class) BindingResult result and then in the method body if (result.hasErrors(Warnings.class)) {...} I know I can work around this using more BindingResults in my method signature, but this is verbose and the long parameter lists soon turn ugly.
          Hide
          Perit Bezek added a comment -

          Spring 3.1 may not support what you ask, but I believe this can be done by implementing a custom HandlerMethodArgumentResolver. You may need to create a custom annotation though, if the default resolver can handle @Validated BindingResult result type argument.

          Show
          Perit Bezek added a comment - Spring 3.1 may not support what you ask, but I believe this can be done by implementing a custom HandlerMethodArgumentResolver. You may need to create a custom annotation though, if the default resolver can handle @Validated BindingResult result type argument.

            People

            • Assignee:
              Juergen Hoeller
              Reporter:
              Pavla Nováková
              Last updater:
              Trevor Marshall
            • Votes:
              54 Vote for this issue
              Watchers:
              53 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 year, 47 weeks, 5 days ago