Binding enum to form <select> element in Play! Framework 2.1

java, playframework, playframework-2.0

Solution

Something like this in your template should work:

<select name="contactType">
    @for(cType <- ContactType.values()){
        <option value="@cType.id">@cType.name()</option>
    }
</select>

Note: it may be better to use `toString()` instead of `name()`. If you override `toString()` in your enum you could return Contractor instead of CONTRACTOR.

Note 2: if your enum is not in the `models` package you need to prefix it with the right package name i.e.: `@for(cType <- com.my_company.enums.ContactType)`

Problem

I'm trying to figure out best practice to bind `enum` to form drop-down `<select>` in Play! 2.0 Here is my enum: ``` public enum ContactType { CLIENT(1), CONTRACTOR(2), SUPPLIER(3); public final int id; ContactType(int id) { this.id = id; } } ``` And here's what i'd like to get as result in my view: ``` <select name="contactType"> <option value="1">CLIENT</option> <option value="2">CONTRACTOR</option> <option value="3">SUPPLIER</option> </select> ```

Original source