How To Add Form Validation to Your React App

Building an address book app with Formik, Yup, and React Bootstrap

React is a simple library for creating interactive front-end web apps. Its feature set is basic. It provides you with a component-based architecture for building web apps.

Each component does a small thing in an app and they can be nested in each other or put side-by-side. It does not come with form validation so building complex forms without a library will not be a pleasant experience.

If you want to create forms that do form validation, you need to use a third-party library.

Formik and Yup work great together and take care of most form validation needs. Formik let us build the forms, display the errors, and handle form value changes, which is another thing we’d otherwise have to do by hand.

Yup let us write a schema for validating our form fields. It can check almost anything, with common validation codes, like email and required fields, available as built-in functions. It can also check for fields that depend on other fields, like the postal code format, depending on country.

In this piece, we will build an address book app, which uses those libraries, plus React Bootstrap, which has great integration with the libraries above to create forms.

Getting Started

To start, we need to run Create React App to scaffold the app.

We run npx create-react-app address-book to create the app project folder with the initial files. The app will have a home page to display the contacts and lets us open a modal to add a contact.

There will be a table that displays all the contacts and edit and delete buttons on each row to edit or delete each contact. The contacts will store in a central Redux store, to store the contacts in a central place, making them easy to access.

React Router will be used for routing. Contacts will be saved in the back end, spawned using the JSON server package.

Once that is done, we have to install some libraries. To install the libraries we mentioned above, we run npm i axios bootstrap formik react-bootstrap react-redux react-router-dom yup.

Axios is the HTTP client that we use for making HTTP requests to the back end. react-router-dom is the package name for the latest version of React Router.

Now that we have all the libraries installed, we can start building the app. All files will be in the src folder unless mentioned otherwise.

First, we work on the Redux store. We create a file called actionCreator.js in the src folder and add the following:

