@@ -32,10 +32,11 @@ npm install zod @hookform/resolvers
3232
3333``` ts
3434import { useRemixForm , getValidatedFormData } from " remix-hook-form" ;
35- import { Form } from " @remix-run/ react" ;
35+ import { Form } from " react-router " ;
3636import { zodResolver } from " @hookform/resolvers/zod" ;
3737import * as zod from " zod" ;
38- import { ActionFunctionArgs , json } from " @remix-run/node" ; // or cloudflare/deno
38+ import type { Route } from " ./+types/home" ;
39+
3940
4041const schema = zod .object ({
4142 name: zod .string ().min (1 ),
@@ -46,16 +47,16 @@ type FormData = zod.infer<typeof schema>;
4647
4748const resolver = zodResolver (schema );
4849
49- export const action = async ({ request }: ActionFunctionArgs ) => {
50+ export const action = async ({ request }: Route . ActionArgs ) => {
5051 const { errors, data, receivedValues : defaultValues } =
5152 await getValidatedFormData <FormData >(request , resolver );
5253 if (errors ) {
5354 // The keys "errors" and "defaultValues" are picked up automatically by useRemixForm
54- return json ( { errors , defaultValues }) ;
55+ return { errors , defaultValues };
5556 }
5657
5758 // Do something with the data
58- return json ( data ) ;
59+ return data ;
5960};
6061
6162export default function MyForm() {
@@ -203,14 +204,14 @@ type SchemaFormData = z.infer<typeof formDataZodSchema>;
203204
204205const resolver = zodResolver (formDataZodSchema );
205206
206- export const action = async ({ request }: ActionFunctionArgs ) => {
207+ export const action = async ({ request }: Route . ActionArgs ) => {
207208 const { errors, data } = await getValidatedFormData <SchemaFormData >(
208209 request ,
209210 resolver ,
210211 true ,
211212 );
212213 if (errors ) {
213- return json ( { errors }) ;
214+ return { errors };
214215 }
215216 // Do something with the data
216217};
@@ -250,12 +251,12 @@ The `receivedValues` property allows you to set the default values of your form
250251``` jsx
251252/** all the same code from above */
252253
253- export const action = async ({ request }: ActionFunctionArgs ) => {
254+ export const action = async ({ request }: Route . ActionArgs ) => {
254255 // Takes the request from the frontend, parses and validates it and returns the data
255256 const { errors , data } =
256257 await getValidatedFormData< FormData > (request, resolver);
257258 if (errors) {
258- return json ( { errors }) ;
259+ return { errors };
259260 }
260261 // Do something with the data
261262};
@@ -266,12 +267,12 @@ If your action returrns `defaultValues` key then it will be automatically used b
266267``` jsx
267268/** all the same code from above */
268269
269- export const action = async ({ request }: ActionFunctionArgs ) => {
270+ export const action = async ({ request }: Route . ActionArgs ) => {
270271 // Takes the request from the frontend, parses and validates it and returns the data
271272 const { errors , data , receivedValues: defaultValues } =
272273 await getValidatedFormData< FormData > (request, resolver);
273274 if (errors) {
274- return json ( { errors, defaultValues }) ;
275+ return { errors, defaultValues };
275276 }
276277 // Do something with the data
277278};
@@ -287,14 +288,14 @@ The difference between `validateFormData` and `getValidatedFormData` is that `va
287288``` jsx
288289/** all the same code from above */
289290
290- export const action = async ({ request }: ActionFunctionArgs ) => {
291+ export const action = async ({ request }: Route . ActionArgs ) => {
291292 // Lets assume you get the data in a different way here but still want to validate it
292293 const formData = await yourWayOfGettingFormData (request);
293294 // Takes the request from the frontend, parses and validates it and returns the data
294295 const { errors , data } =
295296 await validateFormData< FormData > (formData, resolver);
296297 if (errors) {
297- return json ( { errors }) ;
298+ return { errors };
298299 }
299300 // Do something with the data
300301};
@@ -335,7 +336,7 @@ parseFormData is a utility function that can be used to parse the data submitted
335336``` jsx
336337/** all the same code from above */
337338
338- export const action = async ({ request }: ActionFunctionArgs ) => {
339+ export const action = async ({ request }: Route . ActionArgs ) => {
339340 // Allows you to get the data from the request object without having to validate it
340341 const formData = await parseFormData (request);
341342 // formData.age will be a number
0 commit comments