Skip to content
Source
Sign inSign up
TanStack/form
Open History to see the last commit

shared-form.ts

46 lines1.0 KB

1import { createValidator } from '@tanstack/vue-form'
2import { z } from 'zod'
3import { appFormOptions } from '../../hooks/form.ts'
4​
5export const step1Schema = z.object({
6 name: z.string().min(2, 'Name must be at least 2 characters'),
7})
8​
9export const step2Schema = z.object({
10 name: z.string().min(3, 'Name must be at least 3 characters'),
11})
12​
13export const stepValidator = createValidator({
14 triggers: [
15 {
16 trigger: 'change',
17 when: ({ groupApi, formApi }) => {
18 const submissionAttempts =
19 groupApi?.state.submissionAttempts ?? formApi.state.submissionAttempts
20 return submissionAttempts > 0
21 },
22 },
23 ],
24})
25​
26export const wizardFormOpts = appFormOptions({
27 defaultValues: {
28 step1: {
29 name: '',
30 },
31 step2: {
32 name: '',
33 },
34 },
35 validators: [
36 {
37 // Group submission validates only the current step. Direct form
38 // submission validates the entire wizard.
39 run: z.object({
40 step1: step1Schema,
41 step2: step2Schema,
42 }),
43 triggers: [],
44 },
45 ],
46})