Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 1 year ago

react-hook-form with zod validation & sonner

react, tailwind-css, vite, zod
Zup•1,370
@xup60521
A solution to the Contact form challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


What are you most proud of, and what would you do differently next time?

react-hook-form is almost necessary when it comes to building forms. Also, by extracting validation logic using zod, it's easier to write and maintain.

const formSchema = z.object({
    first_name: z.string().min(1, { message: "This field is required" }),
    last_name: z.string().min(1, { message: "This field is required" }),
    email: z
        .string()
        .min(1, { message: "This field is required" })
        .email({ message: "Please enter a valid email address" }),
    query: z.string({ message: "Please select a query type" }),
    message: z.string().min(1, { message: "This field is required" }),
    consant: z
        .boolean()
        .refine((checked) => !!checked, {
            message: "To submit this form, please consant to being contacted",
        }),
});

type FormType = z.infer;

// ...

const { register } = useForm({ resolver: zodResolver(formSchema) });
What challenges did you encounter, and how did you overcome them?

Just read some docs or ask LLMs.

Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

No feedback yet. Be the first to give feedback on Zup's solution.

Join our Discord community

Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!

Join our Discord

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner
  • Use cases

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License