Building accessible forms in Vue with Formkit ⚡️
Tags: vue, javascript, programming, webdev
Published: 2023-09-04 08:30:48

When I first learned about Formkit, I wasn't sure if I understand the its use case. Now after building few applications with it, I can truly say that it is an amazing piece of software!

The general idea is to have a form building framework where all the form logic is delivered by the Formkit so that we can focus on styling and adjusting (instead of building the whole form process from scratch).

In my opinion, these kind of tools are really great because they deliver certain web technology and process directly to your door, while you only have to focus on styling it the way you want. In the world of generic UI librararies where almost all of them do essentially the same thing, library such as Formkit really shines! ⚡️

In this article, I will share with you the general idea so that you can try it for yourself in your projects 😉

## What is Formkit?

FormKit is far more than a UI library. It is a comprehensive form building framework for Vue developers that makes authoring high quality production-ready forms faster, more accessible, with better DX and UX, and less code. It is easy to learn, and ships with production-ready features like inputs, validation rules, and submission/error handling.

Below, you can see a short video intro to Formkit and what problems does it solve:

{% embed https://youtu.be/89dijjTlveI %}

Forms seem simple. After all, a <form> and an <input> tag are all that's required. Yet, experienced engineers know that implementing production-ready forms is tedious and complicated. FormKit's key features were designed to solve these problems, while making form authoring efficient and enjoyable for developers:

* **Single component**: FormKit was designed with a single component for all inputs.
* **Accessibility**: We output an opinionated accessible markup by default.
* **Validation**: Numerous built-in validation rules with instant feedback.
* **Schema**: Powerful schema format to store and generate forms.
* **Styles**: Beautifully crafted theme.
* **Extensibility**: Easy to extend any feature.
* **Community**: Friendly community that will help you solve your questions.

Let's take a look at the next section, to see how we can use it in Vue!

## Building a project with Formkit

When I first looked at the Formkit documentation, I was really amazed by its quality. Everything was clean and up to the point. Not to mention the great wizard for building projects in Vue, Nuxt, or Astro that you can check out below:

![Formkit wizard](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l3zpn89msbs1n7elkzsh.png)

This will guide you through the process of installation and setup so that you can start building your forms easily.

There is a great page by Formkit that shows you how to build your first form that I will link [here](https://formkit.com/getting-started/your-first-form#our-first-input)

I won't be copying all the examples (they are explained brilliantly in the official docs) but I just want to show you one thing. 

With Formkit, you can build any type of form. Take a look at the most basic example:

```html
<FormKit type="text" />
```

And now, take a look at this more advanced case:

```html
<FormKit
  type="range"
  name="strength"
  id="strength"
  label="Strength"
  value="5"
  validation="min:2|max:9"
  validation-visibility="live"
  min="1"
  max="10"
  step="1"
  help="How many strength points should this character have?"
/>
```

What is great about it is that you are using one component and you can customize it the way you want. And you have documentation for any type or use case!

If you would like to try Formkit for yourself without adding it to your project yet, Formkit has a great playground where you can test it, build forms, and see how they behave:


![Formkit playground](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ia69e0pq4j405qyhit5k.png)



You can check it out [here](https://formkit.com/playground?css-framework=genesis&fkv=latest&fileTab=example.vue&imports=jc%28%27name%21%27ImportMap%27%7Elang%21%27json%27%7Eeditor%21%27%28*+1vue%5C%211https%3A%2F%2Fcdn.jsdelivr.net%2Fnpm%2Fvue%403%2Fdist%2Fvue.esm-browser.min.js0*%29*%27%29*%5Cn0%5C%271+0%0110*_&files=jc%5B%28%27name%21%27example.vue%27%7Etemplate%21%27%25target%25B%3Cstylejpre%5Bwrap%5D+%283margin-bottomV20px%26B%29BB.form-label+%283font-sizeV14px%3B3font-weightV500%3B3marginV0+0+-.5rem%3B3paddingV5px%3B3border-radiusV5px%3B3displayVinline-block%263background-colorV%23444%3B3colorVwhite%3BB%29BLstyle%3E%27%7Eeditor%21%27%3CscripYsetupjconsYcastRangeToNumber+%3D+%7BnodeU%283%2F%2F+WZadd+a+check+to+add+thZcasYonly+to+rangZinputs3if+%7Bnode.props.typZ%27%3A%3D%3D+%22range%22%7D+returnB3node.hook.input%7B%7Bvalue%2C+nextUnext%7BNumber%7Bvalue%7D%7D%7DB%29BBconsYcreateCX+%3D+async+%7BfieldsU%283awaiYnew+Promise%7B%7BrUsetTimeout%7Br%2C+1000%7D%7D3alert%7BJSON.stringify%7Bfields%7D%7DB%29BLscriptjB%3Ctemplate%3E3%3Cdiv%3E%3Ch4+class6form-label_%3EAdding+thZcreateCX+submiYhandlerLh4%3ELdiv%3E3%3Ch1%3ENew+CXLh1j3%3C%27%3A--+form+is+also+an+input%2C+so+iYalso+accepts+plugins+--%3E3%3CFormKiYtype6form_+%40submit6createCX_+%3Aplugins6%5BcastRangeToNumber%5D_+%23default6%28+valuZ%29_%3E%24text*name6name*id6nameQnot%3AAdmin%2BName*help6Enter+your+cX%22s+full+name*placeholder6%E2%80%9CScarleYSword%E2%80%9D7select%2BClass*name6class*id6class*placeholder6SelecYa+class*%3Aoptions6%5B%22Warrior%22%2C+%22Mage%22%2C+%22Assassin%22%5D7%60strength*id6strength%2BStrength*value65%5E6min%3A2%7Cmax%3A9%5E-visibility6live*min61*maxGstrength87%60skill*id6skillQmax%3A10%2BSkill*value65*min61*maxGskill87%60d%7F*id6d%7FQmax%3A10%2BD%7F*value65*min61*maxGd%7F8_3I%2Fj3I%3CprZwrap%3E%28%28+valuZ%29%29Lpre%3E3LFormKitj3%3Cp%3E%3Cem%3E%3Csmall%3EPress+submiYto+seZthZcollected+form+data.Lsmall%3ELem%3ELpjLtemplatej%27%7Esyntax%21%27html%27%7Eadded%21true%29%5D*_3II3BI6%3D_7_3I%2Fj%248+points+should+this+cX+have%3FB%5CnG610*step61*help6How+many+I++L%3C%2FQ%5E6required%7CU%7D+%3D%3E+V%3A+XharacterYt+Ze+_%5C%27j%3EB%243I%3CFormKit3IItype6%26+%27%3Aimportant%3B%2B*label6%5E*validation%60range*name6%7Fexterity%01%7F%60%5E%2B%26%24j_ZYXVUQLIGB8763*_)

If you are looking for more materials about Formkit, check out the following course by VueSchool [here](https://vueschool.io/courses/robust-vue-js-forms-with-formkit)

## Summary

Formkit offers a big number of features for free to use by anyone. If you are looking for more advanced and enterprise use cases, they also have a **Pro** tier where you get access to the these advanced components. You can check more details [here](https://formkit.com/pro)

I really recommend you to try it out as it handles all the things that you would need to create by yourself such as logic, accessibility, validation and more!