8

I've a problem using Enumeration in Grails: I try to use an enumeraion in a grails domain object

code:

    package it.xxx.tools.kanban

    import java.util.Date;

    class Task {

        String name
        String description

        Priority priority

 static belongsTo = [user:User, project:Project]

        static constraints = {
           name(nullable:false, maxSize:25)
           description(nullable:false, maxSize:1500)
           priority(nullable:true)
        }
    }

package it.xxx.tools.kanban;

public enum Priority {

 VERY_LOW("Very Low"),
 LOW("Low"),
 MEDIUM("Medium"),
 HIGH("High"),
 VERY_HIGH("Very High")

 private final String value

 Priority(String value){
  this.value = value;
 }

 String toString() {
  value
 }

 String getKey() {
  name()
 }

 static list(){
  [VERY_LOW, LOW, MEDIUM, HIGH, VERY_HIGH]
 }
}

<tr class="prop">
    <td valign="top" class="name">
    <label for="priority">Priority:</label>
    </td>
    <td valign="top" class="value                          ${hasErrors(bean:taskInstance,field:'priority','errors')}">
    <g:select from="${it.weservice.tools.kanban.Priority?.values()}" value="${taskInstance?.priority}" name="priority" noSelection="['':'']"></g:select>
    </td>
</tr>

I use the grails generate-all command

When I try to save via the web application the Task object I have the following error:

Failed to convert property value of type [java.lang.String] to required type [it.weservice.tools.kanban.Priority] for property priority; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [it.weservice.tools.kanban.Priority] for property priority: no matching editors or conversion strategy found
pbanfi
  • 1,885
  • 2
  • 19
  • 23

3 Answers3

7

Consulting the documentation:

The optionKey="key" in the g:select tag was missing.

Maicon Mauricio
  • 2,052
  • 1
  • 13
  • 29
pbanfi
  • 1,885
  • 2
  • 19
  • 23
3

You need to change the view, for example add optionValue="value" on the select in the create.gsp.

You don't need:

String toString() {
  value
}

String getKey() {
  name()
}

Yo need to have a getValue(), so change private final String value to final String value.

More info (in spanish): http://thewhitehatsolution.wordpress.com/

Rob Hruska
  • 118,520
  • 32
  • 167
  • 192
  • I'm sorry but I can't access the link you provided from my workplace I've been able to solve my problem following this: http://www.grails.org/TipsAndTricks I forgot the optionKey="key" in the g:select tag – pbanfi Dec 29 '09 at 08:52
2

If you're using scaffold = true in your controller, then you can also modify the template that generates the select tags in the views by editing the renderEditor.template file. To do this :

  1. grails install-templates
  2. Edit the file src/templates/scaffolding/renderEditor.template
  3. Edit the method renderEnumEditor(...) and add in optionKey=\"key\" to the select tag's options.
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Philippe
  • 6,703
  • 3
  • 30
  • 50