

Better Form Validation in React with Formik
source link: https://blog.openreplay.com/better-form-validation-in-react-with-formik
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Forms are integral to how users interact with our websites and web applications. Validating the data the user passes into the form is essential to our jobs as web developers. Formik is a free and open-source, lightweight form library for React. Formik is created for Scalability and High Performance: a form tool with minimal API that allows developers to build form fields with less code.
Formik is a flexible form library. It allows you to decide when and how much you want to use it. We can control how much functionality of the Formik library we use. Sure it can be used with HTML input fields and custom validations with yup. It was developed by Jared Palmer while working on a form field for his project, searching to standardize the input components and flow of form submission. The idea was to keep things organized and in a single place. Formik addresses three key points:
- Getting values in and out of form state
- Validation and error messages
- Handling form submission
We will consider all these points while building a sample React application — and as a bonus we’ll see an example of validations done with the Yup library.
Introduction
Before we learn how to use Formik, let’s have a little introduction and see how Formik works with a simple newsletter form. Imagine we want to add a newsletter sign-up form for a blog. To start with, our form will have just one field named email. With Formik, this is just a few lines of code.
We pass our form’s initialValues
and a submission function (onSubmit
) to the useFormik()
hook. The hook returns to us a goodie bag of form state and helper methods in a variable we call formik
. For now, the only helper methods we care about are as follows:
handleSubmit
: A submission handlerhandleChange
: A change handler to pass to each<input>
,<select>
, or<textarea>
values
: Our form’s current values
As you can see above, we pass each of these to their respective props… and that’s it! We can now have a working form powered by Formik. Instead of managing our form’s values on our own and writing our custom event handlers for every single input, we can just useFormik().

Advantages of Formik
What are the advantages of using Formik?
- Scalability: Formik is a good fit for a complete solution, including validation and handling form submission; we can also keep form state localized and managed by Formik, which is suitable for building multi-step forms.
- Size: Size has been one of the most significant advantages of Formik; with its minified gzipped size of 12.7kB, Formik still has the capacity of archiving form state with minimal APIs.
- Complementary Packages: Formik extends its capabilities by allowing complementary packages to come into play. One can integrate Yup with Formik for validation, or while using Redux State Management, you can also implement Formik for your form state (which is not possible when it comes to Redux form).
Also, let’s compare Formik with other top-rated form tools in React concerning their Performance, Development Experience, Components, Boilerplate, Popularity Contest, and more.
- Integrations: A form library like Redux Form doesn’t allow integrations, unlike Formik/React Hook Form. Formik and React Hook Form can integrate with any 3rd party app (especially Yup and Redux) for validations, and you can also use HTML5 inputs with Formik for the form field.
- Boilerplate: Boilerplate is something developers don’t want to get caught up with. Form tool libraries such as Redux Form/Final Form are faced with these issues, imagine you have to use a form tool like Final Form, and the whole place is covered with boilerplate; I guess that’s not an excellent way to start a form. On the other hand, Formik and React Hook Form constitute less boilerplate on your codes.
- Popularity Contest: For the popularity contest, we will draw up a table with all the above form libraries and compare them according to their Github stars/Forks/Weekly Download/Size.
Name | Github Stars | Fork | Weekly Downloads | Size |
Formik | 30.4k | 2.5k | 2,043,080 | 580 kB |
React Hook Form | 28.3k | 1.4k | 1,983,501 | 783 kB |
Redux Form | 12.6k | 1.7k | 404,429 | 1.45 MB |
Final Form | 2.8k | 210 | 478,508 | 191 kB |
With the data we have above, it seems Formik is leading the race with over 30k Github stars and, most importantly, 2M+ weekly downloads, a massive win for Formik. Formik is an excellent tool to work with, although the rest of the libraries also have good features.
Build Sign Up Page With Formik And Yup Validation
To fully understand this tutorial, please ensure that you’re familiar with React and have the latest version of Node and npm installed.
Let’s start by creating our project. Head to your terminal and run the following command;
Also, for us to be able to use Formik as our form tool, we need to install its dependencies with the following command;
We will build sign-in and sign-up forms to show how to use Formik, and add Yup for validations.
Creating a sign-up component
We will build a Signup component to accept Firstname, Email, and Password.
This form includes first and last name, email, age, password, and password confirmation fields.
Custom Input component
In CustomInput
, we’ll use useField
, a custom React hook that will enable one to hook up with inputs to formik.
In CustomInput
, useField()
returns [formik.getFieldProps(), formik.getFieldMeta()] which we can spread on <input>
. We can use field meta to show an error message if the field is invalid and it has been touched (i.e. visited). We also created such components for our CustomCheckbox
and CustomSelect
. The purpose is to make the component reusable in other parts of our code.
Sign-in component
Create a Sign In Component to accept Emails and Passwords and a Checkbox for confirmation.
Open Source Session Replay
OpenReplay is an open-source, session replay suite that lets you see what users do on your web app, helping you troubleshoot issues faster. OpenReplay is self-hosted for full control over your data.
Start enjoying your debugging experience - start using OpenReplay for free.
Implementing Layout
Although we can use any UI libraries such as Bootstrap, Material UI, or Chakra UI here, we’ll create a CSS file to make things a little bit easier.
If you look at the above picture, you will notice a red warning sign indicating Required; with our Yup Validation we can set the inputs as required, and if those places are not filled in, the user won’t be able to submit the form.
Our form is now fully functional in the sense that integrating Formik with Yup validation users/clients will have error pop-up text to fill the required data.

Wrap Up
With Formik creating lightweight, less code, scalable forms have been made easier. In this article, we covered the importance of having a form library like Formik, which tends to make form creation a lot easier for developers, who will also be able to implement the step-by-step process of making a Form with Formik and using Yup as our validation. For the live app click here, also for the source code on GitHub.
Resources
</div
Recommend
-
68
It’s not often that you write a web app that doesn’t contain at least one form. More often than naught, your entire app is just a series of forms. Each of these forms requires state management, event handlers, and, often...
-
33
Writing a React form without any additional dependencies isn’t much of a challenge itself. The same goes for applying client-side data validation. What might be troublesome is keeping all your forms consistent across a big...
-
22
How to speed up your React form development with Formik. John Au-Y...
-
19
Build Real-World React Native App #7: Send Feedbac...
-
10
How to Validate the Form Fields?It’s relatively easier to handle form validation in React Hook Form using the register hook. It also supports basic validation attributes such as req...
-
11
Forms Handling forms in React Native using Formik and Yup Apr 15, 2021...
-
8
-
10
-
19
-
8
Handling Form Data with TypeScript and Formik
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK