Handle forms painlessly with Formcat
Formcat aims to make life easier for developers building forms. Come find out what it is and how to use it!

Editorial translation by Impulso.
Hey, devs! Today I’m going to talk a bit about Formcat, a library that’s been a huge help to me when working with forms in React.
If you’ve ever worked with React, at some point you’ve probably had to build forms with validation, state management, and all the other details. You’ve seen how much work it takes, and we often end up spending time on something that was supposed to be simple.
Well, Formcat aims to make life easier for developers building these forms, and I’ll show you how.
What is it?
Formcat is a library for building forms. It was built using the React Context API and manages its own state. It lets us set up one or more validations for a specific field, ensures the form is validated, and makes it easy to retrieve the data users enter. We can do all of this with the Fields that come with Formcat or by creating our own Fields.
How do you use it?
Okay. I’ve written quite a bit so far, but I haven’t actually said anything yet, have I? So let’s get hands-on.
First things first, let’s install Formcat:
yarn add formcat
# or
npm install --save formcat
With the library installed, let’s build our form. It’ll have an input of type text, a select, a radio, a textarea, and a checkbox. To do that, we’ll use all the Fields that come with Formcat. Then I’ll show you how to create a custom one.
Here we go. This is the code for our form.
import { Form, Submit } from 'formcat'
import {
InputField,
TextField,
SelectField,
RadiosField,
CheckboxField
} from "formcat/fields";
...
<Form onSubmit={handleSubmit}>
<InputField
label="Nome"
name="nome"
required
/>
<SelectField
label="Que tipo de dev você se considera?"
name="dev"
required
options={[
{ label: "---", value: "" },
{ label: "Frontend", value: "frontend" },
{ label: "Backend", value: "backend" },
{ label: "Fullstack", value: "fullstack" },
{ label: "Outro", value: "outro" }
]}
/>
<RadiosField
label="Quanto tempo trabalha como dev?"
name="tempo"
required
options={[
{ label: "1 - 3 anos", value: "1-3" },
{ label: "3 - 5 anos", value: "3-5" },
{ label: "+5 anos", value: "+5" }
]}
/>
<TextField
label="Fale um pouco sobre você"
name="sobre"
rows="10"
/>
<CheckboxField
label="Estou de acordo em enviar minha informações"
name="deacordo"
required
/>
<Submit>Enviar</Submit>
</Form>
Okay, now I’ll walk you through everything we used in this form:
Form is our main component. It creates the context and holds our form’s state. In this case, we pass it just one prop, onSubmit. This prop returns the data entered in the form. You can see all the props it accepts by clicking here.
InputField is our input="text"
SelectField is our select, and it takes an options prop that defines the list of items to display.
RadiosField is our input="radio". It takes the same options prop as SelectField to build the radio buttons.
TextField is our textarea
CheckboxField is our input="checkbox"
All the fields we’ve used have one required prop, name. It defines the name under which the field is registered in the form context. To see all the props that Fields can take, check the documentation by clicking here.
And finally, there’s the Submit component, which submits our form. It’s enabled only when the form is valid, so empty required fields or fields with a validations prop that lack an assert will keep the submit button disabled.
But Guilherme... validations? You haven’t said anything about that! Right, let’s talk about it now.
Formcat lets us validate any field integrated with it using the validations prop. It takes a list of functions that return *true* or *false* and considers the field valid only if every function returns *true*. So you can add as many validations as you like to the same field.
Let’s look at an example:
Imagine we need users to enter at least a first and last name in our form’s *name* field, so they can’t enter just a first name.
Our validation would look like this:
function hasMoreThanOne (value) {
return value.trim().split(' ').length > 1;
}
Here, value is always the value entered in the field.
If you’d like to learn a bit more about validations, you can check the documentation by clicking here.
Let’s see our form in action below.
Now that I know how to use Formcat’s Fields, I want to create my own fields. How do I do that?
Let’s do it!
Creating custom Fields
To create custom fields, we’ll use an HOC called withContextForm, and it’s pretty simple. As an example, we’ll create a basic input="text".
import { withFormContext } from 'formcat'
Once that’s done, using the Field component inside the Form automatically integrates it with the context. You can read more about creating a custom field by clicking here. Or, for example, you can see how InputField was built by clicking here.
Below is an example from the documentation of a form with custom fields.
Well, folks, that’s it... I hope it made sense and that you enjoyed it. Most of all, I hope Formcat can help you the way it’s been helping me... and feel free to contribute.
Btw, this is a great project for anyone who wants to contribute to open source. Feel free to submit Pull Requests, open Issues, give it a Star, and share it with a friend.
If you have any questions, feel free to reach out. You can find me on Twitter as @guilhermelouro or in the Impulso chat as @guilherme-louro.


