@Entitypublic class Company {
...
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
public List<CompanyPhone> phones;...
}
@Column(name = "PHONE_TYPE")public PhoneType type;public enum PhoneType { @EnumValue("MAI") MAIN, @EnumValue("MOB") MOBILE, @EnumValue("FAX") FAX, @EnumValue("CUS") CUSTOM;
public static Map<String, String> options() { LinkedHashMap<String, String> options = new LinkedHashMap<String, String>(); for (PhoneType v : PhoneType.values()) { try { EnumValue a = v.getClass().getField(v.name()).getAnnotation(EnumValue.class); options.put(a.value(), Messages.get(String.format("%s.%s", PhoneType.class.getSimpleName(), v.name()))); } catch (NoSuchFieldException | SecurityException e) { /* ignore */ } } return options;}}public class Global extends GlobalSettings {
@Override
public void onStart(Application app) { Formatters.register(PhoneType.class, new PhoneTypeFormatter());
}
...
}public class PhoneTypeFormatter extends SimpleFormatter<PhoneType> { @Override
public PhoneType parse(String input, Locale locale) throws ParseException { PhoneType phoneType = null; for (PhoneType v : PhoneType.values()) { try { EnumValue a = v.getClass().getField(v.name()).getAnnotation(EnumValue.class); if (input != null && a != null && input.equals(a.value())) { phoneType = v; break; } } catch (NoSuchFieldException | SecurityException e) { /* ignore */ } } return phoneType; }
@Override
public String print(PhoneType phoneType, Locale locale) { String v = null; try { v = (phoneType.getClass().getField(phoneType.name()) .getAnnotation(EnumValue.class)).value(); } catch (NoSuchFieldException | SecurityException e) { /* ignore */ } return v; }}...
@phoneGroup(field: Field, className: String = "companyPhone") = { <div class="control-group @className"> <label class="control-label" for="@field("type").id">@Messages("company.phoneNumbers")</label> <div class="controls"> <select id="@field("type").id" name="@field("type").name" class="input-small">
@for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null && value == field("type").value) { selected }>@text</option> } </select> <input type="text" class="inputAreaCode" id="@field("areaCode").id" name="@field("areaCode").name" value="@field("areaCode").value" placeholder="@Messages("phone.areaCode")"> <input type="text" class="input-medium" id="@field("number").id" name="@field("number").name" value="@field("number").value" placeholder="@Messages("phone.number")"> <a class="removePhone btn btn-danger">@Messages("button.remove")</a> </div> </div>}
...
@main {
...
<div class="companyPhones well"> @repeat(companyForm("phones"), min = 1) { phone => @phoneGroup(phone)
}
@** * Keep the hidden block that will be used as template for Javascript copy code. **@ @phoneGroup( companyForm("phones[x]"), className = "companyPhone_template") <div class="manage_repeat"> <a class="addPhone btn btn-success">@Messages("phone.add")</a> </div> </div>...
}
@phoneGroup(field: Field, className: String = "companyPhone") = {
... <select id="@field("type").id" name="@field("type").name" class="input-small">
@for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null && value == field("type").value) { selected }>@text</option> } </select>
...}
@phoneGroup(field: Field, className: String = "companyPhone") = {
...
<span>Selected type: @field("type").value</span>
<select id="@field("type").id" name="@field("type").name" class="input-small">
@for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null && value == field("type").value) { selected }>@text</option> } </select>
...}...
<span>Selected type: FAX</span>
<select id="phones_0__type" name="phones[0].type" class="input-small"> <option value="MAI">Main</option>
<option value="MOB">Mobile</option>
<option value="FAX">Fax</option>
<option value="CUS">Custom</option>
</select>
...@phoneGroup(field: Field, className: String = "companyPhone") = {
...
<span>Selected type: @field("type").value</span>
<select id="@field("type").id" name="@field("type").name" class="input-small">
@for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null) { @field("type").value }>@text</option> } </select>
...}....
<span>Selected type: FAX</span>
<select id="phones_0__type" name="phones[0].type" class="input-small"> <option value="MAI" fax>Main</option>
<option value="MOB" fax>Mobile</option>
<option value="FAX" fax>Fax</option>
<option value="CUS" fax>Custom</option>
</select>
...value == field("type").value
Just guessing, but if these are strings, try value.equals(field("type").value)
<select id="@field("type").id" name="@field("type").name" class="input-small"> @for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null && value.equals(field("type").value)) { selected }>@text</option> }</select> <input type="radio" id="@(id)_@v._1" name="@name" value="@v._1" @(if(value == Some(v._1)) "checked" else "") @toHtmlArgs(htmlArgs)>
<select id="@field("type").id" name="@field("type").name" class="input-small"> @for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").value != null && Some(value) == (field("type").value)) { selected }>@text</option> }</select><select id="@field("type").id" name="@field("type").name" class="input-small"> @for((value, text) <- models.PhoneType.options) { <option value="@value" @if(field("type").exists(_ == value)) { selected }>@text</option> } </select><select id="@field("type").id" name="@field("type").name" class="input-small"> @options(models.PhoneType.options).map { v => <option value="@v._1" @if(Some(v._1) == (field("type").value)) { selected }>@v._2</option> } </select><select id="@field("type").id" name="@field("type").name" class="input-small"> @for((value, text) <- models.PhoneType.options) { <option value="@value" @if(Some(value) == (field("type").value)) { selected }>@text</option> } </select>