#Clear form and enable zod validation with useActionState (server action)

9 messages · Page 1 of 1 (latest)

compact thistle
#

Hey there,

so I am new to trying out server actions. I currently use this useActionState code for my form (which then handles it in the backend) and it works! The only thing that doesnt work anymore (now that I've switched from a simple submit handler to a server action): resetting the form and seeing zod's frontend validation. Quick recap: this used (!) to be my actual code before that worked like a charm:

  function onSubmit(values: z.infer<typeof formSchema>) {
    console.log("Form sent: ", values);
    form.reset();
  }

Then I switched to a server action with useActionState and connected it to my (React Hook) Form:

const [state, action, isPending] = useActionState(sendFormEmail, "");

<Form {...form}>
<form className="w-full max-w-4xl mx-auto space-y-6" action={action}>
...

The server action works fine in the backend, but:
How on earth do I clear my form's inputs now (best practice way, if possible) and how do I make the frontend validation visible again? When I enter invalid data, I don't get Zod's error messages anymore.

Note: Using both the action and the onSubmit attribute does not work for the form, one cancels the other one out. I can only use one of

Last but not least, using the useActionState Hook now forced my server action function to include the "previousState" parameter. I honestly don't know any good use case for this, is there any way to omit it?

lilac gateBOT
#

🔎 This post has been indexed in our web forum and will be seen by search engines so other users can find it outside Discord

🕵️ Your user profile is private by default and won't be visible to users outside Discord, if you want to be visible in the web forum you can add the "Public Profile" role in id:customize

✅ You can mark a message as the answer for your post with Right click -> Apps -> Mark Solution
(if you don't see the option, try refreshing Discord with Ctrl + R)

compact thistle
#

bump

timid trail
compact thistle
# timid trail leerob has you covered: https://youtu.be/KhO4VjaYSXU

Not really. In his example, his form is cleared automatically after it gets submitted. Thats not the case for me, if it'd reset automatically, I wouldnt be posting here.

Its worthy to note that while he uses Shadcn UI, he is NOT using the Shadcn Form component in the video, apparently? Maybe thats why? Idk

fast widget
#

which next version are you using?

compact thistle
desert basin
# timid trail leerob has you covered: https://youtu.be/KhO4VjaYSXU

If you watch this video, you can solve the front end validate issue with Zod..

about the cleared automatically issue, you don't need do nothing if you made your code following the example on the Doc...
Has this example in video: https://www.youtube.com/watch?v=dDpZfOQBMaU
or in code: https://github.com/vercel/next.js/tree/canary/examples/next-forms

Learn how to create forms and handle data mutations in the Next.js App Router. This tutorial walks through using Server Actions, creating loading and error states with useFormStatus and useFormState, as well as accessibility best practices.

0:00 – Introduction
0:06 – Demo
0:21 – Forms with Pages Router
1:05 – create-next-app
1:30 – Server Compo...

▶ Play video
GitHub

The React Framework. Contribute to vercel/next.js development by creating an account on GitHub.

compact thistle