Skip to content

Commit 6945029

Browse files
committed
fix: updating form examples to use zod
1 parent 0d35c5b commit 6945029

File tree

4 files changed

+32
-28
lines changed

4 files changed

+32
-28
lines changed

templates/react/add-on/form/assets/src/components/demo.FormComponents.tsx

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ export function SubscribeButton({ label }: { label: string }) {
1717
)
1818
}
1919

20+
function ErrorMessages({
21+
errors,
22+
}: {
23+
errors: Array<string | { message: string }>
24+
}) {
25+
return (
26+
<>
27+
{errors.map((error) => (
28+
<div
29+
key={typeof error === 'string' ? error : error.message}
30+
className="text-red-500 mt-1 font-bold"
31+
>
32+
{typeof error === 'string' ? error : error.message}
33+
</div>
34+
))}
35+
</>
36+
)
37+
}
38+
2039
export function TextField({
2140
label,
2241
placeholder,
@@ -39,11 +58,7 @@ export function TextField({
3958
className="w-full px-4 py-2 rounded-md border border-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500"
4059
/>
4160
</label>
42-
{errors.map((error: string) => (
43-
<div key={error} className="text-red-500 mt-1 font-bold">
44-
{error}
45-
</div>
46-
))}
61+
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
4762
</div>
4863
)
4964
}
@@ -70,11 +85,7 @@ export function TextArea({
7085
className="w-full px-4 py-2 rounded-md border border-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500"
7186
/>
7287
</label>
73-
{errors.map((error: string) => (
74-
<div key={error} className="text-red-500 mt-1 font-bold">
75-
{error}
76-
</div>
77-
))}
88+
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
7889
</div>
7990
)
8091
}
@@ -103,11 +114,7 @@ export function Select({
103114
>
104115
{children}
105116
</select>
106-
{errors.map((error: string) => (
107-
<div key={error} className="text-red-500 mt-1 font-bold">
108-
{error}
109-
</div>
110-
))}
117+
{field.state.meta.isTouched && <ErrorMessages errors={errors} />}
111118
</div>
112119
)
113120
}

templates/react/add-on/form/assets/src/routes/demo.form.simple.tsx.ejs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { <% if (fileRouter) { %>createFileRoute<% } else { %>createRoute<% } %> } from '@tanstack/react-router'
2+
import { z } from 'zod'
23

34
import { useAppForm } from '../hooks/demo.form'
45

@@ -10,24 +11,19 @@ export const Route = createFileRoute('/demo/form')({
1011
})
1112
<% } %>
1213

14+
const schema = z.object({
15+
title: z.string().min(1, 'Title is required'),
16+
description: z.string().min(1, 'Description is required'),
17+
})
18+
1319
function SimpleForm() {
1420
const form = useAppForm({
1521
defaultValues: {
1622
title: '',
1723
description: '',
1824
},
1925
validators: {
20-
onBlur: ({ value }) => {
21-
const errors = {
22-
fields: {},
23-
} as {
24-
fields: Record<string, string>
25-
}
26-
if (value.title.trim().length === 0) {
27-
errors.fields.title = 'Title is required'
28-
}
29-
return errors
30-
},
26+
onBlur: schema,
3127
},
3228
onSubmit: ({ value }) => {
3329
console.log(value)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{
22
"dependencies": {
3-
"@tanstack/react-form": "^1.0.0"
3+
"@tanstack/react-form": "^1.0.0",
4+
"zod": "^3.24.2"
45
}
56
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"dependencies": {
3-
"@tanstack/solid-form": "^0.43.2"
3+
"@tanstack/solid-form": "^1.0.0"
44
}
55
}

0 commit comments

Comments
 (0)