Forms
Collect each step's data with v-model, validate it with a schema, and submit at the end.
Stepperize owns the flow; your fields stay in v-model 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/vue";
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 changes, through the next next(data) or a
setData. Typing in a v-model field does not change the step's data by
itself: to clear the message as the user types, write the draft as shown in
Keeping drafts.
<script setup lang="ts">
import { reactive } from "vue";
import { signup } from "./signup";
const stepper = signup.useStepper({
onFinish: (output) => createAccount(output),
});
const account = reactive({ email: "" });
const profile = reactive({ name: "" });
function submit() {
if (stepper.current.id === "account") return stepper.next({ ...account });
if (stepper.current.id === "profile") return stepper.next({ ...profile });
return stepper.next();
}
</script>
<template>
<form novalidate @submit.prevent="submit">
<label v-if="stepper.current.id === 'account'">
Email
<input v-model="account.email" type="email" />
</label>
<label v-else-if="stepper.current.id === 'profile'">
Name
<input v-model="profile.name" />
</label>
<p v-else>Create the account for {{ stepper.output.profile?.name }}?</p>
<p v-for="(issue, index) in stepper.issues()" :key="index" role="alert">
{{ issue.message }}
</p>
<button type="button" :disabled="stepper.isFirst" @click="stepper.prev()">Back</button>
<button type="submit" :disabled="stepper.isPending">
{{ stepper.isLast ? "Create account" : "Continue" }}
</button>
</form>
</template>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), or pass stepper down as a
prop.
<script setup lang="ts">
import { reactive, watch } from "vue";
import { signup } from "./signup";
const stepper = signup.useStepperContext();
const values = reactive({ email: "", ...stepper.data.account });
watch(values, () => stepper.setData("account", { ...values }));
</script>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 Vue developers use most. Each page is a complete example, checked against the library's current version:
- VeeValidate:
useFormper step, with any Standard Schema through a small typed-schema adapter. - TanStack Form: the step's schema is the form's validator, and the move is its last one.
- FormKit: FormKit's inputs and rules, with the step's issues shown on them.
More
Schemas and validation covers async schemas, guards and cross-step rules. The options are the same here.
Last updated on