title: Enum in Grails Domain
body: <p>I'm trying to use an <code>enum</code> in a Grails 2.1 domain class.  I'm generating the controller and views via the <code>grails generate-all &lt;domain class&gt;</code> command, and when I access the view I get the error shown below.  What am I missing here?</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>Failed to convert property value of type java.lang.String to required type 
  com.domain.ActionEnum for property action; nested exception is 
  java.lang.IllegalStateException: Cannot convert value of type 
  [java.lang.String] to required type [com.domain.ActionEnum] for property action: 
  no matching editors or conversion strategy found
  </code></pre>
  
  <p><strong>Enum</strong>    (in <code>/src/groovy</code>)</p>
  
  <pre><code>package com.domain
  
  enum ActionEnum  {
      PRE_REGISTER(0), PURCHASE(2)
  
      private final int val
      public ActionEnum(int val) {
          this.val = val
      }
  
      int value() { return value }
  }
  </code></pre>
  
  <p><strong>Domain</strong></p>
  
  <pre><code>package com.domain
  
  class Stat {
      ActionEnum action
  
      static mapping = {
          version false
      }
  }   
  </code></pre>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;g:select name="action" 
      from="${com.domain.ActionEnum?.values()}"
      keys="${com.domain.ActionEnum.values()*.name()}" required="" 
      value="${xyzInstance?.action?.name()}"/&gt;
  </code></pre>
  
  <p><br/>
  <hr/>
  <strong>EDIT</strong></p>
  
  <p>Now getting error <code>Property action must be a valid number</code> after changing the following.</p>
  
  <p><strong>View</strong></p>
  
  <pre><code>&lt;g:select optionKey='id' name="action" 
  from="${com.domain.ActionEnum?.values()}" 
  required="" 
  value="${xyzInstance?.action}"/&gt;  // I tried simply putting a number here
  </code></pre>
  
  <p><strong>Enum</strong></p>
  
  <pre><code>package com.domain
  
  enum ActionEnum  {
      PRE_REGISTER(0), PURCHASE(2)
  
      final int id
      public ActionEnum(int id) {
          this.id = id
      }
  
      int value() { return value }
  
      static ActionEnum byId(int id) {
          values().find { it.id == id }
      } 
  }
  </code></pre>
  
  <p><strong>Domain</strong></p>
  
  <pre><code>package com.domain.site
  
  class Stat {
      static belongsTo = Game;
  
      Game game
      Integer action
  
      static mapping = {
          version false
     }
  
      static constraints = {
          action inList: ActionEnum.values()*.id
      }
  
      String toString() {
          return "${action}"
      }
  }
  </code></pre>