Zod validation with react-hook-form

Is there a way to validate form field for multiple errors simultaneosly? My use case is password strength indicator where I need to validate password for 3 validation criteria (length, contains digit, contains special symbol) at the same time. I am using zod and my schema is like this:
import { z } from "zod";

export const schema = z
.object({
old_password: z.string().min(1, { message: "Required" }),
new_password: z
.string()
.min(8, { message: "Min 8 characters" })
.regex(/[0-9]/g, { message: "Need a digit" })
.regex(/[!,@,#,$,%,^,&,*]/g, {
message: "Need a special character !@#$%^&*",
}),
confirm_new_password: z.string(),
})
.refine((data) => data.confirm_new_password === data.new_password, {
message: "Should be equal to new password",
path: ["confirm_new_password"],
});
export type Schema = z.infer<typeof schema>;
import { z } from "zod";

export const schema = z
.object({
old_password: z.string().min(1, { message: "Required" }),
new_password: z
.string()
.min(8, { message: "Min 8 characters" })
.regex(/[0-9]/g, { message: "Need a digit" })
.regex(/[!,@,#,$,%,^,&,*]/g, {
message: "Need a special character !@#$%^&*",
}),
confirm_new_password: z.string(),
})
.refine((data) => data.confirm_new_password === data.new_password, {
message: "Should be equal to new password",
path: ["confirm_new_password"],
});
export type Schema = z.infer<typeof schema>;
But the problem is that I am only getting 1 error at a time (in react-hook-forms form.formState.errors) as I defined above. I tried to use z.union and even z.superRefine but to no avail.
7 Replies
alexmartos
alexmartos8mo ago
Found this PR https://github.com/react-hook-form/react-hook-form/pull/750 Try using criteriaMode instead
GitHub
support multiple errors for ErrorMessage component by kotarella1110...
Add render prop (children prop) with message and messages as arguments! Related PR: #715 export default function App() { const { register, errors, handleSubmit } = useForm<{ password: stri...
GoldStrikeArch
GoldStrikeArch8mo ago
@alexmartos Do you know how to implement this? I mean I am only getting a single error inside form.formState.errors ?
alexmartos
alexmartos8mo ago
Alex Martos P.
StackBlitz
Next.js Starter - StackBlitz
The React framework for production
alexmartos
alexmartos8mo ago
Try commenting out criteriaMode and it shold only warn about 1 error
GoldStrikeArch
GoldStrikeArch8mo ago
@alexmartos Oh, I see, thank you! This is really helpful But this solutions doesn't seem flexible at all Like if I need some fields to show all errors and other fields to show only first error, then I think I would need to use 2 different useForm hooks...
alexmartos
alexmartos8mo ago
If I remember correctly, you always get the first error as normal under ‘error.message’ if you have multiple errors those are under ‘error.types’ So you could use this solution for both all and first
GoldStrikeArch
GoldStrikeArch8mo ago
Yes, I see. You are correct this should solve my case
Want results from more Discord servers?
Add your server
More Posts
Having an issue when using Nextauth for signing in getting `Invalid Compact JWE`So i have adjusted my User interface and what Nextauth session return. this is my new interfaces: ``Using tRPC without the back-end lock-in. I like it now, but wanna be flexible in the future.Hey, everyone! My use-case here is that I'm using NextJS for a project that relies on somewhat complIs there a way to get a previous dynamic route in app directory?Take this for example ```ts app - site - [slug] - map - page.tsx // how to get tRPC alternatives for a simple public APII have a T3 monorepo with tRPC with 100+ PRIVATE API endpoints. 1) I want to create a public API wii am building a react component library how can i keep the bundle size very lowI am trying to build a component library for react Nothing to crazy just a simple and sing componentBest way to prefetch queries with tRPC and app/ dirI am in the process of switching my pages/ T3 app to the new fancy app directory and know that ReactonSuccess, onError and onSettled deprecated next major version?with `onSuccess`, `onError` and `onSettled` being deprecated soon™️, what would the best practice beHow did Vercel Implement this ??When you encounter an error, it opens up the editor file in VSCode. How do you built this browser feHave someone implemented pagination with server actions and prisma?Could you send link if soRestrict direct access to NextJS routesI am writing a user onboarding flow where the whole onboarding flow is about 3 pages, it starts with