Forms
Collect each step's data with a store, validate it with a schema, and submit at the end.
Stepperize owns the flow; your fields stay in signals, a store or 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/solid";
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.
import { For, Match, Switch } from "solid-js";
import { createStore } from "solid-js/store";
import { signup } from "./signup";
export function Signup() {
const stepper = signup.useStepper({
onFinish: (output) => createAccount(output),
});
const [account, setAccount] = createStore({ email: "" });
const [profile, setProfile] = createStore({ name: "" });
function submit(event: SubmitEvent) {
event.preventDefault();
if (stepper.current.id === "account") return stepper.next({ ...account });
if (stepper.current.id === "profile") return stepper.next({ ...profile });
return stepper.next();
}
return (
<form novalidate onSubmit={submit}>
<Switch>
<Match when={stepper.current.id === "account"}>
<label>
Email
<input
type="email"
value={account.email}
onInput={(event) => setAccount("email", event.currentTarget.value)}
/>
</label>
</Match>
<Match when={stepper.current.id === "profile"}>
<label>
Name
<input
value={profile.name}
onInput={(event) => setProfile("name", event.currentTarget.value)}
/>
</label>
</Match>
<Match when={stepper.current.id === "done"}>
<p>Create the account for {stepper.output.profile?.name}?</p>
</Match>
</Switch>
<For each={stepper.issues()}>{(issue) => <p role="alert">{issue.message}</p>}</For>
<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:
{ ...account }copies the store, so what the flow stores is a plain object of its own.unwrap(account)is not a copy: it is the store's own object, and later edits would change the flow's data behind its back. For a nested store, usestructuredClone(unwrap(account)). 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).
import { createEffect, on } from "solid-js";
import { createStore } from "solid-js/store";
const stepper = signup.useStepperContext();
const [values, setValues] = createStore({ email: "", ...stepper.data.account });
createEffect(
on(
() => ({ ...values }),
(draft) => stepper.setData("account", draft),
{ defer: true },
),
);defer skips the run on mount: without it, the first run would write the same
values back and make a complete step incomplete. Actions are never tracked, so
the effect only follows values.
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 Solid developers use most. Each page is a complete example, checked against the library's current version:
- TanStack Form: the step's schema is the form's validator, and the move is its last one.
- Formisch, the successor of Modular Forms: one Valibot schema for the form and the step.
More
Schemas and validation covers async schemas, guards and cross-step rules. The options are the same here.
Last updated on