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

page.tsx

73 lines2.0 KB

1import { useAppForm } from '../../hooks/form.tsx'
2import { AddressFields } from './address-fields.tsx'
3import { peopleFormOpts } from './shared-form.tsx'
4​
5export const PeoplePage = () => {
6 const form = useAppForm({
7 ...peopleFormOpts,
8 validators: {
9 onChange: ({ value }) => {
10 const errors = {
11 fields: {},
12 } as {
13 fields: Record<string, string>
14 }
15 if (!value.fullName) {
16 errors.fields.fullName = 'Full name is required'
17 }
18 if (!value.phone) {
19 errors.fields.phone = 'Phone is required'
20 }
21 if (!value.emergencyContact.fullName) {
22 errors.fields['emergencyContact.fullName'] =
23 'Emergency contact full name is required'
24 }
25 if (!value.emergencyContact.phone) {
26 errors.fields['emergencyContact.phone'] =
27 'Emergency contact phone is required'
28 }
29​
30 return errors
31 },
32 },
33 onSubmit: ({ value }) => {
34 alert(JSON.stringify(value, null, 2))
35 },
36 })
37​
38 return (
39 <form
40 onSubmit={(e) => {
41 e.preventDefault()
42 form.handleSubmit()
43 }}
44 >
45 <h1>Personal Information</h1>
46 <form.AppField
47 name="fullName"
48 children={(field) => <field.TextField label="Full Name" />}
49 />
50 <form.AppField
51 name="email"
52 children={(field) => <field.TextField label="Email" />}
53 />
54 <form.AppField
55 name="phone"
56 children={(field) => <field.TextField label="Phone" />}
57 />
58 <AddressFields form={form} />
59 <h2>Emergency Contact</h2>
60 <form.AppField
61 name="emergencyContact.fullName"
62 children={(field) => <field.TextField label="Full Name" />}
63 />
64 <form.AppField
65 name="emergencyContact.phone"
66 children={(field) => <field.TextField label="Phone" />}
67 />
68 <form.AppForm>
69 <form.SubscribeButton label="Submit" />
70 </form.AppForm>
71 </form>
72 )