Play中的嵌套表格! Scala 2.2 [英] Nested form in Play! Scala 2.2
问题描述
我已经阅读了几次文档,但是在Play中的嵌套表单仍然有问题! Scala 2.2(进一步详细说明).
I have read several times the documentation but I still have problems with my nested form in Play! Scala 2.2 (detailed further).
这是我的表格:
<form method="post" action="/admin/test2">
first name : <input type="text" name="firstname"> <br>
info 0 : <input type="text" name="label[0]"> <br>
info 1 : <input type="text" name="label[1]"> <br>
<input type="submit" value="test nested form" />
</form>
和相应的案例类:
case class Contact(firstname: String,
informations: Seq[ContactInformation])
case class ContactInformation(label: String)
val contactForm: Form[Contact] = Form(
mapping(
"firstname" -> nonEmptyText,
"informations" -> seq(
mapping(
"label" -> nonEmptyText
)(ContactInformation.apply)(ContactInformation.unapply)
)
)(Contact.apply)(Contact.unapply)
)
def saveContact = Action { implicit request =>
contactForm.bindFromRequest.fold(
formWithErrors => Ok("error"),
contact => {
println(contact)
Ok("Done")
}
)
}
我没有收到任何错误,但是我从表单(用println(contact)
打印)上获得的联系人的信息字段为空,即,它看起来像这样:Contact(h,List())
I don't get any errors, but the contact that I get from the form (printed with println(contact)
) has an empty informations field, i.e. it looks like this : Contact(h,List())
该错误可能来自html
部分,因为我已仔细阅读了此页面上的文档:播放! Scala表单文档
但我不知道.
The error comes probably from the html
part since I have followed to the letter the documentation from this page : play! scala forms documentation
but I can’t figure it out.
推荐答案
input
的字段名称应使用Form
中的完整路径. label
出现在informations
Mapping
中,并且informations
是序列,而不是label
,因此您应该使用informations[0].label
而不是label[0]
.
The field names of the input
s should use the full path in the Form
. label
appears within the informations
Mapping
, and informations
is the sequence, not the label
, so you should use informations[0].label
instead of label[0]
.
您的视图应如下所示:
first name : <input type="text" name="firstname"> <br>
info 0 : <input type="text" name="informations[0].label"> <br>
info 1 : <input type="text" name="informations[1].label"> <br>
这篇关于Play中的嵌套表格! Scala 2.2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!