Formsnap & Superforms
Building forms with Formsnap, Superforms, & Zod.
Forms are tricky. They are one of the most common things you'll build in a web application, but also one of the most complex.
Well-designed HTML forms are:
- Well-structured and semantically correct.
- Easy to use and navigate (keyboard).
- Accessible with ARIA attributes and proper labels.
- Has support for client and server side validation.
- Well-styled and consistent with the rest of the application.
In this guide, we will take a look at building forms with formsnap, sveltekit-superforms and zod.
Features
The Form
components offered by svell
are wrappers around formsnap
& sveltekit-superforms
which provide a few things:
- Composable components for building forms.
- Form field components for scoping form state.
- Form validation using Zod or any other validation library supported by Superforms.
- Applies the correct
aria
attributes to form fields based on states. - Enables you to easily use various components like Select, RadioGroup, Switch, Checkbox and other form components with forms.
If you aren't familiar with Superforms & Formsnap, you should check out their documentation first, as this guide assumes you have a basic understanding of how they work together.
Anatomy
Example
Installation
Usage
Create a form schema
Define the shape of your form using a Zod schema. You can read more about using Zod in the Zod documentation. We're going to define it in a file called schema.ts
in the same directory as our page component, but you can put it anywhere you like.
Return the form from the route's load function
Create a form component
For this example, we'll be passing the form
returned from the load function as a prop to this component. To ensure it's typed properly, we'll use the SuperValidated
type from sveltekit-superforms
, and pass in the type of our form schema.
The name
, id
, and all accessibility attributes are applied to the input by spreading the attrs
object from the Form.Control
component. The Form.Label
will automatically be associated with the input using the for
attribute, so you don't have to worry about that.
Create a page component that uses the form
We'll pass the form
from the data returned from the load function to the form component we created above.
Create an Action that handles the form submission
Done
That's it. You now have a fully accessible form that is type-safe and has client & server side validation.
Next Steps
Be sure to check out the Formsnap and Superforms documentation for more information on how to use them.
Examples
See the following links for more examples on how to use the other Form
components:
On This Page