-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1d30561
commit 2fefe5e
Showing
3 changed files
with
33 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,36 +23,28 @@ const ACCEPTED_IMAGE_TYPES = [ | |
"image/webp", | ||
]; | ||
const FormDataZodSchema = z.object({ | ||
action: z.string(), | ||
outline: z.string().min(2, { | ||
message: "Outline must be at least 2 characters.", | ||
}), | ||
email: z.string().trim().nonempty("validation.required"), | ||
password: z.string().trim().nonempty("validation.required"), | ||
number: z.number({ coerce: true }).int().positive(), | ||
redirectTo: z.string().optional(), | ||
}); | ||
|
||
type FormData = z.infer<typeof FormDataZodSchema>; | ||
|
||
const resolver = zodResolver(FormDataZodSchema); | ||
|
||
export const action = async ({ request }: ActionFunctionArgs) => { | ||
const formData = await request.formData(); | ||
|
||
if (formData.get("action") === "accept") { | ||
console.log("formData", Object.fromEntries(formData)); | ||
|
||
const { data, errors } = await validateFormData<FormData>( | ||
formData, | ||
resolver, | ||
); | ||
|
||
console.log("errors", errors); | ||
|
||
if (errors) { | ||
return json(errors); | ||
} | ||
|
||
console.log(data); | ||
return null; | ||
const { data, errors, receivedValues } = await getValidatedFormData( | ||
request, | ||
resolver, | ||
); | ||
console.log("action", data, errors, receivedValues); | ||
if (errors) { | ||
return json(errors, { | ||
status: 422, | ||
}); | ||
} | ||
return json({ result: "success" }); | ||
}; | ||
|
||
export const loader = ({ request }: LoaderFunctionArgs) => { | ||
|
@@ -65,7 +57,10 @@ export default function Index() { | |
const methods = useRemixForm<FormData>({ | ||
resolver, | ||
defaultValues: { | ||
action: "accept", | ||
email: "[email protected]", | ||
password: "12345678", | ||
redirectTo: undefined, | ||
number: 1, | ||
}, | ||
submitConfig: { | ||
method: "POST", | ||
|
@@ -80,8 +75,9 @@ export default function Index() { | |
|
||
<Form method="post" onSubmit={handleSubmit}> | ||
<div className="flex flex-col gap-2"> | ||
<input type="text" {...register("action")} /> | ||
<input type="text" {...register("outline")} /> | ||
<input type="text" {...register("email")} /> | ||
<input type="text" {...register("password")} /> | ||
<input type="number" {...register("number")} /> | ||
</div> | ||
<div> | ||
<button type="submit" className="button"> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters