vendredi 17 avril 2015

Play Framework 2.3.x mapping a checkbox to a boolean field

Using Java Play Framework 2.3.7, I have a custom view template for a form with a text field and a select dropdown. This is how I was able to get my custom view to work. Now, I need to add a checkbox to this view template. I am having trouble getting the checkbox to map properly to a boolean attribute. I've looked at this question but it seems to be for an older version of Play, so it isn't helpful to me.


I have a class CSVData which contains a list of type CSVField. Here are the attributes for these classes:



public class CSVData{

private String name;
private String description;
private String dataFilePath;
private List<CSVField> fields;
private Double latitude;
private Double longitude;


// rest of class... }


and



public class CSVField {
private String name;
private String type;
private boolean unique;

...}


In my controller I have this



Form<CSVData> formData = Form.form(CSVData.class).bindFromRequest();


In my view template I have this (most bootstrap styling removed for brevity):



@helper.repeat(csvForm("fields"), min=1) { csvField =>
@fieldGroup(csvField, optionMap = myTypes)
}


Where @fieldGroup is the following:



@fieldGroup(csvField: Field, optionMap: List[String]) = {
<div class="multi-field">
<input type="text"
class="form-control"
name="@csvField("name").name"
value="@csvField("name").value.getOrElse("")"/>

<select
class="form-control"
id="select-type"
name="@csvField("type").name"
>
<option class="blank" value="">Select type</option>
@for((optionName) <- optionMap) {
<option id="@optionName" value="@optionName" @if(csvField("type").value.equals(Some(optionName))) {selected} >
@optionName
</option>
}
</select>

<div class="checkbox">
<label>
<input type="checkbox"
name="@csvField("unique").name"
value=@csvField("unique").value.getOrElse(true)> Unique
</label>
</div>
<button type="button" class="btn btn-danger">Delete</button>
</div>
}


This ends up looking like fieldGroup


However, when I fill in my form and submit it, I get a Persistence Exception. I think the issue is with the value=@csvField("unique").value.getOrElse(true) portion of the checkbox. How can I set up the checkbox value properly so it maps to my boolean attribute?


PS - Here is the stacktrace of the exception that I am seeing


Aucun commentaire:

Enregistrer un commentaire