4

I have a form which contains some rows, each has a checkbox in the leading. User can select some of them, then press "delete selected rows" button to submit.

The posted data looks like:

id=1&id=2&id=3

I want to get them in action, my code is:

def delete = Action { implicit request =>
   Form("id"->seq(nonEmptyText)).bindFromRequest.fold(
      errors => BadRequest, 
      ids => {
         println(ids)    // (!)
         for(id<-ids) deleteRow(id)
      }
   )
}

But I found the ids was always List(), an empty List.

I've checked the "Form samples" provided by play2, and found seq(...) should only worked with posted data with such format:

company sdfdsf
firstname   sdfds
informations[0].email   sdf@sdf.com
informations[0].label   wef
informations[0].phones[0]   234234
informations[0].phones[1]   234234
informations[0].phones[x]   
informations[1].email   sdf@sdf.com
informations[1].label   wefwef
informations[1].phones[0]   234234
informations[1].phones[x]   
informations[x].email   
informations[x].label   
informations[x].phones[x]   

Please notice that there are many [0] or other indexes in the parameter names.

4

3 回答 3

4

Form在这种情况下,您可以(并且可能想要)访问请求正文的 url 编码内容,而不是使用帮助程序。

这样做的方法是:

def delete = Action { implicit request =>
  request.body.asFormUrlEncoded match {
    case Some(b) =>
      val ids = b.get("id")
      for(id <- ids) deleteRow(id)
      Ok
    case None =>
      BadRequest
  }
}
于 2012-03-14T11:22:06.340 回答
3

目前,这在 Play2 中是一个硬限制。

整个表单绑定框架基于从/到转换Map[String,String]以及对于 FormUrlEncoded 和 Json 输入的转换,这是通过丢弃除每个键的第一个值之外的所有内容来完成的。

我正在尝试将所有内容更改为,Map[String,Seq[String]]但目前尚不清楚该方法的兼容性如何。有关正在进行的工作,请参阅https://github.com/bartschuller/Play20/tree/formbinding (分支将在没有警告的情况下被强制推送)。

欢迎批评、API 建议和测试。

于 2012-03-14T22:50:40.287 回答
0

如果您使发布的数据看起来像

id[0]=1&id[1]=2&id[2]=3

它应该工作。

于 2012-04-16T05:41:04.023 回答