How many forms have you built? 1? 2? 5 bazillion?

Lemme guess, every project involves 1 or 2 forms and they’re all the friggen same. You got forms coming out the wazoo.

Render fields. Connect to state. Add field validation. Detect submit. Add form validation. Send fetch request. Repeat.

shoot_me giphy

How many times can you go through that mind-numbing software bureaucracy before you say “Enough already!”

And no, that doesn’t mean “Give it to the most junior engineer on the team coz they still think it’s exciting” either. That’s shitty. (and yes every company does it)

Build a form generator

Here’s what you do instead my friend, you notice a pattern and you solve it once and for all. For every future project. For every engineer on the team. For yourself.

That’s how you level up.

Click through for source

Next time you’re building a form, grab some extra time, turn it into a library.

Sure, libraries like formik and react-form and redux-form exist. There’s a bunch for every frontend framework.

And they’re not what I’m talking about.

What you need is a form generator. A piece of code that takes JSON configuration and spits out a form.

A form that looks like your forms. Follows your design system, has your validation principles, looks like you built it.

Form generators vs. forms

Take a basic form for example. Name and email.

Handles 2 input fields, validates email as you type, verifies both fields are filled out before submitting.

58 lines of boring code and there’s a bunch of cases I didn’t cover.

Click through for source

Every engineer on your team goes through that every time they build a form. And they build a lot.

This is how they feel 👇

groan giphy

Wouldn’t you?

Here’s what you do after someone 😉 builds a form generator for your team:

Click through for source

😍

How you build a form generator

A form generator doesn’t have to be complicated my friend, it just needs to get the job done.

You’ll need 3 parts:

  1. A generic <Field> component
  2. A generic <Form> component
  3. Couple of loops

Here’s a basic example that supports text fields only.

You start with a <Field> component like this.

Click through for source

This component handles a complete field. Renders an input with a label, stores state, and performs validation as you type. Even renders errors.

You can use render props for more flexibility. But that’s often too flexible. Aim to build a generator that spits out perfect forms for your company. Every time.

The <Form /> component is a glorified loop.

Click through for source

Loop through fields, render each one. Add a submit button, <form> element, and a place to put errors.

onSubmit

The onSubmit method handles submissions. Validates all fields, runs any form-level validations, and does the thing.

Something like this:

Click through for source

This one iterates through the fields and grabs their values straight from the DOM. A little dirty but it works ✌️

You might want to find a way to communicate values from <Field /> components with callbacks or something. Maybe hoist state up a level or add a React context … depends what you need.

Invalid fields are thrown out and the rest collected into an array of { name, value } objects. If that array is same length as the number of fields, the form is valid.

My submit alerts. Yours should call the onSubmit prop function of some sort. Let the user of your form builder define what happens 🙂

🤯

An afternoon of brain-bending once, a lifetime of perfect forms.

What are you waiting for? Go save your team from this pain, be the hero they need.

hero giphy

Cheers,
~Swizec

Learned something new? Want to improve your skills?

Join over 10,000 engineers just like you already improving their skills!

Here's how it works 👇

Leave your email and I'll send you an Interactive Modern JavaScript Cheatsheet 📖right away. After that you'll get thoughtfully written emails every week about React, JavaScript, and your career. Lessons learned over my 20 years in the industry working with companies ranging from tiny startups to Fortune5 behemoths.

PS: You should also follow me on twitter 👉 here.
It's where I go to shoot the shit about programming.