Forms
On the web forms are a common way for users to interact with your application by performing state mutations. torque
provides a number of utilities for working with forms and validating form data.
Throughout this section we will be using the following HTML form as an example:
When a user submits this form, a POST request is made to the configured /signup
endpoint. The request body will contain the current value for each field nested in the
:
Decoding form data
torque
provides a convenient utility for decoding form data into structs.
The following example is a module that handles a torque.Action
as an HTTP POST request to the /signup
endpoint.
The SignupForm
struct is used to decode and store the incoming form data. You can use the json
struct tag to map the struct fields to the form input field names.
Multi-part forms
It is possible to handle multi-part forms within a module's Action
. This is useful for handling things like file uploads.
The following example is a RouteModule
capable of handling an Action that allows users to upload a new avatar photo.