Conquering Forms in Vue

Rate this content
Bookmark

Web forms are the connective tissue of the internet. Without them, people cannot request their health records, apply for university, order pad thai, or book a plane ticket. Yet too often, the tools we use to build those forms are a grab bag of libraries and DIY solutions that can result in a subpar user experience, poor accessibility, and low maintainability.

In this talk, we will introduce FormKit — a form-building framework — and explore how this tool can empower Vue developers to write consistent, accessible, even delightful forms without spending a lifetime building them.

Talk table of contents:

  • - The problem with forms, why they’re hard
  • - Introduction to FormKit
  • - Input library
  • - Validation
  • - Accessibility
  • - Form architecture
  • - Generating forms from JSON
  • - Next steps and closing statements

This talk has been presented at Vue.js London 2023, check out the latest edition of this JavaScript Conference.

FAQ

FormKit is a form building framework designed specifically for Vue, focusing on the architecture of your form rather than just being a component library. It automates the collection of input values, applies validation with a single property, and allows for easy form generation. This framework significantly reduces the complexity and boilerplate code associated with form handling in Vue.

FormKit automatically collects input values, eliminating the need for manual setup of v-model for each input. It also simplifies validation by allowing developers to apply rules directly through a single prop on the input. FormKit ships with numerous predefined validation rules that include internationalization support.

The single component approach in FormKit standardizes form inputs across applications, making learning and integration easier. Every native HTML input type is mirrored in FormKit with added functionalities, creating a consistent API and reducing inconsistencies found in standard HTML forms.

Yes, FormKit supports the structuring of nested and grouped data within forms. This feature allows developers to encapsulate specific parts of forms, such as addresses, into separate components or groups, maintaining a clean and organized data structure even in complex form setups.

FormKit Schema is a JSON serializable data format that allows for defining and generating forms and their structures. It supports rendering DOM elements and Vue components, and includes advanced features like conditional rendering and expressions. Schema can dynamically generate forms based on stored or transmitted data configurations, enhancing flexibility and reusability.

FormKit enhances form accessibility by automatically generating IDs and linking labels and descriptions correctly, adhering to accessibility standards. It also handles error messages effectively by placing them next to the relevant input fields, as recommended by usability studies, thus making forms more user-friendly.

Learning resources for FormKit can be found on the official website, FormKit.com, and through Vue School's series of courses available at viewschool.io. These resources provide detailed guidance on utilizing FormKit effectively in your Vue applications.

Justin Schroeder
Justin Schroeder
24 min
15 May, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

Today's Talk focused on building forms in Vue using FormKit. The speaker highlighted the simplicity of forms in Vue and the importance of adding buttons, labels, and help text for a better user experience. They also discussed handling form data and errors, refactoring form components, and implementing inline validation. The introduction to FormKit showcased its features such as a single component approach, automatic data collection, and simplified form building. The talk also covered applying validation and form generation using the FormKit schema, which allows for easy form representation and rendering.
Available in Español: Dominando los Formularios en Vue

1. Introduction to Conquering Forms in Vue

Short description:

Hi Vue.js Live. Today we'll learn about making forms in Vue with a focus on FormKit. I'm Justin Schrader, involved in projects like FormKit, Arrow.js, AutoAnimate, and Vue Formulate. Building complex forms can be tedious and hard to maintain. However, forms are easy. All you need is a form tag and a couple of input tags.

Hi Vue.js Live. I'm talking to you today about conquering forms in Vue. We're going to learn about how easy and wonderful it is to make forms in Vue with a particular bent on FormKit.

So I'm Justin Schrader and you might know me from the internet, you might not. Some of the open source projects that I'm involved in are FormKit, which is what we're talking about today. Arrow.js, which is a small two kilobyte lightweight alternative to things like React and Vue. Another one is AutoAnimate, which is really neat. Works great with Vue and React and really any other ones. And it lets you automatically animate DOM elements coming in and out of the DOM or moving around. And then Vue Formulate, which is the spiritual ancestor of FormKit. It was a Vue 2 library, all about building forms in Vue. And finally, I'm a partner in an agency called Braid here in Charlottesville, Virginia. So that's me.

This is also me in 2019 staring at a massive client project that we had built with hundreds, maybe even thousands of inputs and forms. It was incredibly tedious to build, very hard to maintain. And at the time we were thinking there's just gotta be a better way. So let's talk a little bit about that because the prevailing wisdom on the internet right now, especially in dev Twitter is forms are not hard. Forms are easy. In fact, Ryan Florence, one of the creators of Remix recently had this tweet and I think it applies, you know, he's talking about remix and react, but I think it applies to really any of the big libraries out there. Let me read this. I don't think there should even be form abstractions. Forms are fundamentally markup user interactions, event handlers, state management requests between views fell to react. What will the form library out here? Toss in some validation and be done with it. Interesting sentiment. And you know what? Fundamentally Ryan's right. Forms are easy. All you really need is a form tag and a couple of input tags and voila, you've got yourself a completely valid form. Um, here's one. It's a username and password. This could be a login or registration.

2. Adding Buttons, Labels, and Help Text

Short description:

It's incredibly simple. We should put a button for submitting the form. We need labels for accessibility. Adding help text makes the form look better. Now we're ready to accept and process the data.

And as you can see, it's incredibly simple. Now to be fair, we probably should put a button in there for submitting the form. Um, in this case we put a register button in there. So this must be a registration form.

And realistically for accessibility reasons, we're going to need some labels on those. Plus people won't even know what to fill out if we don't put them on there. So let's go ahead and do that. We'll just add an ID to our inputs because we need to semantically link them with the four attributes. So we'll just pop that on there. Great. Look at our form renders. People would know how to fill this out. We are good to go.

Although to be fair, it does look a little bit like a login form, so we should probably add some help text on those inputs. And to do that, all we got to do is add an ARIA described by on our actual input. And then wherever we put our help text, we can put an ID, uh, something like, you know, username help. And now we're good. Now we're off to the races. Here we go. Now our form is starting to look better. We've got help text, you know, could probably use some styles, but it's, but it's looking good. Forms are easy.

To be fair, this doesn't actually do anything yet. So let's go ahead and put a script set up in the head here, and we'll just put an at submit. This is view. Super easy. Submit handler. And now we're ready to actually accept this data and do something with it. And there's a couple ways. We can get the data right.