O'Reilly logo

Play Framework Essentials by Julien Richard-Foy

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Reading and validating HTML form data

If you try to submit the form, you get an error because the data submitted by your form is not sent to the browser as a JSON blob, as expected by your current Items.create action. Indeed, web browsers send the form data as application/x-www-form-urlencoded content. So, we have to update our action code to handle this content type instead of JSON.

Handling the HTML form submission

The form model you use to produce the HTML form can also be used to process the request body of a form submission. Change the Items.create action as follows:

val create = Action(parse.urlFormEncoded) { implicit request =>
  createItemFormModel.bindFromRequest().fold(
    formWithErrors => BadRequest(views.html.createForm(formWithErrors)), ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required