Play中的嵌套表单! Scala 2.2

时间:2015-02-13 20:13:19

标签: scala nested-forms playframework-2.2

我已经阅读了几次文档,但我在Play中的嵌套表单仍然存在问题! Scala 2.2(详细进一步详述)。

这是我的表格:

<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())

该错误可能来自html部分,因为我已经跟随此页面中的文档:play! scala forms documentation 但我无法弄清楚。

1 个答案:

答案 0 :(得分:3)

input的字段名称应使用Form中的完整路径。 label informations中显示Mappinginformations是序列,而不是label,因此您应使用informations[0].label代替label[0] 1}}。

您的观点应如下所示:

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>