Learn how to take off with Formsnap by building a settings form.
Installation
Since Formsnap is built on top of Superforms, you'll need to install it as well as a schema validation library of your choice. We'll use Zod.
Tutorial: Build a settings form
Before diving into this tutorial, it's important to be confident with Superforms, as Formsnap is built on top of it and uses the same APIs.
Define a Zod schema
This schema will represent the shape of our form data. It's used to validate the form data on the client (optional) and server, along with some other useful things.
Looking at the schema above, we know we'll need a few different input types to represent the different data types. Here's how we'll map the schema to input types:
Of course, there are other ways to represent the data, but this is the approach we'll take for this tutorial.
Return the form from a load function
In Superforms fashion, we'll return the form from a load function to seamlessly merge our PageData and ActionData.
Setup the form in the page component
Now that we have our form in the PageData object, we can use it, along with the schema we defined earlier, to setup the form in our page component.
We'll initialize the super form using superForm and pass in the form from the PageData. We'll also enable client-side validation by passing the validators option. Then, we'll setup the form using the enhance function, which will progressively enhance the form with client-side validation and other features.
Constructing a form field
You can think of form fields as the building blocks of your form. Each property of the schema will have a corresponding form field, which will be responsible for displaying the error messages and description.
We'll start with the email field and work our way down.
We pass the form and name to the Field component, which will be used to setup the context for the field. The name is typed to the keys of the schema, so it's type-safe.
Now let's add the remaining parts of the field:
We've first added the Control component. Controls are used to represent a form control and its label. They keep the control and label in sync via the attrs slot prop, which is spread onto the control. Inside the Control, we've added the Label component, which will automatically associate itself with the control the attrs are spread onto. We've also added the control itself, which is an input that we're binding to the email property of the form data.
The Description component is optional, but it's useful for providing additional context to the user about the field. It'll be synced with the aria-describedby attribute on the input, so it's accessible to screen readers.
The FieldErrors component is used to display validation errors to the user. It also is synced with the aria-describedby attribute on the input, which can receive multiple IDs, so that screen readers are able to read the error messages in addition to the description.
And that's really all it takes to setup a form field. Let's continue on with the rest of the fields.
Add remaining form fields
You may have noticed for the allergies and theme fields, we used the Fieldset and Legend components. These are used to group related fields together and provide a title for the group, which is great for accessibility and organization. Additionally, we only use a single FieldError and Description component for the entire group, and use an Control for each field in the group to associate the label with the control.
And that's it! You've now successfully built a settings form with Formsnap!
Next Steps
Now that you've built your first form, you're ready to start building more complex forms with Formsnap & Superforms. Be sure to check out the rest of the documentation to learn more about the different components and APIs available to you.