How to get published "id = 1 & id = 2" in action, in play2?

I have a form that contains several lines, each of which has a checkbox in the sheet. The user can select some of them, and then click the "delete selected lines" button to send.

Published data is as follows:

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 that identifiers are always List() , an empty list.

I checked the "Sample Forms" provided by play2 and found that seq(...) should only work with published data with this 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] 

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

+4
source share
3 answers

Instead of using the Form helper in this case, you can (and probably want to) access the content encoded in the request body URL.

A way to do this, for example:

 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 } } 
+4
source

This is a tight limitation in Play2, for the time being.

The whole structure of the form binding is based on translating from / to Map[String,String] , and to enter both FormUrlEncoded and Json this is done by throwing everything except the first value for each key.

I am experimenting with changing everything to Map[String,Seq[String]] , but it is not yet clear how compatible this approach is. See https://github.com/bartschuller/Play20/tree/formbinding for current work (the branch will be forced to push without warning).

Criticism, API suggestions and tests are welcome.

+3
source

If you make your published data look like

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

It should work.

0
source

Source: https://habr.com/ru/post/1401340/


All Articles