- 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.
Features
TheForm components offered by shadcn-svelte 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
ariaattributes to form fields based on states. - Enables you to easily use various components like Select, RadioGroup, Switch, Checkbox and other form components with forms.
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.src/routes/settings/schema.ts
Create form component
For this example, we’ll be passing the 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.src/routes/settings/settings-form.svelte
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.Use the component
We’ll pass the
form from the data returned from the load function to the form component we created above.src/routes/settings/+page.svelte
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 otherForm components: