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

shared-form.ts

31 lines861 Bytes

1import { createValidator, formOptions } from '@tanstack/angular-form'
2import { z } from 'zod'
3​
4export const step1Schema = z.object({
5 name: z.string().min(2, 'Name must be at least 2 characters'),
6})
7export const step2Schema = z.object({
8 name: z.string().min(3, 'Name must be at least 3 characters'),
9})
10​
11export const stepValidator = createValidator({
12 triggers: [
13 {
14 trigger: 'change',
15 when: ({ groupApi, formApi }) =>
16 (groupApi?.state.submissionAttempts ??
17 formApi.state.submissionAttempts) > 0,
18 },
19 ],
20})
21​
22export const wizardFormOptions = formOptions({
23 defaultValues: { step1: { name: '' }, step2: { name: '' } },
24 validators: [
25 {
26 run: z.object({ step1: step1Schema, step2: step2Schema }),
27 triggers: [],
28 },
29 ],
30 onSubmit: ({ value }) => alert(`Form submitted: ${JSON.stringify(value)}`),
31})