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)), ...
Get Play Framework Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.