Grails
  1. Grails
  2. GRAILS-8209

Command object inList constraint not available in a gsp

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Blocker Blocker
    • Resolution: Fixed
    • Affects Version/s: 2.0-RC1
    • Fix Version/s: 2.0-RC2
    • Component/s: View technologies
    • Labels:
      None
    • Environment:
      2.3GHz quad core processor with 4GB of memory running Win 7 x64. The environment is Java JDK version 1.7.0 with JAVA_OPTS set to -XX:MaxPermSize=256M -Xmx768M. GRAILS_HOME is set to version 2.0.0.RC1.

      Description

      Create an app with a domain of:

      class Book {
      
         String title
         String state
      
         static constraints = {
            title(blank: false, size: 1..40)
            state(inList: ['active', 'loginDisabled', 'actionsDisabled'])
         }
      }
      

      Create the controller and views

      Create a command object in src/groovy as follows:

      class Operation {
      
         String state
      
         static constraints = {
            state(inList: ['active', 'loginDisabled', 'actionsDisabled'])
         }
      }
      

      Add an action to the Book controller as follows:

      def operation(Operation operationInstance) {
         operationInstance.state = 'active'	// Any valid value will do
         return [operationInstance: operationInstance]
      }
      

      create an 'operation' gsp that includes a form and within the form add the following input field:

      <g:select id="state" name="state" from="${operationInstance.constraints.state.inList}" value="${operationInstance.state}"/>
      

      Run the application and insert a new Book. The select list for the book's state is correctly populated. Now browse to the book/operation page. You will get an error that boils down to:

      Caused by MissingPropertyException: No such property: state for class: test.Operation
      

      where 'test' just happened to be the name of my app and thus the default package name also.

        Activity

        Hide
        Burt Beckwith added a comment -

        The constraints block is a DSL that's parsed to infer the constraint rules. In your example "state(inList: ['active', 'loginDisabled', 'actionsDisabled'])" is actually a method call with name "state" taking a map of parameters. There's no direct way to get access to those parameters from the constraints block.

        Show
        Burt Beckwith added a comment - The constraints block is a DSL that's parsed to infer the constraint rules. In your example "state(inList: ['active', 'loginDisabled', 'actionsDisabled'] )" is actually a method call with name "state" taking a map of parameters. There's no direct way to get access to those parameters from the constraints block.
        Hide
        Paul Fernley added a comment -

        Burt,

        I'm somewhat confused. The RC1 User Guide for Command Objects still says that they are for when you require "data binding and validation". This code worked in Grails 1.3.7. Are you saying that this is a change in Grails 2.0 'cos, if so, I must have missed it in the 2.0 Upgrade Notes on the grails web site.

        Regards,

        Paul

        Show
        Paul Fernley added a comment - Burt, I'm somewhat confused. The RC1 User Guide for Command Objects still says that they are for when you require "data binding and validation". This code worked in Grails 1.3.7. Are you saying that this is a change in Grails 2.0 'cos, if so, I must have missed it in the 2.0 Upgrade Notes on the grails web site. Regards, Paul
        Hide
        Burt Beckwith added a comment -

        Sorry, you're right - this should work

        Show
        Burt Beckwith added a comment - Sorry, you're right - this should work

          People

          • Assignee:
            Jeff Scott Brown
            Reporter:
            Paul Fernley
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development