Skip to content

Persistent Validation Errors After Failed Submission with react-hook-form, shadcn and Zod #11112

Answered by Juli4nG
DeniCarvalho asked this question in General
Discussion options

You must be logged in to vote

Hello! I saw a tutorial from Jack Herrington talking about this issue a few weeks back:
https://youtu.be/VLk45JBe8L8?feature=shared&t=684

Basically if the submission fails you send back the fields from the server and set them as defaultValues.

I'm using Next.js with server actions, but it may be of some guidance. :)

Client side

const [state, formAction] = useFormState(FormAction, {
    message: "",
  });

  const form = useForm<z.output<typeof FormSchema>>({
    resolver: zodResolver(FormSchema),
    defaultValues: {
     // Put your normal default values here
      ...(state?.fields ?? {}),
    },
  });

return (
<Form {...form}>
      <form
        ref={formRef}
        action={formActi…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by DeniCarvalho
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants