Closed nilaq closed 10 months ago
@nilaq it seems you not properly assigning the form props, please ref to NextJS Docs because your problem isnt from the shadcn-ui itself
Check out this discussion, there are possible solutions: https://github.com/react-hook-form/react-hook-form/issues/10391
Thanks @igorm84 & @ShaikRehan123, figured it out, had indeed nothing to do with the Form component
I can across this as well, after reading those docs and the thread, I managed to get it working by calling the server function from the the handleSubmit
call has a reference to:
const form = useForm<z.infer<typeof formSchema>>({
resolver: zodResolver(formSchema),
defaultValues: {
emailAddress: "",
password: "",
},
})
async function onSubmit(values: z.infer<typeof formSchema>) {
console.log(values)
performLogin(values);
}
and obviously the form
is configured as per the shadcnui
docs:
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-8">
In this instance I have a server method either redirecting the user to another (on successful login) or returning a message if there was a problem (credentials didn't match):
const { pending } = useFormStatus();
const form = useForm<z.infer<typeof loginFormSchema>>({
resolver: zodResolver(loginFormSchema),
defaultValues: {
emailAddress: "",
password: "",
},
})
async function onSubmit(values: z.infer<typeof loginFormSchema>) {
const response = await performLogin(values);
if(response && response.message) {
setMessage(response.message);
}
}
Wonder if some of these component templates will move to using useForState
that is now available in react canary.
Anyone implemented error handling and toast notifications with server actions?
Anyone implemented error handling and toast notifications with server actions?
Just add your toast function/error handling in the onSubmit.
onSubmit={async (data) => {
const result = await sendEmail(data);
if (result.error)
return toast.error(result.message);
toast.success(result.message);
form.reset();
}}
I had the zod schema in a "use server" file (the one for the action). I moved it to the "use client" file where the form is, and it worked.
is there a way to use it with the action instead of onSubmit ? I want to use useFormStatus but it doesn't work with onSubmit
@JMRodriguez-work Don't use the form component by shadcn, use the native one, Checkout my project AntiExcel, I've implemented many forms with useFormStatus and useFormState
I had the zod schema in a "use server" file (the one for the action). I moved it to the "use client" file where the form is, and it worked.
@alfonsocartes I first moved the schema to the 'use client' and exported the schema so I could add the validation in the server too. I got an error saying I could not do that so, in the end, I created a separate .ts file (no "use client" or "use server") and from there exported my schema so I could use it in both the client and server.
I am trying to build a simple signup dialog, where users can leave their email to be put on a waitlist. However, I am running into issues when trying to trigger a Next.js Server Action as submit action in the Form Component. Is this a bug or am i just doing something wrong?
page.tsx
GetStartedButton.tsx
actions.ts
I am getting this error: