Easy Form validation with YUP (+ Formik) in react (For beginners)

Software engineer
Search for a command to run...

Software engineer
No comments yet. Be the first to comment.
"Talk is cheap. Show me the code." β Linus Torvalds β¨ How can you call yourself a good painter if you have not made any painting in life. you canβt be good artist just by reading books or passing comments on other paint works. If you think you are go...
This command is just to give some description about what you did so far, this will help camera to remember even if you check it after a year. It can tell you exactly what, when, who was doing this. Now you have the exact copy of your original work, ...
1) Project Setup: I am going to use the composer to setup everything here. composer create-project laravel/laravel simple-project-name This will basically setup the new laravel project in your machine. Once the setup is complete, cd simple-project-na...

We just need 3 things to work, React , Formik & YUP (Obviously). π€¦

I will be assuming that you already setup the react and just looking something for the validating the form.
Just incase if you haven't, here is the link to setup the react link. π
Setting up the Formik is also dead easy,
just use the NPM or the YARN to install the formik on your project.
npm: npm install formik --save
yarn: yarn add formik
Now we need to setup the form, for that we need the Formik.
We can either useFormik hooks or directly import the Formik.
// by implementing useFormik() hooks
const formik = useFormik({
initialValues: {},
onSubmit: values => {}, ...
});
or directly using the Formik as the form, more classical approach per say,
// using Formik as the Form.
<Formik onSubmit={} initialValues={}>
</Formik>
I will be using the formik hooks for this tutorial, feel free to use whatever you want. Below we have the very simple Form made from the formik.
import React from "react";
import { useFormik } from "formik";
export default function App() {
const formik = useFormik({
initialValues: {
email: ""
},
onSubmit: (values) => {
// simple code
}
});
return (
<form onSubmit={formik.handleSubmit}>
<input
id="email"
name="email"
type="email"
onChange={formik.handleChange}
value={formik.values.email}
placeholder="Email"
/>
<button type="submit">Submit</button>
</form>
);
}
Output:

Finally, lets add the YUP validation on the form.
Installing yup on the project using npm and yarn:
npm install yup or yarn add yup
For the sake of easiness, Let's say we want to add the validation where email must not be empty, or Required validation on email field.
First, import the YUP package where your code is, for me i will import on the code above.
import React from "react";
import { useFormik } from "formik";
// import like this
import * as yup from "yup";
We need the create the validation schema, which is also very simple.
import React from "react";
import { useFormik } from "formik";
// import like this
import * as yup from "yup";
const formSchema = yup.object().shape({
email: yup.string().required('Required'),
// This will make sure that the email is string,
// and value must not be empty.
});
You can put any error message inside the required("message-to-show-on-error")
Now all we have to do is link the yup schema and the formik form
import React from "react";
import { useFormik } from "formik";
// import like this
import * as yup from "yup";
const formSchema = yup.object().shape({
email: yup.string().required('Required'),
// This will make sure that the email is string,
// and value must not be empty.
});
export default function App() {
const formik = useFormik({
initialValues: {
email: ""
},
// >>> Link schema here <<<
validationSchema: formSchema,
onSubmit: (values) => {
// simple code
}
});
return (
<form onSubmit={formik.handleSubmit}>
<input
id="email"
name="email"
type="email"
onChange={formik.handleChange}
value={formik.values.email}
placeholder="Email"
/>
<button type="submit">Submit</button>
</form>
);
}
<span>{formik.errors.email}</span>
Final code will look something like this π
import React from "react";
import { useFormik } from "formik";
import * as yup from "yup";
export default function App() {
const formSchema = yup.object().shape({
email: yup.string().required("Required")
});
const formik = useFormik({
initialValues: {
email: ""
},
validationSchema: formSchema,
onSubmit: (values) => {
// simple code
console.log(values);
}
});
return (
<form onSubmit={formik.handleSubmit}>
<div>
<input
id="email"
name="email"
type="email"
onChange={formik.handleChange}
value={formik.values.email}
placeholder="Email"
/>
<br></br>
<span>{formik.errors.email}</span>
</div>
<button type="submit">Submit</button>
</form>
);
}
Output:
And obviously looks ugly, which can be fixed with simple css/ styling.
Congratulations! ππ you have successfully used the formik and yup validation.

Yup is super powerful, we can do all sorts of stuff like:
email: Yup.string().email('Invalid email address').required('Required'),
To check for the valid email input + must not be emptyfirstName: Yup.string()
.max(15, 'Must be 15 characters or less')
.required('Required'),
To check for the non empty firstname field + firstName length of 15 characterage: yup.number().required().positive().integer()
To check if the age is positive number + non empty I will leave the example code here: https://codesandbox.io/s/vigilant-lamarr-fhscbj?file=/src/App.js:0-817
Have fun experimenting on it. π¨βπ¬
Byeeee πππππ
