Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-6373

Support for validation groups in @Valid used by spring MVC

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: 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
          juergen.hoeller 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 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
          bennyflint Ben Flint added a comment -

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

          Show
          bennyflint Ben Flint added a comment - In 3.1.0.RELEASE, I believe the spring-specific annotation is @Validated, not @Valid.
          Hide
          birnbuazn 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
          birnbuazn 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 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 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
          dellasavia Daniel Della Savia added a comment - Hi, Sam. Take a look in this link http://stackoverflow.com/questions/18911154/how-to-specify-validation-group-for-valid

            People

            • Assignee:
              juergen.hoeller Juergen Hoeller
              Reporter:
              pavla Pavla Nováková
              Last updater:
              Daniel Della Savia
            • Votes:
              54 Vote for this issue
              Watchers:
              54 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                1 week, 6 days ago