Next.js Discord

Discord Forum

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

Unanswered
Skipjack tuna posted this in #help-forum
Open in Discord
Skipjack tunaOP
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?

8 Replies

Skipjack tunaOP
bump
leerob has you covered: https://youtu.be/KhO4VjaYSXU
@averydelusionalperson leerob has you covered: https://youtu.be/KhO4VjaYSXU
Skipjack tunaOP
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
which next version are you using?
@chisto which next version are you using?
Skipjack tunaOP
I am using version 15.1.0. If necessary, I can also provide more code
@averydelusionalperson leerob has you covered: https://youtu.be/KhO4VjaYSXU
West African Lion
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
@West African Lion 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
Skipjack tunaOP
Hey Eduardo,
yeah I followed his example now and made it work. Its pretty nice, he uses zod validation in the backend though.
In the frontend, he only uses browser provided input attributes like "minLength", "maxLength" and "required", if I'm not mistaken?
That works, but looks a little ugly. He didnt use zod for the frontend then, I guess?
You could still use Zod in the front end if you want to, but you would have to move back to onSubmit and doing the checks there before you call the server action (server actions can be called anywhere from the client component and still work with useActionState hook)