I’m using Play 2.0 with Scala and I’d like to extract some values that are stored in hidden fields.
The form in the controller:
val recommendationForm: Form[Recommendation] = Form(
mapping(
"fromMail" -> email,
"fromName" -> nonEmptyText(5, 30),
"toMail" -> email,
"toName" -> nonEmptyText(5, 30),
"message" -> optional(text(0, 300)),
"productGroupId" -> nonEmptyText(),
"masterProductId" -> nonEmptyText()
)
(Recommendation.apply)(Recommendation.unapply)
)
def doRecommend = xyzAction { implicit request =>
recommendationForm.bindFromRequest.fold(
errors => {
errors.data.map(x => println(x))
BadRequest("")
},
{
case (recommendation) => {
Ok("")
}
}
)
}
The Recommendation case class:
case class Recommendation(fromMail: String, fromName: String, toMail: String, toName: String, message: Option[String], productGroupId: String, masterProductId: String)
The HTML:
@form(action = routes.Product.doRecommend) {
<input type="hidden" value="@{(productGroup \ "@id").text}" />
<input type="hidden" value="@{(currentMasterProduct \ "@id").text}" />
@inputText(field = recommendationForm("fromName"),
'_label -> "",
'placeholder -> Messages("recommendation.from.name"),
'_showConstraints -> false,
'class -> "text ui-widget-content ui-corner-all"
)
some more fields...
}
Generated HTML of hidden field:
<input type="hidden" value="74.75.652">
But I’m unable to extract those two hidden fields from the errors. It only prints the other five ones. How can I extract the hidden ones?
All I had to do was to give the input field a name attribute…