---------------------------------------------------------------------------------- Learn how to add Zod schema validation to a React Hook Form profile settings page in Next.js + TypeScript. Live error messages, conditional styling, z.infer, and best practices for real-world job portal forms. ------------------------------------------------------------------------------------ 🔗 Useful Playlists
⌛TIMELINE⏳ 0:00 – Intro: Applicant Profile Form Overview 1:02 – Support the Channel 1:30 – Visiting Zod Official Website 2:00 – Folder Structure & Schema File Setup 2:32 – Understanding the Zod Schema for Applicant Profile 8:40 – z.infer Deep Dive 9:40 – Integrating Zod with React Hook Form (Zod Resolver) 11:35 – Real-Time Error Handling & Conditional Styling 14:20 – Practical Task: Convert Hardcoded Fields to Dynamic Array
AI Generated·May contain errors
Hello what's up guys Welcome to NextGest and MySQL's job portal application — today
is episode number 76 and today we're going
to explore applicant profile form validation
Yes guys so here you can
see very clearly—like if
I don’t type anything I
get this validation error
Name must be at least 2 characters long phone
number must be at least 10 digits
location and nationality are required
invalid date of birth
and similar details
so here I can see all those things
so how can we do this
that we are going to see in this complete video with the help of JWT
yes guys you must remember that JWT is very important
so through that we'll create a schema then
connect it to our React
Hook Form and in this
way each individual input field text area
or other element will
be linked accordingly
we're going to include that so this is very important because nothing
is being hardcoded here—you
just need to create a schema and that's it the
rest this React hook form
will handle automatically
Alright let's jump right into the video without
any delay—before that if you haven't checked out
the previous videos make sure to do so and if
you enjoy this one don't forget to give it a like
Share it with your friends
and if you haven 't
already subscribed to my awesome channel
Thapa Technical please do so because your
like share and subscribe mean a lot to me
gives a lot of courage yes let's upload it for free
are you getting it
alright so guys
let's head over to our VS Code editor
and by the way we've already gone over this
Here's how we can
create a schema with
the help of JWT where JWT stands for Z
O and then D—we've
already covered this
if you recall let me demonstrate JWT
we're talking about this
—so it's a TypeScript-
first schema validation
with static type inference
basically static type
inference it's very simple
now we have to create a
schema so first of all—
Let's go to
the folder structure that's
in our source folder
where we have features
and inside that an
applicant—and in this
applicant folder if
you look at the bottom—
I've kept it there
so I thought we should
keep everything in the applicant it 's
your applicant schema ts—I hope
you can see it clearly now here you can see
let's go to this
so guys you can see
this is our actual schema
where we have installed JWT
If you haven't done
so already please
go ahead and do it and we now have Z so if
you want to create a schema we have one
core function in JWT
and that is Z object
so once again it's an
object where you can see
that you've passed an
object and there every key
corresponds to its value
so the value means that
each key represents a field
in your table such as name
It's clearly in
our user's table so
it represents that now what is this
name what is its type and what do
you want to validate with this name
what do you want the user to enter in this field
what are its rules
which you need to specify here
that's your schema
so this is my name
this property must be a string
No one can write 1
2 3 here—they must
enter a string of at
least two characters
The name must be at least 2 characters long and
if I show it here you can
see the name must be at
least 2 characters long
so whatever error you enter
you can see it right here
whatever string you type
and then if you don't write at least 2
as soon as I write
it will disappear that's the beauty of it
we've implemented it in React hook form
at as soon as you type
it will go
you're validating it in real time
and as soon as you do this
it will come back
isn't it awesome
we have this email
The beauty of email
is that it'
s supported
by default so invalid
email addresses phone numbers or
strings
can be handled when we
insert them—so we'll
address it then string
minimum 10 z string
at least 2 characters
US or IN
date of birth
we have applied a refinement method
where you receive the value
whatever the user typed
and if it's
invalid after passing
en we will indicate an invalid date of birth
so this is crucial in the refine method
which is highly beneficial
nationality gender
these are standard
the key point regarding gender is
we have used an enum here
The best part of enum is
here—I have this you can
see gender underscore
option here
you have passed
an array
and we're writing
it in z enum so this is
simple thing all elements
in this array should be
selected
simple
and we need
to write
as const read-only
f you don't write
anything you'll get a
warning an overload
warning and
for some reason
whatever it is you'll get it then let's
go down
then we have marital
status and by
the way comma
again we have an object
then we have used the error function
here you can see the error
method is the fat error function
simple like
please select a gender
like it's your Zored version 4
earlier versions
ad required error
but here you need to do it like this
enum comma brackets
followed by the error method
the same applies to your marital
status education
since it's also an enum
just like
an array only
so inside it
All elements have been selected
that's it
experience your website
URL
alright
it's working
let's check it out
did they
add the URL
or not
define schema URL
let's take a look
so here's what we have
to validate any
WG-compatible URL
as you can see this is quite
permissive internally new URL constructor to validate inputs
to validate hostname
again
it's working
z url
we have z url
it will validate
please check out
our zored validation
and then biography
resume I want to tell you
in resume
I'm holding off on it for
now because we can definitely
do it—there are ways to
make it work We've already
uploaded materials even if it's not your image we
used the upload feature to
get it in I'll let you know
We've kept it simple:
minimum file size length
must be zero file
must be required resume
must be required—let
it be for now the rest
of the
fields I've already explained to you now
If you're using TypeScript
you must define the type
beauty of zored you don't
have to think
you just have to
see
here
you can see
this part
let me check
what we have
we have a z infer the special symbol
refers to the schema
inferred output type
z infer type of schema
similarly you can use this
the beauty here is
you don't have to type manually
what is the type
Zored is so powerful it
will automatically
tell you the type
The type of your schema
is what you need to write: angle brackets
and z infer — that's all
see
ype of my application
setting schema
type of
z infer
we will pass it
we don't have to worry
about type of
it will
handle it automatically
this is the beauty
z infer
it will handle it automatically
that's all
now we have a schema
now we need to use it
in our actual form
react-hook-
form by default
gives you a property
that's your resolver
this is like having
a resolver in an object
—you have to pass a
value get the zod
resolver and
then pass it
your schema
react-hook-form
whenever you submit a form
we have this handle-submit
before calling this function handle-subm
It will
verify tha
your schema — is it valid
or not — is it consistent
or not — it will verify —
if it's true — then your
function will execute —
proceed —
invoke the server
action and insert the data
— that's how it works —
You need to define your schema
and use this form
specifying the type
of the form you're using
our profile settings form
where we must provide a type
so that it's
easy for TypeScript
to identify the fields you
can use and secondly it will
provide you with suggestions
like if I hover here
it shows everything about what I can use
and what its
type can be
website URL
and biography
are optional which
is why both are
optional—you can write "
no tension" if you
wish I was wondering why
it hadn't come earlier
but now it's invalid
It was optional which
is why you can't see
it this way
now you've
added it but when you
submit either an error
appears or it runs automatically—so two
outcomes are possible
one is you are styling
your border if something
is wrong change the
border color to red
otherwise keep it as it is and below that we
are adding text—basically
here too you are using
a span or paragraph so
that you can see it here so let's check it out—
if I take you down for
some reason I have not
added anything on upload
photo because here we
have to upload image
So this code will change later
but this is your div
for our input field
and mostly for our
full name
if you see there
I've told you
pay attention to the class name
s is your left padding
pay attention here
in case there is an error in our
this field
our full name field
if there is an error
if it is true
there is a question mark
ternary operator
The border will be
destructive the focus will be
visible the ring will be
destructive if there is
no error then we are not
doing anything it will
remain as is now you
understand why the border is red
because of this class we'
ve applied
a conditional and
ultimately this error is clearly due
to what we 've
done—once again we're
using a condition in
JavaScript: if error name
is present then
and only the
We're using the p tag and
within it the error message comes
from errors name message which
is where we'retrieving the errors—
specifically I'm getting errors
from React Hook Form We have these
errors and when I
hover over them I
see the field names such as errors
f there is a
value means
it is triggered means it
is running
in a paragraph
tag what was the error
dot message by default
you are getting message
is a string you have to
do the same
for our email
We did the same thing
you have to do the same
Let's go with the
controller—you 'll
enjoy it
we have this controller
and we've done the same
in controller
I just realized
this data
I can loop through it
this is your task
we already have the schema
employers
settings
let me show you
what we've done organization type
and where it's coming from
from the employers'
schema I obtained it
and here it is displayed
—now it's your turn our applicant settings
form I don't want to
do manual work because
accuracy is crucial
I've already
created it in
applicant schema
and that 's
your gender option—now
you need to use it This is
your task try it The rest
remains the
same—we've used
the controller but we're
taking the default values
Hard-code the values
do it manually—we can
convert it into a single
line just do it I'll
tell you how to do it
better Our form validation
is complete That's
it for today's video
I hope you
like it like it
share it with your friends
Our next goal is 8
lakh subscribers —see you
in the next video thank you
so much take care