Skip to content
Swizec Teller - a geek with a hatswizec.com

Why you should build a form generator

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
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
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
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
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
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
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

Did you enjoy this article?

Published on April 27th, 2020 in Front End, Technical

Learned something new?
Want to become a high value JavaScript expert?

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.

Start with an interactive cheatsheet ๐Ÿ“–

Then get thoughtful letters ๐Ÿ’Œ on mindsets, tactics, and technical skills for your career.

"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. ๐Ÿ‘Œ"

~ Ashish Kumar

Join over 10,000 engineers just like you already improving their JS careers with my letters, workshops, courses, and talks. โœŒ๏ธ

Have a burning question that you think I can answer?ย I don't have all of the answers, but I have some! Hit me up on twitter or book a 30min ama for in-depth help.

Ready to Stop copy pasting D3 examples and create data visualizations of your own? ย Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization

Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.

Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first ๐Ÿ’ฐ on the side with ServerlessReact.Dev

Want to brush up on your modern JavaScript syntax?ย Check out my interactive cheatsheet: es6cheatsheet.com

By the way, just in case no one has told you it yet today: I love and appreciate you for who you areย โค๏ธ

Created bySwizecwith โค๏ธswizec.com