Submitted over 1 year agoA solution to the Contact form challenge
react-hook-form with zod validation & sonner
react, tailwind-css, vite, zod
@xup60521

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 GitHubCommunity 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