-
I have a project where I use the zod library for the error messages in the forms. But I don't know how I could integrate this library with the zod message maps. There is a library that handles this kind of situation, it is called Is there any way to integrate this library ( |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 10 replies
-
Interesting question! I haven't worked with My general advice would be that you should receive a structured error object from This is somewhat related: https://next-intl-docs.vercel.app/blog/translations-outside-of-react-components But again, I'm not really familiar with EDIT: Here's an example I saw in the wild: nextjs-shadcn-admin ( |
Beta Was this translation helpful? Give feedback.
-
I've just added a section on Server Actions to the next-intl docs. There's also an example of how Zod can be used for parsing incoming form data, hope this helps! A side note for |
Beta Was this translation helpful? Give feedback.
-
Try this solution. I've simplified the complexity of use. |
Beta Was this translation helpful? Give feedback.
-
Hey, If you wish, you can use this approach. It allows you to isolate schemas in specific files without complicating the form components :) Example :
import { z } from "zod";
import { useTranslations } from "next-intl";
export const useLoginSchema = () => {
const t = useTranslations("validation");
const LOGIN_SCHEMA = z.object({
email: z
.string()
.min(1, { message: t("emailEmpty") })
.email({ message: t("emailInvalid") }),
password: z.string().min(1, { message: t("passwordEmpty") }),
});
return LOGIN_SCHEMA;
};
export type TLoginSchema = z.infer<ReturnType<typeof useLoginSchema>>;
"use client";
import { useLoginSchema, type TLoginSchema } from "~/schemas/auth.schema";
import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
export default function LoginForm() {
const loginSchema = useLoginSchema();
const form = useForm<TLoginSchema>({
resolver: zodResolver(loginSchema),
defaultValues: {
email: "",
password: "",
},
});
} |
Beta Was this translation helpful? Give feedback.
-
The Before the update, I was able to do this without any typescript errors from zod validators:
Now, I get this typescript error when hovering over Workarounds: • Override P.S. using Any suggestions? |
Beta Was this translation helpful? Give feedback.
I've just added a section on Server Actions to the next-intl docs. There's also an example of how Zod can be used for parsing incoming form data, hope this helps!
A side note for
react-hook-form
users:zodResolver
seems to accept a second argument (schemaOptions
) that can be used to provide theerrorMap
as shown in the Server Actions example in the docs.