4

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' /}

It saves ENUM value in DB, which is OK. But, when I reload/edit then the problem rises. Because it uses ALIVE, DEAD, etc. as the value for options so it can't show the list properly.

Any Insight?

Jonas
  • 121,568
  • 97
  • 310
  • 388
Rifat
  • 7,628
  • 4
  • 32
  • 46

2 Answers2

3

If I understand your question properly you want to use the valueProperty and labelProperty to set the proper values in the option. Something like:

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

EDIT:

For this to work you will need to tweak the enum a bit, like this:

public enum Outcome {
  A,B;

  public int getOrdinal() {
     return ordinal();
  }

}

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

Pere Villega
  • 16,429
  • 5
  • 63
  • 100
  • Hi, Thanks for your reply and I thought it should work but it doesn't. Can you please go to this link and search for 'valueProperty', they have commented that part - http://svn.codehaus.org/grails-plugins/grails-filterpane/tags/RELEASE_0_7/grails-app/taglib/com/zeddware/grails/plugins/filterpane/FilterTagLib.groovy – Rifat Aug 09 '11 at 04:55
  • @rifat you gave a link to grails, not play framework... what error do you get? – Pere Villega Aug 09 '11 at 09:19
  • no error! but it gives wrong output whereas I expected – Rifat Aug 09 '11 at 11:14
  • though I'm still in trouble but what you said is OK :) – Rifat Aug 11 '11 at 07:38
1

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

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

Make sure to use the following declaration:

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

You can also add this in the 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 being called automatically when displayed) but function name() uses toString() so you will have to bind valueProperty to another function, as follow:

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

public String getKey() {
    return super.toString();
}

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

And the #select use:

#{select "[field]", items:models.[Enum].values(), value:flash.[field], valueProperty:'key', labelProperty: 'label' /}
Apokai
  • 249
  • 2
  • 9