Grails
  1. Grails
  2. GRAILS-8343

NoSuchMethodError at Command Object Validation

    Details

      Description

      After upgrade from 1.3.7 to 2.0.0 RC1 i've got the following exception while using the command object validation feature:

      java.lang.NoSuchMethodError: UserFormCommand.validate()Z

      Obs.: It works after downgrade to 1.3.7.

      The following is the code i used:

      ---> Controller

      def create = { UserFormCommand cmd ->
      if(cmd.validate()) {
      User.withTransaction { status ->
      User user = new User(name: params.name, email: params.email, password: params.password)
      if (params.openId)

      { user.addToOpenIds(new OpenID(url: params.openId)) }

      user.setEnabled(true)
      user.setAccountLocked(false)
      user.password = securityService.encodePassword(user.password)
      user.save()

      UserRole.create user, Role.findByAuthority(Role.PROFESSIONAL)
      }
      }
      render createModelMessages(cmd) as JSON
      }

      ---> Command object

      class UserFormCommand {
      String email
      String name
      String password
      String confirmPassword

      static constraints = {
      email(nullable:false, blank:false)
      name(nullable:false, blank:false)
      password(size: 4..10)
      confirmPassword(size: 4..10, validator: { val, obj, errors ->
      if(!val.equals(obj.password))

      { obj.errors.rejectValue('confirmPassword', "userCommand.password.error.mismatch", "The password and confirmation field must match.") }

      })
      }
      }

        Activity

        Hide
        Juan Manuel Barroso added a comment -

        Hi,

        I'm using Grails 2.1.0-RC2 and I have this problem. If I put mi CommandObject in a .groovy file it doesn't work. When I put the command object code in controller this works fine.

        Is it possible that the problem occurred in version 2.1.0-RC2?

        Regards

        Show
        Juan Manuel Barroso added a comment - Hi, I'm using Grails 2.1.0-RC2 and I have this problem. If I put mi CommandObject in a .groovy file it doesn't work. When I put the command object code in controller this works fine. Is it possible that the problem occurred in version 2.1.0-RC2? Regards
        Hide
        Jeff Scott Brown added a comment -

        Juan,

        Using 2.1.0-RC2 if you create a controller that uses a command object which is defined under src/groovy and is not marked @Validateable, the compiler should issue a warning letting you know that the thing will not be validateable. The docs at http://grails.org/doc/2.1.0.RC2/guide/theWebLayer.html#commandObjects mention this.

        If a command object class is not defined in the same source file as a controller which uses the class as a command object and the class is not marked with Validateable, the class will not be made validateable. It is not required that command object classes be validateable.

        If your app includes something like this:

        grails-app/controllers/com/demo/DemoController.groovy
        package com.demo
        
        class DemoController {
        
            def index(DemoCommand co) { 
                render "It Worked"
            }
        }
        
        src/groovy/com/demo/DemoCommand.groovy
        package com.demo
        
        class DemoCommand {
            String name
        }
        

        The compiler should issue a warning like this:

        | Compiling 10 source files
        | Warning The [index] action accepts a parameter of type [com.demo.DemoCommand] which has not been marked with @Validateable.  Data binding will still be applied to this command object but the instance will not be validateable.
        
               def index(DemoCommand co) { 
               ^
        

        Is that what you are seeing?

        Show
        Jeff Scott Brown added a comment - Juan, Using 2.1.0-RC2 if you create a controller that uses a command object which is defined under src/groovy and is not marked @Validateable, the compiler should issue a warning letting you know that the thing will not be validateable. The docs at http://grails.org/doc/2.1.0.RC2/guide/theWebLayer.html#commandObjects mention this. If a command object class is not defined in the same source file as a controller which uses the class as a command object and the class is not marked with Validateable, the class will not be made validateable. It is not required that command object classes be validateable. If your app includes something like this: grails-app/controllers/com/demo/DemoController.groovy package com.demo class DemoController { def index(DemoCommand co) { render "It Worked" } } src/groovy/com/demo/DemoCommand.groovy package com.demo class DemoCommand { String name } The compiler should issue a warning like this: | Compiling 10 source files | Warning The [index] action accepts a parameter of type [com.demo.DemoCommand] which has not been marked with @Validateable. Data binding will still be applied to this command object but the instance will not be validateable. def index(DemoCommand co) { ^ Is that what you are seeing?
        Hide
        Juan Manuel Barroso added a comment -

        Ouch!! I was using the 'latest' documentation version (http://grails.org/doc/latest/), and this point to 2.0.4 versión where the command object usage is different.

        I see it's necessary mark with Validatable and not register the class. Now, It's working like a charm!

        Thank you very much!

        Show
        Juan Manuel Barroso added a comment - Ouch!! I was using the 'latest' documentation version ( http://grails.org/doc/latest/ ), and this point to 2.0.4 versión where the command object usage is different. I see it's necessary mark with Validatable and not register the class. Now, It's working like a charm! Thank you very much!
        Hide
        Jeff Scott Brown added a comment -

        Juan,

        Apologies for the confusion. The "latest" docs are for the latest released version of the framework, not snapshots and RCs. Once we release 2.1 the "latest" docs will point to that and the 2.0 docs will be available at http://grails.org/doc/2.0.x/ (they already are actually).

        I am glad you got it all working.

        Show
        Jeff Scott Brown added a comment - Juan, Apologies for the confusion. The "latest" docs are for the latest released version of the framework, not snapshots and RCs. Once we release 2.1 the "latest" docs will point to that and the 2.0 docs will be available at http://grails.org/doc/2.0.x/ (they already are actually). I am glad you got it all working.
        Hide
        Juan Manuel Barroso added a comment -

        Yes, It was my fault!! .. Thank you very much.

        Show
        Juan Manuel Barroso added a comment - Yes, It was my fault!! .. Thank you very much.

          People

          • Assignee:
            Jeff Scott Brown
            Reporter:
            Osvaldo Andrade
          • Votes:
            2 Vote for this issue
            Watchers:
            6 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:
              Last Reviewed:

              Development