java - Binding enum to form <select> element in Play! Framework 2.1 -
i'm trying figure out best practice bind enum
form drop-down <select>
in play! 2.0
here enum:
public enum contacttype { client(1), contractor(2), supplier(3); public final int id; contacttype(int id) { this.id = id; } }
and here's i'd result in view:
<select name="contacttype"> <option value="1">client</option> <option value="2">contractor</option> <option value="3">supplier</option> </select>
something in template should work:
<select name="contacttype"> @for(ctype <- contacttype.values()){ <option value="@ctype.id">@ctype.name()</option> } </select>
note: may better use tostring()
instead of name()
. if override tostring()
in enum return contractor instead of contractor.
note 2: if enum not in models
package need prefix right package name i.e.: @for(ctype <- com.my_company.enums.contacttype)
Comments
Post a Comment