In this tutorial we’re going to learn in detail about:
- How to do field validations using Zod.
- How to customize error messages.
- How to display validation errors.
We’re going to build a form that contains the following fields and requirements:
- Name: length between 2 - 10 characters.
- Email: must use valid email.
- website: must use valid url.
UI setup
We’re going to create a form containing labels and text fields using the good old <input />
element.
Define the form fields and validations
The next thing to do is to define our validations using Zod.
I like to define it in a separate file to make the code look clean.
Zod already provides us a way to do most common validations such as string length, email and url validation.
We also export a type from the schema that will be used in the next step.
Integrate schema validation into the form
We use the formSchema
defined previously and create a hook that will handle everything else for us.
The option resolver: zodResolver(formSchema)
tells React Hook Form to use the field validations defined in formSchema
.
Handle text field events
Now we want to inform React Hook Form whenever there are changes in the text field.
register('email')
means anything we do to that text field will affect the value of email
field.
When we put the Form
generic type into the useForm<Form>
, we’re telling TypeScript to only accept fields defined in the Form
type.
That means register('nonExistingField')
will result in TypeScript compilation error.
Handling form submission
We need a way to capture all the validated form data so we can send it to the server for example.
For now we’ll just show the validated data in the console.
Now try inputting valid values to the field and see the result.
Congratulations, now you get a working form, but not complete yet!
Display validation errors
If you leave the form blank or put some invalid values, the form will not show any error message.
React Hook Form validates our input when we press “Submit” and stores the error messages.
It is our job to show the validation errors.
Now all the errors will show up on the screen.
Customize error messages
The error messages shown above are not the most user friendly ones.
My parents would think that getting the name right would involve something to do with guitars or violins.
Fortunately modifying error messages is simple, we only need to define them in each validation in the schema.
Now try running the validations again, you’ll get a much more user friendly error messages.
Final result
The sandbox below summarizes this tutorial. Feel free to fork and try it out.