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

shared-code.ts

36 lines762 Bytes

1import { formOptions } from '@tanstack/react-form'
2​
3export const formOpts = formOptions({
4 defaultValues: {
5 firstName: '',
6 age: 0,
7 },
8 validators: [
9 {
10 triggers: ['change'],
11 run: ({ value, createErrorMap }) => {
12 const errors = createErrorMap()
13​
14 if (value.age < 8) {
15 errors.fields.age = 'Client validation: You must be at least 8'
16 }
17​
18 return errors
19 },
20 },
21 {
22 triggers: ['server'],
23 skipOnSubmit: true,
24 run: ({ value, createErrorMap }) => {
25 const errors = createErrorMap()
26​
27 if (value.age < 12) {
28 errors.fields.age =
29 'Server validation: You must be at least 12 to sign up'
30 }
31​
32 return errors
33 },
34 },
35 ],
36})