我正在使用Play 2.0与Scala,我想提取一些存储在隐藏字段中的值。
控制器中的表单:
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("")
}
}
)
}
建议案例类:
case class Recommendation(fromMail: String, fromName: String, toMail: String, toName: String, message: Option[String], productGroupId: String, masterProductId: String)
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...
}
生成隐藏字段的HTML:
<input type="hidden" value="74.75.652">
但是我无法从错误中提取这两个隐藏字段。它只打印其他五个。如何提取隐藏的?
答案 0 :(得分:0)
我所要做的就是给输入字段一个名字属性......
<input type="hidden" value="@{(productGroup \ "@id").text}" name="@recommendationForm("productGroupId").name" />