-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware-example.tsx
More file actions
79 lines (68 loc) · 2.21 KB
/
middleware-example.tsx
File metadata and controls
79 lines (68 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
// Example of using the new middleware feature in remix-hook-form v7.0.0
import { Form } from 'react-router';
import { RemixFormProvider, useRemixForm } from 'remix-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import * as zod from 'zod';
import { TextField } from '@lambdacurry/forms/remix-hook-form';
import { getValidatedFormData } from 'remix-hook-form/middleware';
import type { ActionFunctionArgs } from 'react-router';
// Define schema and types
const schema = zod.object({
name: zod.string().min(1, "Name is required"),
email: zod.string().email("Invalid email format").min(1, "Email is required"),
});
type FormData = zod.infer<typeof schema>;
const resolver = zodResolver(schema);
// Action function using the new middleware
export const action = async ({ context }: ActionFunctionArgs) => {
// Use the middleware to extract and validate form data
const { errors, data, receivedValues } = await getValidatedFormData<FormData>(
context,
resolver
);
if (errors) {
return { errors, defaultValues: receivedValues };
}
// Process the validated data
console.log('Processing data:', data);
return { success: true, data };
};
// Component
export default function MiddlewareExample() {
const {
handleSubmit,
formState: { errors },
register,
} = useRemixForm<FormData>({
mode: "onSubmit",
resolver,
});
return (
<div className="p-4">
<h1 className="text-2xl font-bold mb-4">Remix Hook Form v7 Middleware Example</h1>
<RemixFormProvider>
<Form method="POST" onSubmit={handleSubmit}>
<div className="space-y-4">
<TextField
label="Name"
{...register("name")}
error={errors.name?.message}
/>
<TextField
label="Email"
type="email"
{...register("email")}
error={errors.email?.message}
/>
<button
type="submit"
className="px-4 py-2 bg-blue-500 text-white rounded hover:bg-blue-600"
>
Submit
</button>
</div>
</Form>
</RemixFormProvider>
</div>
);
}