Forms
Collect each step's data with bind:value, validate it with a schema, and submit at the end.
Stepperize owns the flow; your fields stay in $state or in your form library.
The two meet in one call: next(data).
A schema per step
Give a step a schema and its data is typed and validated. Any
Standard Schema library works.
import { defineStepper } from "@stepperize/svelte";
import { z } from "zod";
export const signup = defineStepper([
{ id: "account", title: "Account", schema: z.object({ email: z.email() }) },
{ id: "profile", title: "Profile", schema: z.object({ name: z.string().min(1) }) },
{ id: "done", title: "Done" },
]);Validate on Continue
next(data) saves the data for the current step, validates it and only moves
when it is valid. It resolves with what happened, and stepper.issues() holds
the reasons until the step's data in the flow changes, through the next
next(data) or a setData. Typing in a field does not change it by itself.
<script lang="ts">
import { signup } from "./signup";
const stepper = signup.useStepper({
onFinish: (output) => createAccount(output),
});
let account = $state({ email: "" });
let profile = $state({ name: "" });
function submit(event: SubmitEvent) {
event.preventDefault();
if (stepper.current.id === "account") return stepper.next($state.snapshot(account));
if (stepper.current.id === "profile") return stepper.next($state.snapshot(profile));
return stepper.next();
}
</script>
<form novalidate onsubmit={submit}>
{#if stepper.current.id === "account"}
<label>Email <input type="email" bind:value={account.email} /></label>
{:else if stepper.current.id === "profile"}
<label>Name <input bind:value={profile.name} /></label>
{:else}
<p>Create the account for {stepper.output.profile?.name}?</p>
{/if}
{#each stepper.issues() ?? [] as issue}
<p role="alert">{issue.message}</p>
{/each}
<button type="button" disabled={stepper.isFirst} onclick={() => stepper.prev()}>Back</button>
<button type="submit" disabled={stepper.isPending}>
{stepper.isLast ? "Create account" : "Continue"}
</button>
</form>- Pass plain data:
$state.snapshot(values)unwraps the reactive proxy, so what the flow stores (and what you persist) is a plain object. stepper.datais what each step holds as it was typed;stepper.outputis what each schema produced, with transforms and defaults applied.onFinish(output, data)runs once, whennext()completes the last step. Return a promise andstepper.isPendingstaystrueuntil it settles.
Keeping drafts
To keep what was typed when the user goes back, or to persist it, write the
draft as it changes and read it back when the step mounts. A step in its own
component reads the flow with useStepperContext(), which needs a shared
flow: wrap the steps in signup.Provider (see
Shared state).
<script lang="ts">
import { signup } from "./signup";
const stepper = signup.useStepperContext();
let values = $state({ email: "", ...stepper.data.account });
</script>
<label>
Email
<input
type="email"
bind:value={values.email}
oninput={() => stepper.setData("account", $state.snapshot(values))}
/>
</label>Write the draft from the input's event, not from an $effect: an effect also
runs once on mount, and that first write would make a complete step incomplete
without the user touching it.
setData does not validate. A step that was complete stops being complete when
its data changes.
Checking one step without moving
stepper.validate() checks the current step and resolves with its issues, or
undefined when it is valid. Use it for "validate on blur".
With a form library
The same checkout, built with the form libraries Svelte developers use most. Each page is a complete example, checked against the library's current version:
- Superforms: SvelteKit form actions, one per step, validated in the browser and on the server.
- TanStack Form: the step's schema is the form's validator, and the move is its last one.
More
Schemas and validation covers async schemas, guards and cross-step rules. The options are the same here.
Last updated on