Chapter 6. Handling user input
This chapter covers
- Working with forms
- Data binding
- Using body parsers
- Validation
- Handling file uploads
In this chapter, we’ll explain in detail how users can interact with our application. This is where we’ll enable users to send data to our application. We’ll see how to handle different kinds of data and how to customize Play to use our own data types. We’ll also explain how to make sure the data sent is correct and, if it’s not, how to alert our users.
Working with forms in a web application involves two operations: displaying the form and processing the form submission. Forms allow users to send data to the server (our application). In Play, forms are represented by objects that you can manipulate. Play provides useful helpers to handle form submission. Because we don’t want users to send just any kind of data, Play makes sure the data is properly formatted and processed. If data isn’t well formatted, we need to tell the user what the problem is, so they can correct it and resubmit the form.
In our application, to submit a new product, we created two action methods: the first one, called newProduct(), displays the create a new product form, whereas the second, save(), handles the form submission. First we’ll take a closer look at what exactly the action method that shows the empty form does.
As a reminder, figure 6.1 shows the form.
As we’ve seen, displaying a form involves the following steps: