# Forms

> Collect each step's data with bind:value, validate it with a schema, and submit at the end.

Framework: Svelte (`@stepperize/svelte`), Stepperize v9.
Source: https://stepperize.com/docs/v9/svelte/guides/forms

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](https://standardschema.dev) library works.

```ts title="signup.ts"
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.

```svelte title="Signup.svelte"
<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.data` is what each step holds as it was typed; `stepper.output` is
  what each schema produced, with transforms and defaults applied.
- `onFinish(output, data)` runs once, when `next()` completes the last step.
  Return a promise and `stepper.isPending` stays `true` until 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](https://stepperize.com/docs/v9/svelte/guides/shared-state)).

```svelte title="AccountStep.svelte"
<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](https://stepperize.com/docs/v9/svelte/forms/superforms): SvelteKit form actions, one
  per step, validated in the browser and on the server.
- [TanStack Form](https://stepperize.com/docs/v9/svelte/forms/tanstack-form): the step's schema is
  the form's validator, and the move is its last one.

## More

[Schemas and validation](https://stepperize.com/docs/v9/svelte/guides/validation) covers async
schemas, guards and cross-step rules. The options are the same here.
