Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get form parameter value from request in Play 2.0 Scala controller

In Play 2.0 Scala application I have simple page with Form with one parameter. It redirects to another page, where I want to do something with parameter from the form. How can I get it?

I'm looking for something like

request.formData.get("paramName")

I know request.body, but still don't know how to get single parameter value from it.

like image 642
amorfis Avatar asked Feb 02 '12 00:02

amorfis


2 Answers

I'd say that the easyest way to retrieve forms data is to use the Form structure in play.api.data. So here is how you could do it in play2.0-rc1

val form = Form[(String, String)](
  tuple(
    "paramName1" -> nonEmptyText,
    "paramName2" -> nonEmptyText
  )
)

form.bindFromRequest.fold(
  failure => (),//do smthg with the failure info
  { case (p1, p2) => println(p1);println(p1)}
)

Instead of using nonEmptyText you might use of[String].

Check what is put in your hands for that mapping here Forms Helper. Some other information that should help you further are here.

like image 61
Andy Petrella Avatar answered Nov 03 '22 22:11

Andy Petrella


If a post request with following may work

request().body().asFormUrlEncoded().get("myparam")[0];

like image 21
user2324323 Avatar answered Nov 03 '22 22:11

user2324323