Play! framework ENUM and Groovy problem

I have something like the following -

Woman.java

... @Entity public class Woman extends Model { public static enum Outcome { ALIVE, DEAD, STILL_BIRTH, LIVE_BIRTH, REGISTER } ... } 

File.java

 ... @Entity public class Form extends Model { ... public Outcome autoCreateEvent; ... } 

Create.html

 #{select "autoCreateEvent", items:models.Woman.Outcome.values(), id:'autoCreateEvent' /} 

Saves the ENUM value in DB, which is normal. But, when I reboot / edit, the problem comes up. Since it uses ALIVE, DEAD, etc. As a value for the parameters, therefore, it cannot display the list correctly.

Any insight?

+4
source share
2 answers

If I understand your question correctly, you want to use valueProperty and labelProperty to set the correct values ​​in option . Sort of:

 #{select "autoCreateEvent", items:models.Woman.Outcome.values(), valueProperty:'ordinal', labelProperty: 'name', id:'autoCreateEvent' /} 

EDIT:

To do this, you will need to slightly modify the enumeration, for example:

 public enum Outcome { A,B; public int getOrdinal() { return ordinal(); } } 

The reason is that Play # {select} expects getters in the valueProperty and labelProperty , and when the default value is not found, enum toString

+3
source

To add to the previous answer, add this to the Enum declaration:

 public String getLabel() { return play.i18n.Messages.get(name()); } 

Be sure to use the following declaration:

 #{select "[field]", items:models.[Enum].values(), valueProperty:'name', labelProperty: 'label' /} 

You can also add this to Enum:

  @Override public String toString() { return getLabel(); } 

Which will be useful if you want to display the internationalized value in your view file (since toString is called automatically when it is displayed), but the name of the function () uses toString (), so you have to bind valueProperty to another function, as follows:

 public String getLabel(){ return toString(); } public String getKey() { return super.toString(); } @Override public String toString() { return Messages.get(name()); } 

And select # select:

 #{select "[field]", items:models.[Enum].values(), value:flash.[field], valueProperty:'key', labelProperty: 'label' /} 
+1
source

All Articles