Scala 使用play应用程序/表单url编码的正文解析器在表单中显示多个字段
我有一张表格Scala 使用play应用程序/表单url编码的正文解析器在表单中显示多个字段,scala,playframework-2.1,Scala,Playframework 2.1,我有一张表格 <form action="/action" method="post"> <input type="text" name="email[1]" /> <input type="text" name="email[2]" /> <input type="text" name="email[3]" /> <button type="submit">Submit</submit> </for
<form action="/action" method="post">
<input type="text" name="email[1]" />
<input type="text" name="email[2]" />
<input type="text" name="email[3]" />
<button type="submit">Submit</submit>
</form>
这个代码是我收到的
Map(email[1] -> List(test@test.com), email[3] -> List(), email[2] -> List(test@test.com))
而不是
Map( email -> Map( 1 -> List(test@test.com), 3 -> List(), 2 -> List(test@test.com) )
我需要提取这些数字,因为它们指向数据库中的一些内部ID
问题:如何提取这些数字?我现在看到的唯一方法是对地图名称进行模式匹配。也许有更好的选择
谢谢我认为更好的解决方案是定义表单,然后根据请求绑定表单。 试试这样:
import play.api.mvc.Action
import play.api.data._
import play.api.data.Forms._
val form = Form(
mapping(
"email[1]" -> text,
"email[2]" -> text,
"email[3]" -> text
)(_ :: _ :: _ :: Nil) {
case first :: second :: third :: Nil => Option((first, second, third))
case _ => None
}
)
def myAction = Action{
implicit request =>
form.bindFromRequest().fold(errors => BadRequest, data => Ok)
}
我不能硬编码这些字段,它们应该是动态的。本表格旨在为客户提供所有可能的联系信息。可能的联系信息来自另一个DB表。但您的回答实际上让我知道了我可以尝试做什么:
import play.api.mvc.Action
import play.api.data._
import play.api.data.Forms._
val form = Form(
mapping(
"email[1]" -> text,
"email[2]" -> text,
"email[3]" -> text
)(_ :: _ :: _ :: Nil) {
case first :: second :: third :: Nil => Option((first, second, third))
case _ => None
}
)
def myAction = Action{
implicit request =>
form.bindFromRequest().fold(errors => BadRequest, data => Ok)
}