Replies: 4 comments 1 reply
-
Hello, |
Beta Was this translation helpful? Give feedback.
0 replies
-
With react-hook-form, the only way that worked for me is to set the value programmatically, during render or in useEffect:
|
Beta Was this translation helpful? Give feedback.
1 reply
-
const email = "[email protected]";
const schema = z.object({
email: z.string().email().default(email) // 👍
});
const form = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
defaultValues: {
email,
},
}); |
Beta Was this translation helpful? Give feedback.
0 replies
-
Here is the snippet: const formSchema = z.object({
addressLine1: z.string(),
addressLine2: z.string(),
city: z.string(),
state: z.string(),
country: z.string(),
pincode: z.string().min(6).max(6),
gstNumber: z.string(),
});
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
addressLine1: "",
addressLine2: "",
city: "",
state: "",
country: "",
pincode: "",
gstNumber: "",
},
});
useEffect(() => {
if (billingData) {
form.setValue("addressLine1", billingData.billing_add_1 || "");
form.setValue("addressLine2", billingData.billing_add_2 || "");
form.setValue("city", billingData.city || "");
form.setValue("state", billingData.state || "");
form.setValue("country", billingData.country || "");
form.setValue("pincode", billingData?.pin?.toString() || "");
form.setValue("gstNumber", billingData.gst_number || "");
}
}, [billingData, form]);
|
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I tried adding default value to a form. It also has
zod
as validation. When I submit the form, it says this field is required even when I have default value set to something.Beta Was this translation helpful? Give feedback.
All reactions