val tagForm = Form(
mapping(
"name" -> nonEmptyText, //unrelated field used to test form validation
"tags" -> list(boolean) // a list of checkboxes
)(TagForm.apply)(TagForm.unapply)
)
@form(action = routes.Application.submit) {
@inputText(testForm("name"), '_label -> "My name")
@repeat(testForm("tags")) { tag =>
@checkbox(tag)
}
<button type="submit">Submit</button>
}
val tagForm = Form(
mapping(
"name" -> nonEmptyText, //unrelated field used to test form validation
"tags" -> list(boolean), // a list of checkboxes
"tagNames" -> list(text) // names for the checkboxes
)(TagForm.apply)(TagForm.unapply)
)
@form(action = routes.Application.submit) {
@inputText(testForm("name"), '_label -> "My name")
@repeat(testForm("tags")) { tag =>
@* Easy access to the corresponding list of tagNames. *@
@defining(testForm(tag.name.replace("tags", "tagNames"))) { tagName =>
@* This is the checkbox we care about with a label from the list of tagNames *@
@checkbox(tag, '_label -> s"${tagName.value.get}")
@* Browsers only return checkboxes that are true. This fills in the false values. *@
<input type="hidden" name="@tag.name" value="false">
@* Return the tags so the form processing will fill them in to the completed form. *@
<input type="hidden" name="@tagName.name" value="@tagName.value">
}
}
<button type="submit">Submit</button>
}
- When I tried compiling this I received 10 "illegal start of type" errors that all involved "new ArrayList<>();". Taking the <> out made the errors go away.
- I notice that the forms lose data if there is an error. I see the comment in controllers.Application.postIndex that says "don't call formData.get, pass null instead." Why?