Blog / React

How to Build Scalable Forms with Formik using Advanced Techniques and Validation

How to Build Scalable Forms with Formik using Advanced Techniques and Validation

In this post find out about how to Build Scalable Forms with Formik using Advanced Techniques and Validation

Will MaygerWill Mayger
June 10, 2023
Article

Formik is a powerful form library for React that simplifies the process of building complex and scalable forms. It provides an intuitive API, handles form state management, and offers advanced features like validation, error handling, and form submission. In this article, we will explore how to leverage Formik to build scalable forms using advanced techniques and validation. By the end of this article, you will have a solid understanding of how to use Formik to create robust and user-friendly forms in your React applications.

Table of Contents

  • Introduction to Formik
  • Setting Up a Formik Project
  • Building Basic Forms with Formik
  • Handling Form Validation with Yup
  • Custom Validation and Error Messages
  • Handling Form Submission
  • Managing Complex Forms with Nested Fields
  • Dynamic Forms with Formik
  • Integrating Formik with UI Libraries
  • Optimizing Form Performance
  • Conclusion

Introduction to Formik

Formik is a form library for React that aims to simplify the process of building and managing forms. It provides an intuitive and declarative API for handling form state, validation, submission, and error handling. Formik is built on the principles of React’s unidirectional data flow, making it easy to integrate with existing React components and state management libraries. With Formik, you can reduce boilerplate code, improve code maintainability, and enhance the user experience of your forms.

Setting Up a Formik Project

To get started with Formik, you need to set up a new React project and install the necessary dependencies. Follow these steps to create a new React project and install Formik:

  1. Initialize a new React project:
npx create-react-app formik-app
cd formik-app
  1. Install Formik and Yup (a popular validation library):
npm install formik yup

With these steps, you have set up a basic React project and installed Formik and Yup.

Building Basic Forms with Formik

Formik makes it easy to build basic forms by handling form state management and providing utility functions and components. To create a basic form with Formik, follow these steps:

  1. Import the necessary dependencies:
import { Formik, Field, ErrorMessage } from 'formik';
  1. Define the initial form values:
const initialValues = {
  name: '',
  email: '',
  password: '',
};
  1. Define the form validation schema using Yup:
import * as Yup from 'yup';

const validationSchema = Yup.object().shape({
  name: Yup.string().required('Name is required'),
  email: Yup.string().email('Invalid email').required('Email is required'),
  password: Yup.string().required('Password is required'),
});
  1. Create the form component:
const MyForm = () => {
  const handleSubmit = (values) => {
    // Handle form submission logic
    console.log(values);
  };

  return (
    <Formik
      initialValues={initialValues}
      validationSchema={validationSchema}
      onSubmit={handleSubmit}
    >
      <form>
        <div>
          <label htmlFor="name">Name:</label>
          <Field type="text" id="name" name="name" />
          <ErrorMessage name="name" component="div" />
        </div>
        <div>
          <label htmlFor="email">Email:</label>
          <Field type="email" id="email" name="email" />
          <ErrorMessage name="email" component="div" />
        </div>
        <div>
          <label htmlFor="password">Password:</label>
          <Field type

="password" id="password" name="password" />
          <ErrorMessage name="password" component="div" />
        </div>
        <button type="submit">Submit</button>
      </form>
    </Formik>
  );
};

In this example, we create a MyForm component that uses Formik to handle form state, validation, and submission. The Formik component wraps the form and provides the necessary context for Formik to work. We define the initial form values, validation schema, and form submission logic. The Field component is used to render form inputs, and the ErrorMessage component displays validation errors.

Handling Form Validation with Yup

Formik integrates seamlessly with Yup, a powerful validation library. Yup allows you to define a validation schema to validate form values. To handle form validation with Yup, follow these steps:

  1. Define the validation schema using Yup:
import * as Yup from 'yup';

const validationSchema = Yup.object().shape({
  name: Yup.string().required('Name is required'),
  email: Yup.string().email('Invalid email').required('Email is required'),
  password: Yup.string().required('Password is required'),
});

In this example, we define a validation schema using Yup’s chainable API. We specify the validation rules for each form field and provide custom error messages for invalid values.

  1. Pass the validation schema to the Formik component:
<Formik
  initialValues={initialValues}
  validationSchema={validationSchema}
  onSubmit={handleSubmit}
>
  {/* Form fields and components */}
</Formik>

By passing the validation schema to the Formik component, Formik automatically validates the form values against the defined schema.

  1. Display validation errors using the ErrorMessage component:
<ErrorMessage name="name" component="div" />

The ErrorMessage component renders the validation error message for a specific form field. It receives the name prop, which corresponds to the field name in the validation schema.

Custom Validation and Error Messages

Formik allows you to define custom validation rules and error messages to suit your specific form requirements. To define custom validation rules, follow these steps:

  1. Define a custom validation function:
const customValidation = (value) => {
  if (value !== 'custom') {
    return 'Value must be "custom"';
  }
};
  1. Use the custom validation function in the validation schema:
const validationSchema = Yup.object().shape({
  customField: Yup.string().test('custom', customValidation),
});

In this example, we define a custom validation function called customValidation. We then use the test method of Yup to add a custom validation rule to the customField. If the value doesn’t match the specified condition, the error message defined in the validation function will be displayed.

Handling Form Submission

Formik provides an easy way to handle form submission with its onSubmit prop. To handle form submission, follow these steps:

  1. Define the form submission logic:
const handleSubmit = (values) => {
  // Handle form submission logic
  console.log(values);
};
  1. Pass the submission logic to the onSubmit prop of the Formik component:
<Formik
  initialValues={initialValues}
  onSubmit={handleSubmit}
>
  {/* Form fields and components */}
</Formik>

By providing the onSubmit prop, Formik handles form submission and passes the form values to the submission logic defined in the callback function.

Managing Complex Forms with Nested Fields

Formik supports managing complex forms with

nested fields, such as arrays or objects. To manage complex forms, follow these steps:

  1. Define the initial form values with nested fields:
const initialValues = {
  name: '',
  address: {
    street: '',
    city: '',
  },
};
  1. Use the dot notation to access nested fields in the form:
<Field type="text" id="address.street" name="address.street" />
  1. Define the nested validation schema:
const validationSchema = Yup.object().shape({
  name: Yup.string().required('Name is required'),
  address: Yup.object().shape({
    street: Yup.string().required('Street is required'),
    city: Yup.string().required('City is required'),
  }),
});

By following these steps, you can manage and validate complex forms with nested fields using Formik and Yup.

Dynamic Forms with Formik

Formik provides support for dynamically adding and removing form fields. This is useful when dealing with forms that allow the user to add multiple items or when the form structure is determined dynamically. To create dynamic forms with Formik, follow these steps:

  1. Use arrays to manage dynamic form fields:
<FieldArray name="items">
  {({ push, remove }) => (
    <div>
      {values.items.map((item, index) => (
        <div key={index}>
          <Field type="text" name={`items[${index}]`} />
          <button type="button" onClick={() => remove(index)}>
            Remove
          </button>
        </div>
      ))}
      <button type="button" onClick={() => push('')}>
        Add Item
      </button>
    </div>
  )}
</FieldArray>
  1. Define validation rules for dynamic form fields:
const validationSchema = Yup.object().shape({
  items: Yup.array().of(
    Yup.string().required('Item is required')
  ),
});

In this example, we use the FieldArray component from Formik to manage an array of form fields. We map over the values.items array and render form fields for each item. The user can add new items by clicking the “Add Item” button and remove items by clicking the “Remove” button. The validation schema ensures that each item in the array is a non-empty string.

Integrating Formik with UI Libraries

Formik can be integrated with various UI libraries and components to enhance form building and styling. UI libraries like Material-UI, Ant Design, and Semantic UI provide pre-built components that can be used with Formik. To integrate Formik with UI libraries, follow these steps:

  1. Install the UI library of your choice:
npm install @mui/material  // For Material-UI
npm install antd  // For Ant Design
npm install semantic-ui-react  // For Semantic UI
  1. Import and use the UI library components with Formik:
import { TextField, Button } from '@mui/material';  // For Material-UI
import { Input, Button } from 'antd';  // For Ant Design
import { Input, Button } from 'semantic-ui-react';  // For Semantic UI

// ...

<Field component={TextField} name="name" label="Name" />
<Button type="submit">Submit</Button>

By importing the UI library components and using them as the component prop for the Field component, you can leverage the styling and functionality provided by the UI library while still benefiting from Formik’s form management capabilities.

Optimizing Form Performance

When working with large and complex forms, it’s important to optimize form performance to ensure a smooth user experience.

Here are some tips to optimize form performance with Formik:

  1. Field-Level Validation: Instead of validating the entire form on every change, consider using field-level validation to validate individual fields as the user interacts with them. This reduces unnecessary validation and improves performance.

  2. Debounced Form Submission: When dealing with forms that trigger intensive operations, like API requests or calculations, use a debounce function to delay the form submission until the user stops typing or interacting with the form. This prevents frequent and unnecessary submissions.

  3. Conditional Rendering: If your form contains conditional fields or sections that are hidden based on user interactions, optimize rendering by conditionally rendering those elements only when necessary. This reduces the number of DOM elements and improves performance.

  4. Form State Chunking: If your form has a large number of fields or complex state objects, consider splitting the form into smaller logical components using techniques like field grouping or form sections. This allows you to manage the form state more efficiently and improves rendering performance.

By following these optimization techniques, you can ensure that your forms built with Formik perform well and provide a responsive user experience.

Conclusion

In this article, we explored how to build scalable forms with Formik using advanced techniques and validation. We learned how to set up a Formik project, build basic forms, handle form validation with Yup, implement custom validation and error messages, handle form submission, manage complex forms with nested fields, create dynamic forms, integrate Formik with UI libraries, optimize form performance, and more. Formik empowers us to create robust and user-friendly forms in React applications, saving time and effort in form development. With Formik’s powerful features and flexibility, you can build scalable forms that provide an excellent user experience.

There we have how to Build Scalable Forms with Formik using Advanced Techniques and Validation, if you want more like this be sure to check out some of my other posts!

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app

Some graphics used on this post were made using icons from flaticon.

Latest Posts

Learn React, JavaScript and TypeScript

Learn React, JavaScript and TypeScript

Join the platform that top tier companies are using.
Master the skills you need to succeed as a software engineer and take your career to the next level with Pluralsight.

Start here

Become an expert in ReactJS, TypeScript, and JavaScript.

Here you will find my personal recomendations to you, for full disclosure I earn a small commission from some of these links, but I only recommend what I trust and personally use.

Good things are coming, don't miss out!

Good things are coming, don't miss out!

Follow me on Twitter to stay up to date and learn frontend, React, JavaScript, and TypeScript tips and tricks!

Are you a novice, intermediate or expert react engineer?

Find out here by taking my fun, interactive, quick quiz which takes approximately 1 - 3 minutes. How well will you will do?

Foxi - Budget Planner & Tracker

Foxi

Budget Planner & Tracker

More money in your pocket by the end of the month.

Free to use and no account needed.

Get started now.

Get the app