Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form data in Play Framework

I found this neat post before asking this question (but doesn't solve my problem) :

I'm trying to update a record with ajax call using play framework as a backend.

Here is some data regarding my request :

Request URL:http://172.20.12.50:9000/updateName
Request Method:PUT

Form Data
name=&value=Testttt&pk=367

enter image description here

Here is how I try to test what I get on the server side :

Logger.info("PK IS " + request().getQueryString("pk"));

This is what I get in the log:

[info] application - PK IS null

How would I get these params from FormData? I got this data regarding my request from firebug

like image 753
Gandalf StormCrow Avatar asked Aug 27 '13 15:08

Gandalf StormCrow


1 Answers

Adapted from http://www.playframework.com/documentation/2.0/ScalaForms

import play.api.data._
import play.api.data.Forms._

case class MyFormData(name: Option[String], value: String, pk: Long)

val myForm = Form(
    mapping(
        "name" -> optional(text),
        "value" -> text,
        "pk" -> number
    )(MyFormData.apply)(MyFormData.unapply)
)

myForm.bindFromRequest.fold(
    formWithErrors => // binding failure, you retrieve the form containing errors,
    value => // binding success, you get the MyFormData value 
)

Obviously substitute MyFormData for something meaningful to your domain.

like image 68
Chris Beach Avatar answered Sep 28 '22 12:09

Chris Beach