Open History to see the last commit
1import z from 'zod'
2import { createValidator } from '@tanstack/react-form'
3import { appFormOptions } from '../../hooks/form.tsx'
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 submitTries =
19 groupApi?.state.submissionAttempts ?? formApi.state.submissionAttempts
20 return submitTries > 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 // This validator is only used when `form.handleSubmit` is called itself.
38 // When `form.FormGroup`'s `handleSubmit` is called, it will only validate the current step's schema.
39 // This means that this schema will not be called when the user submits the form group, but instead when they submit the entire form.
40 run: z.object({
41 step1: step1Schema,
42 step2: step2Schema,
43 }),
44 triggers: [],
45 },
46 ],
47})