import { SET_CONTACTS } from './actions';const setContacts = (contacts) => {
return {
payload: contacts
};export { setContacts };

This is the action creator to create the action of storing the contacts in the store.

We create another file, called actions.js, and add:


This has the type constant for dispatching the action.

In App.js, we replace the existing code with the following:

import React from 'react';
import { Router, Route, Link } from "react-router-dom";
import HomePage from './HomePage';
import { createBrowserHistory as createHistory } from 'history'
import Navbar from 'react-bootstrap/Navbar';
import Nav from 'react-bootstrap/Nav';
import './App.css';
const history = createHistory();function App() {
return (
<div className="App">
<Router history={history}>
<Navbar bg="primary" expand="lg" variant="dark" >
<Navbar.Brand href="#home">Address Book App</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/">Home</Nav.Link>
<Route path="/" exact component={HomePage} />
}export default App;

This is where we add the navigation bar and show our routes routed by the React Router. In App.css, to center text, we replace the existing code with:

.App {
text-align: center;

Next, we build our contact form. This is the most logic-heavy part of our app. We create a file called ContactForm.js and add:

import React from 'react';
import { Formik } from 'formik';
import Form from 'react-bootstrap/Form';
import Col from 'react-bootstrap/Col';
import InputGroup from 'react-bootstrap/InputGroup';
import Button from 'react-bootstrap/Button';
import * as yup from 'yup';
import { COUNTRIES } from './exports';
import PropTypes from 'prop-types';
import { addContact, editContact, getContacts } from './requests';
import { connect } from 'react-redux';
import { setContacts } from './actionCreators';const schema = yup.object({
firstName: yup.string().required('First name is required'),
lastName: yup.string().required('Last name is required'),
address: yup.string().required('Address is required'),
city: yup.string().required('City is required'),
region: yup.string().required('Region is required'),
country: yup.string().required('Country is required').default('Afghanistan'),
postalCode: yup
.when('country', {
is: 'United States',
then: yup.string().matches(/^[0-9]{5}(?:-[0-9]{4})?$/, 'Invalid postal code'),
.when('country', {
is: 'Canada',
then: yup.string().matches(/^[A-Za-z]\d[A-Za-z][ -]?\d[A-Za-z]\d$/, 'Invalid postal code'),
phone: yup
.when('country', {
is: country => ["United States", "Canada"].includes(country),
then: yup.string().matches(/^[2-9]\d{2}[2-9]\d{2}\d{4}$/, 'Invalid phone nunber')
email: yup.string().email('Invalid email').required('Email is required'),
age: yup.number()
.required('Age is required')
.min(0, 'Minimum age is 0')
.max(200, 'Maximum age is 200'),
});function ContactForm({
}) {
const handleSubmit = async (evt) => {
const isValid = await schema.validate(evt);
if (!isValid) {
if (!edit) {
await addContact(evt);
else {
await editContact(evt);
const response = await getContacts();
}return (
<div className="form">
initialValues={contact || {}}
}) => (
<Form noValidate onSubmit={handleSubmit}>
<Form.Group as={Col} md="12" controlId="firstName">
<Form.Label>First name</Form.Label>
placeholder="First Name"
value={values.firstName || ''}
isInvalid={touched.firstName && errors.firstName}
<Form.Control.Feedback type="invalid">
<Form.Group as={Col} md="12" controlId="lastName">
<Form.Label>Last name</Form.Label>
placeholder="Last Name"
value={values.lastName || ''}
isInvalid={touched.firstName && errors.lastName}
/><Form.Control.Feedback type="invalid">
<Form.Group as={Col} md="12" controlId="address">
value={values.address || ''}
isInvalid={touched.address && errors.address}
<Form.Control.Feedback type="invalid">
<Form.Group as={Col} md="12" controlId="city">
value={ || ''}
isInvalid={ &&}
/><Form.Control.Feedback type="invalid">
<Form.Group as={Col} md="12" controlId="region">
value={values.region || ''}
isInvalid={touched.region && errors.region}
<Form.Control.Feedback type="invalid">
</Form.Group><Form.Group as={Col} md="12" controlId="country">
value={ || ''}
isInvalid={touched.region &&}>
{ => <option key={c} value={c}>{c}</option>)}
<Form.Control.Feedback type="invalid">
</Form.Group><Form.Group as={Col} md="12" controlId="postalCode">
<Form.Label>Postal Code</Form.Label>
placeholder="Postal Code"
value={values.postalCode || ''}
isInvalid={touched.postalCode && errors.postalCode}
/><Form.Control.Feedback type="invalid">
</Form.Group><Form.Group as={Col} md="12" controlId="phone">
value={ || ''}
isInvalid={ &&}
/><Form.Control.Feedback type="invalid">
</Form.Group><Form.Group as={Col} md="12" controlId="email">
value={ || ''}
isInvalid={ &&}
/><Form.Control.Feedback type="invalid">
</Form.Group><Form.Group as={Col} md="12" controlId="age">
value={values.age || ''}
isInvalid={touched.age && errors.age}
/><Form.Control.Feedback type="invalid">
<Button type="submit" style={{ 'marginRight': '10px' }}>Save</Button>
<Button type="button" onClick={edit ? onCancelEdit : onCancelAdd}>Cancel</Button>
}ContactForm.propTypes = {
edit: PropTypes.bool,
onSave: PropTypes.func,
onCancelAdd: PropTypes.func,
onCancelEdit: PropTypes.func,
contact: PropTypes.object
}const mapStateToProps = state => {
return {
contacts: state.contacts,
}const mapDispatchToProps = dispatch => ({
setContacts: contacts => dispatch(setContacts(contacts))
})export default connect(

We use Formik here, to facilitate building our contact form, with our Boostrap Form component nested in the Formik component, so we can use Formik’s handleChange, handleSubmit, values, touched, and errors parameters.

handleChange is a function that lets us update the form field data from the inputs without writing the code ourselves. handleSubmit is the function that we passed into the onSubmit handler of the Formik component.

The parameter in the function is the data we entered, with the field name as the key, as defined by the name attribute of each field and the value of each field as the value of those keys.

Notice that, in each value prop, we have ||'' so we do not get undefined values and prevent uncontrolled form warnings from getting triggered.

To display form validation messages, we have to pass in the isInvalid prop to each Form.Control component. The schema object is what Formik will check against for form validation.

The argument in the required function is the validation error message. The second argument of the matches, min, and max functions are also validation messages.

The parameter of the ContactForm function are props, which we will pass in from the HomePage component that we will build later.

The handleSubmit function checks if the data is valid and if it is, it will proceed to saving according to whether it is adding or editing a contact.

When saving is successful we set the contacts in the store and call onSave prop, which is a function to close the modal that the form is in. The modal will be defined on the home page.

mapStateToProps is a function provided by React Redux so we can map the state directly to the props of our component, as the function name suggests.

mapDispatchToProps allows us to call a function in the props of the component, called setContacts, to dispatch the action, as we defined in actionCreators.js.

Next, we create a file called exports.js, and put:

export const COUNTRIES = ["Afghanistan", "Albania", "Algeria", "Andorra", "Angola", "Anguilla", "Antigua &amp; Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas"
, "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia &amp; Herzegovina", "Botswana", "Brazil", "British Virgin Islands"
, "Brunei", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Chad", "Chile", "China", "Colombia", "Congo", "Cook Islands", "Costa Rica"
, "Cote D Ivoire", "Croatia", "Cruise Ship", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea"
, "Estonia", "Ethiopia", "Falkland Islands", "Faroe Islands", "Fiji", "Finland", "France", "French Polynesia", "French West Indies", "Gabon", "Gambia", "Georgia", "Germany", "Ghana"
, "Gibraltar", "Greece", "Greenland", "Grenada", "Guam", "Guatemala", "Guernsey", "Guinea", "Guinea Bissau", "Guyana", "Haiti", "Honduras", "Hong Kong", "Hungary", "Iceland", "India"
, "Indonesia", "Iran", "Iraq", "Ireland", "Isle of Man", "Israel", "Italy", "Jamaica", "Japan", "Jersey", "Jordan", "Kazakhstan", "Kenya", "Kuwait", "Kyrgyz Republic", "Laos", "Latvia"
, "Lebanon", "Lesotho", "Liberia", "Libya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Mauritania"
, "Mauritius", "Mexico", "Moldova", "Monaco", "Mongolia", "Montenegro", "Montserrat", "Morocco", "Mozambique", "Namibia", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia"
, "New Zealand", "Nicaragua", "Niger", "Nigeria", "Norway", "Oman", "Pakistan", "Palestine", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Poland", "Portugal"
, "Puerto Rico", "Qatar", "Reunion", "Romania", "Russia", "Rwanda", "Saint Pierre &amp; Miquelon", "Samoa", "San Marino", "Satellite", "Saudi Arabia", "Senegal", "Serbia", "Seychelles"
, "Sierra Leone", "Singapore", "Slovakia", "Slovenia", "South Africa", "South Korea", "Spain", "Sri Lanka", "St Kitts &amp; Nevis", "St Lucia", "St Vincent", "St. Lucia", "Sudan"
, "Suriname", "Swaziland", "Sweden", "Switzerland", "Syria", "Taiwan", "Tajikistan", "Tanzania", "Thailand", "Timor L'Este", "Togo", "Tonga", "Trinidad &amp; Tobago", "Tunisia"
, "Turkey", "Turkmenistan", "Turks &amp; Caicos", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay"
, "Uzbekistan", "Venezuela", "Vietnam", "Virgin Islands (US)", "Yemen", "Zambia", "Zimbabwe"];

These are the countries for the countries field in the form.

In HomePage.js, we put:

import React from 'react';
import { useState, useEffect } from 'react';
import Table from 'react-bootstrap/Table'
import ButtonToolbar from 'react-bootstrap/ButtonToolbar';
import Button from 'react-bootstrap/Button';
import Modal from 'react-bootstrap/Modal'
import ContactForm from './ContactForm';
import './HomePage.css';
import { connect } from 'react-redux';
import { getContacts, deleteContact } from './requests';function HomePage() {
const [openAddModal, setOpenAddModal] = useState(false);
const [openEditModal, setOpenEditModal] = useState(false);
const [initialized, setInitialized] = useState(false);
const [selectedId, setSelectedId] = useState(0);
const [selectedContact, setSelectedContact] = useState({});
const [contacts, setContacts] = useState([]);const openModal = () => {
}const closeModal = () => {
}const cancelAddModal = () => {
}const editContact = (contact) => {
}const cancelEditModal = () => {
}const getData = async () => {
const response = await getContacts();
}const deleteSelectedContact = async (id) => {
await deleteContact(id);
}useEffect(() => {
if (!initialized) {
})return (
<div className="home-page">
<Modal show={openAddModal} onHide={closeModal} >
<Modal.Header closeButton>
<Modal.Title>Add Contact</Modal.Title>
<ContactForm edit={false} onSave={closeModal.bind(this)} onCancelAdd={cancelAddModal} />
</Modal><Modal show={openEditModal} onHide={closeModal}>
<Modal.Header closeButton>
<Modal.Title>Edit Contact</Modal.Title>
<ContactForm edit={true} onSave={closeModal.bind(this)} contact={selectedContact} onCancelEdit={cancelEditModal} />
<ButtonToolbar onClick={openModal}>
<Button variant="outline-primary">Add Contact</Button>
<br />
<Table striped bordered hover>
<th>First Name</th>
<th>Last Name</th>
<th>Postal Code</th>
{ => (
<tr key={}>
<Button variant="outline-primary" onClick={editContact.bind(this, c)}>Edit</Button>
<Button variant="outline-primary" onClick={deleteSelectedContact.bind(this,}>Delete</Button>
}const mapStateToProps = state => {
return {
contacts: state.contacts,
}export default connect(

It has the table to display the contacts and buttons to add, edit, and delete contacts. It gets data once on first load with the getData function call in the useEffect‘s callback function.

The useEffect‘s callback is called on every render so we want to set an initialized flag and check that it loads only if it’s true.

Note that we pass in all the props in this component to the ContactForm component.

To pass an argument to an onClick handler function, we have to call bind on the function and pass in the argument for the function as a second argument to bind.

For example, in this file, we have editContact.bind(this, c), where c is the contact object. The editContact function is defined as follows:

const editContact = (contact) => {

c is the contact parameter we pass in.

Next, we create a file, called HomePage.css, and put:

.home-page {
padding: 20px;

To add some padding.

In index.js, we replace the existing code with:

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import * as serviceWorker from './serviceWorker';
import { contactsReducer } from './reducers';
import { Provider } from 'react-redux'
import { createStore, combineReducers } from 'redux'const addressBookApp = combineReducers({
contacts: contactsReducer,
})const store = createStore(addressBookApp)ReactDOM.render(
<Provider store={store}>
<App />
, document.getElementById('root'));// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
// Learn more about service workers:

We combined the reducers and created the store, then injected it into our app with the Provider component so that we can use it everywhere in the app.

Then, we make a file, called reducers.js, and add:

import { SET_CONTACTS } from './actions';function contactsReducer(state = {}, action) {
switch (action.type) {
state = JSON.parse(JSON.stringify(action.payload));
return state;
return state
}export { contactsReducer };

This is the reducer where we store the contacts that we dispatch by calling the prop provided by the mapDispatchToProps function in our components.

We create a file, called requests.js, and add:

const APIURL = 'http://localhost:3000';
const axios = require('axios');
export const getContacts = () => axios.get(`${APIURL}/contacts`);
export const addContact = (data) =>`${APIURL}/contacts`, data);
export const editContact = (data) => axios.put(`${APIURL}/contacts/${}`, data);
export const deleteContact = (id) => axios.delete(`${APIURL}/contacts/${id}`);

These are functions that making our HTTP requests to the back end to save and delete contacts.

Finally, in public/index.html, we replace the existing code with:

<!DOCTYPE html>
<html lang="en"><head>
<meta charset="utf-8" />
<link rel="shortcut icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta name="description" content="Web site created using create-react-app" />
<link rel="apple-touch-icon" href="logo192.png" />
<link rel="manifest" crossorigin="use-credentials" href="%PUBLIC_URL%/manifest.json" /><!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
<title>React Address Book App</title>
<link rel="stylesheet" href=""
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous" />
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.

To change the title and add the Bootstrap stylesheet.

Now we can run the app by running set PORT=3001 && react-scripts start on Windows or PORT=3006 react-scripts start on Linux.

To start the back end, we first install the json-server package by running npm i json-server. Then, go to our project folder and run:

json-server --watch db.json

In db.json, change the text to:

"contacts": [

So we have the contacts endpoints defined in the requests.js available.

In the end, we have the following: