Top Related Projects
web development, streamlined
lix (change control system) && inlang (globalization ecosystem for software built on lix)
Easily add integrations and other functionality to Svelte(kit) apps
web development for the rest of us
🤖 Headless, performant, and type-safe form state management for TS/JS, React, Vue, Angular, Solid, and Lit.
📋 React Hooks for form state management and validation (Web + React Native)
Quick Overview
SvelteKit Superforms is a library that enhances form handling in SvelteKit applications. It provides a robust solution for client and server-side form validation, offering a seamless experience for developers working with forms in SvelteKit projects.
Pros
- Seamless integration with SvelteKit and its form actions
- Supports both client-side and server-side validation
- Provides TypeScript support for improved type safety
- Offers a simple API for complex form handling scenarios
Cons
- Requires additional setup and configuration compared to native form handling
- May have a learning curve for developers new to the library
- Could potentially increase bundle size for smaller applications
- Limited to SvelteKit projects, not usable in vanilla Svelte applications
Code Examples
- Basic form setup:
import { superForm } from 'sveltekit-superforms/client';
import type { SuperValidated } from 'sveltekit-superforms';
import { z } from 'zod';
const schema = z.object({
email: z.string().email(),
password: z.string().min(8)
});
export const load = async () => {
const form = await superForm(schema);
return { form };
};
- Form validation in a Svelte component:
<script lang="ts">
import { superForm } from 'sveltekit-superforms/client';
export let data;
const { form, errors, enhance } = superForm(data.form);
</script>
<form method="POST" use:enhance>
<input name="email" bind:value={$form.email}>
{#if $errors.email}<span class="error">{$errors.email}</span>{/if}
<input name="password" type="password" bind:value={$form.password}>
{#if $errors.password}<span class="error">{$errors.password}</span>{/if}
<button>Submit</button>
</form>
- Server-side form handling:
import { superValidate } from 'sveltekit-superforms/server';
export const actions = {
default: async ({ request }) => {
const form = await superValidate(request, schema);
if (!form.valid) {
return fail(400, { form });
}
// Process the valid form data
return { form };
}
};
Getting Started
-
Install the library:
npm install sveltekit-superforms zod
-
Create a schema and load function in your route file:
import { superForm } from 'sveltekit-superforms/client'; import { z } from 'zod'; const schema = z.object({ /* your schema */ }); export const load = async () => { const form = await superForm(schema); return { form }; };
-
Use the form in your Svelte component:
<script lang="ts"> import { superForm } from 'sveltekit-superforms/client'; export let data; const { form, errors, enhance } = superForm(data.form); </script> <form method="POST" use:enhance> <!-- Your form fields here --> </form>
Competitor Comparisons
web development, streamlined
Pros of Kit
- Core SvelteKit framework with comprehensive documentation and community support
- Integrated routing, server-side rendering, and API endpoints
- Regular updates and maintenance from the Svelte core team
Cons of Kit
- Steeper learning curve for beginners
- Requires more setup and configuration for form handling
- Less specialized features for form management and validation
Code Comparison
Kit (basic form handling):
<form method="POST">
<input name="username" />
<button>Submit</button>
</form>
export const actions = {
default: async ({ request }) => {
const data = await request.formData();
// Process form data
}
};
Sveltekit-superforms:
<script>
import { superForm } from 'sveltekit-superforms/client';
const { form, errors, enhance } = superForm(data);
</script>
<form method="POST" use:enhance>
<input name="username" bind:value={$form.username} />
{#if $errors.username}<span>{$errors.username}</span>{/if}
<button>Submit</button>
</form>
Sveltekit-superforms provides a more streamlined approach to form handling with built-in validation and error management, while Kit offers a more flexible but less specialized foundation for building web applications.
lix (change control system) && inlang (globalization ecosystem for software built on lix)
Pros of monorepo
- Comprehensive monorepo structure for managing multiple packages
- Includes a wide range of tools and configurations for a full-stack application
- Supports multiple frameworks and technologies in one repository
Cons of monorepo
- More complex setup and maintenance compared to a single-purpose library
- Steeper learning curve for developers new to monorepo structures
- May include unnecessary components for projects that don't require all features
Code Comparison
sveltekit-superforms:
import { superForm } from 'sveltekit-superforms/client';
import type { SuperForm } from 'sveltekit-superforms';
export const { form, errors, enhance } = superForm(data);
monorepo:
import { createTRPCRouter } from '@/server/api/trpc';
import { exampleRouter } from '@/server/api/routers/example';
export const appRouter = createTRPCRouter({
example: exampleRouter,
});
The code snippets highlight the different focus areas of the two projects. sveltekit-superforms is specifically designed for form handling in SvelteKit applications, while monorepo provides a more general-purpose structure for full-stack applications, including API routing and other features.
Easily add integrations and other functionality to Svelte(kit) apps
Pros of svelte-add
- Broader scope: Adds various integrations to Svelte projects, not limited to form handling
- Customizable: Allows selecting specific features to add
- Community-driven: Accepts contributions for new integrations
Cons of svelte-add
- Less specialized: May not provide as deep form functionality as Superforms
- Potentially more complex setup: Requires choosing and configuring multiple integrations
- Maintenance challenges: With many integrations, keeping all up-to-date can be difficult
Code Comparison
svelte-add (adding TailwindCSS):
npx svelte-add@latest tailwindcss
Superforms (basic setup):
import { superForm } from 'sveltekit-superforms/client';
const { form, errors, enhance } = superForm(data);
While svelte-add focuses on adding various integrations to Svelte projects, Superforms specializes in form handling for SvelteKit. svelte-add offers flexibility and a wide range of options, but Superforms provides a more focused and potentially more robust solution for form management. The choice between them depends on whether you need a general-purpose tool for adding features or a specialized form handling library.
web development for the rest of us
Pros of Svelte
- Core framework with broader application beyond form handling
- Larger community and ecosystem for general Svelte development
- More comprehensive documentation and learning resources
Cons of Svelte
- Lacks specialized form handling features out of the box
- Requires additional setup and custom code for advanced form functionality
- May need third-party libraries for complex form validation and processing
Code Comparison
Svelte (basic form handling):
<script>
let name = '';
function handleSubmit() {
// Handle form submission
}
</script>
<form on:submit|preventDefault={handleSubmit}>
<input bind:value={name} />
<button type="submit">Submit</button>
</form>
Sveltekit-superforms:
<script>
import { superForm } from 'sveltekit-superforms/client';
const { form, errors, enhance } = superForm(data.form);
</script>
<form method="POST" use:enhance>
<input name="name" bind:value={$form.name} />
{#if $errors.name}<span class="error">{$errors.name}</span>{/if}
<button>Submit</button>
</form>
The code comparison shows that Sveltekit-superforms provides a more streamlined approach to form handling with built-in error management and enhancement features, while Svelte requires more manual setup for similar functionality.
🤖 Headless, performant, and type-safe form state management for TS/JS, React, Vue, Angular, Solid, and Lit.
Pros of TanStack Form
- Framework-agnostic, works with React, Vue, Solid, and more
- Advanced features like form arrays and nested fields
- Extensive documentation and community support
Cons of TanStack Form
- Steeper learning curve due to its flexibility and complexity
- Requires more setup and configuration compared to SvelteKit-superforms
- Not specifically optimized for SvelteKit, may require additional integration work
Code Comparison
SvelteKit-superforms:
import { superForm } from 'sveltekit-superforms/client';
const { form, errors, enhance } = superForm(data);
TanStack Form:
import { useForm } from '@tanstack/react-form';
const form = useForm({
defaultValues: { name: '', email: '' },
onSubmit: async (values) => {
// Submit logic
},
});
Summary
SvelteKit-superforms is tailored for SvelteKit applications, offering a simpler setup and integration process. It provides a more streamlined experience for SvelteKit developers but may lack some advanced features.
TanStack Form, on the other hand, is a versatile solution that works across multiple frameworks. It offers more advanced features and flexibility but requires more setup and may have a steeper learning curve. The choice between the two depends on the specific project requirements and the developer's familiarity with the frameworks.
📋 React Hooks for form state management and validation (Web + React Native)
Pros of react-hook-form
- Widely adopted in the React ecosystem with a large community
- Extensive documentation and examples available
- Supports complex form validation scenarios out of the box
Cons of react-hook-form
- Limited to React applications, not suitable for other frameworks
- Requires more boilerplate code for basic form setups
- Learning curve can be steeper for beginners
Code Comparison
react-hook-form:
import { useForm } from "react-hook-form";
const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("firstName")} />
<input type="submit" />
</form>
sveltekit-superforms:
<script>
import { superForm } from 'sveltekit-superforms';
const { form, errors, enhance } = superForm(data);
</script>
<form method="POST" use:enhance>
<input bind:value={$form.firstName} />
<button>Submit</button>
</form>
The code comparison shows that sveltekit-superforms offers a more concise syntax for basic form setup, while react-hook-form requires more explicit configuration. However, react-hook-form provides more granular control over form elements and validation.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Superforms ð¥
Making SvelteKit forms a pleasure to use!
Feature list
- Server- and client-side validation with your favorite validation libraries, and more to come: ð¥ Arktype ð¥ class-validator ð¥ Effect ð¥ Joi ð¥ Superstruct ð¥ TypeBox ð¥ Valibot ð¥ VineJS ð¥ Yup ð¥ Zod ð¥ or use JSON Schema directly.
- Seamless merging of
PageData
andActionData
- Forget about how to combine them, just focus on your form data, always strongly typed. - Auto-centering and focusing on invalid form fields.
- Tainted form detection, prevents the user from losing data if navigating away from an unsaved form. Or use snapshots to save the form state.
- Automatically coerces
FormData
into correct types, including arrays and files. - For advanced data structures, forget about the limitations of
FormData
- Post nested data structures like a RPC call. - Generates default form values from many validation schemas.
- Handles multiple forms on the same page.
- Works both on the server and with single-page applications (SPA)!
- Convenient handling and validation of file uploads, both on server and client and even in nested data.
- Proxy objects for handling data conversions to string and back again.
- Realtime client-side validation for the best possible UX.
- Create loading spinners easily with three auto-updating timers, based on human perception research.
- Hook into a number of events for full control over the validation data and the
ActionResult
, with a possibility to cancel the update at every step. - Complete customization with a huge list of options.
- No JavaScript required as default, but full support for progressive enhancement.
- Comes with a Super Debugging Svelte Component: SuperDebug.
Get started
Follow the Get started tutorial on the website to get a hands-on introduction to Superforms: https://superforms.rocks/get-started
You can also watch this excellent introduction video to see what's possible: https://www.youtube.com/watch?v=MiKzH3kcVfs
Help & support
- If you're using Superforms in non-profit circumstances, support is completely free; a star on Github is more than enough to show your appreciation. Join the #free-support channel on Discord and ask away!
- If you're making or aiming to make money on your project, a donation proportional to the current profit of the project or the company you work for, will give you a month of commercial support. Donate with one of the options on the website, then ask in the #commercial-support channel on Discord.
Contributing
General feedback, feature requests, bug reports, PR:s, are very welcome as a Github issue or on the Discord server!
Donating
If you appreciate the hard work behind Superforms, please support open source software with a donation.
Top Related Projects
web development, streamlined
lix (change control system) && inlang (globalization ecosystem for software built on lix)
Easily add integrations and other functionality to Svelte(kit) apps
web development for the rest of us
🤖 Headless, performant, and type-safe form state management for TS/JS, React, Vue, Angular, Solid, and Lit.
📋 React Hooks for form state management and validation (Web + React Native)
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot