2012-03-14 25 views
4

我有一個包含一些行的表單,每個表單中都有一個複選框。用戶可以選擇其中一些,然後按「刪除選定的行」按鈕進行提交。如何在play2中獲取發佈的「id = 1&id = 2」?

張貼的數據是這樣的:

id=1&id=2&id=3 

我想他們在行動上,我的代碼是:

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

但我發現IDS總是List(),一個空列表。

我已經檢查由play2提供的「樣品表」,發現seq(...)只能用這樣的格式發佈數據的工作:

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

請注意,有許多[0]或其他指標的參數名。

回答

4

在這種情況下,您可能(也可能想要)訪問請求主體的url編碼內容,而不是使用Form幫助程序。

做到這一點的方式是例如: -

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 
    } 
} 
3

這是在Play2有嚴格限制,目前。

整個表單綁定框架基於從/到Map[String,String]的翻譯,並且對於FormUrlEncoded和Json輸入,這是通過丟棄除了每個鍵的第一個值之外的所有內容來完成的。

我正在嘗試將所有內容都更改爲Map[String,Seq[String]],但目前尚不清楚該方法的兼容性如何。查看https://github.com/bartschuller/Play20/tree/formbinding正在進行的工作(分支將在沒有警告的情況下強行推送)。

批評,API建議和測試歡迎。

0

如果你讓你發佈的數據看起來像

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

它應該工作。

相關問題