Error messages from react-hook-form with zod(来自带有Zod的Reaction-Hook-Form的错误消息)
转载作者:bug小助手更新时间:2023-10-27 19:54:47314
I created a customer registration form, but all my "messages" from my "errors" are coming as required. Does anyone know what it could be? Something I set up wrong in zod or react-hook-form. Below I will leave the prints of the code.
const createUserFormSchema = z.object({ name: z .string() .min(2, 'Password must be at least 2 characters long') .transform((name) => name .trim() .split(' ') .map((word) => { return word[0].toLocaleUpperCase().concat(word.substring(1)) }) .join(' '), ), email: z .string() .email('Invalid e-mail format') .nonempty('E-mail is required'), birthdate: z.string().nonempty('Date of birth is mandatory'), cpf: z.string().length(11, 'CPF mus have 11 digits'), })
type CreateUserFormData = z.infer<typeof createUserFormSchema>
The problem here is, when I give console.log(errors), my errors are all as "Required", that is, they are not showing the error messages that I set inside my schema.
I was facing the same problem and it had to do with a bad setup between my custom Input component and the {...register("name")} props. Try changing your input to the standard <input /> component and see if the game changes... it probably will.
Honestly I have no idea why spreading the {...register("name")} is causing problems, but it is. Here's a fixed version of your component, all I did different was passing down the register function as a Prop and calling it inside the Input (Maybe you should rename the component to InputForm cus it's now a pretty specific component that only works with such function)
I also decided to pass down the name as a prop so we can properly call register. For convenience we can also infer the error messages with the name prop.
import { type DetailedHTMLProps, type InputHTMLAttributes } from "react"; import { type FieldErrors, type FieldValues, type Path, type UseFormRegister, } from "react-hook-form";
<Input label="Nome" type="text" placeholder="Enter your name" name="name" errors={errors} register={register} />
<Input label="Email" type="email" name="email" placeholder="Enter your e-mail" errors={errors} register={register} /> // ... and so forth
Here's the result:
结果如下:
Workaround
If you still want to keep you component that way, there's a workaround, you'll need to specify your message in more than one place.
如果您仍然想让组件保持这种方式,有一个解决办法,您需要在多个地方指定您的消息。
What's happening is that zod and react-hook-form generate different error messages depending on the situation, which is a great feature actually. So the error type that it is firing for you does not come from the message you've set in min() but from the z.string() part of the zod schema:
name: z .string() .min(2, 'Password must be at least 2 characters long')
So to fix that, let's specify a custom message for the required_error in the string()
因此,为了解决这个问题,让我们为字符串()中的REQUIRED_ERROR指定一个定制消息
name: z .string({ required_error: 'Passoword is required' }) .min(2, 'Password must be at least 2 characters long')
Now you'll have a better error message when the form is submitted with an empty name field.
现在,当表单提交时名称字段为空时,您会得到更好的错误消息。
For anyone facing a similar problem but not exactly this one, I recommend logging the error message that is coming from react-hook-form so you can investigate where your error is coming from, that's how I figured how to fix this problem.
// Pass down this function in the handleSubmit <form onSubmit={handleSubmit(createUser, onFormError)}>
I faced the same problem when using zod with react-hook-form. Even though i added custom message in zod object but still im getting error message as Required. after searching through the net i found this article.
我是一名优秀的程序员,十分优秀!