Mewel
Mewel

Reputation: 1279

Integer enum and g:select

I have an Enumeration like this:

public enum MyEnum {
  Apple (1)
  Microsoft (2)
  IBM (4)
  Intel (8)

  int company

  MyEnum(int company) {
    this.company = company
  }
}

And I want a g:select box looking like this (the integer values are important in the value attribute):

<select>
  <option value="1">Apple</option>
  <option value="2">Microsoft</option>
  <option value="4">IBM</option>
  <option value="8">Intel</option>
</select>

Ok thats no problem using the g:select:

<g:select name="myenum" from="${MyEnum?.values()*.company}" />

But when I try to save the form I always get: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [MyEnum] for property myenum: no matching editors or conversion strategy found

How can I resolve this?

Upvotes: 0

Views: 1189

Answers (2)

Mewel
Mewel

Reputation: 1279

Now I use a simple integer with the inList constrain instead of the enum. Its not the same but solves my problem.

class MyDomain {
    int company
    static constraints = {
        company(inList: [1, 2, 4, 8])
    }
}

In the form: <g:select valueMessagePrefix="company" name="company" from="${MyDomain.constraints.company.inList}" value="${myDomainInstance?.company}"/>

Now I need to use the i18n files (messages.properties):

company.1=Apple
company.2=Microsoft
company.4=IBM
company.8=Intel

Upvotes: 0

Jack
Jack

Reputation: 133609

Try with:

public enum MyEnum {
  Apple (1)
  Microsoft (2)
  IBM (4)
  Intel (8)

  int company

  MyEnum(int company) {
    this.company = company
  }

  String toString() { return company }
  String getKey() { name() }
}

and then modify the tag with

<g:select name="myenum" from="${MyEnum?.values()*.company}" optionKey="key" />

Upvotes: 2

Related Questions