@formkit/vue と vuelidate は、どちらも Vue.js アプリケーションでフォームの入力検証を実装するためのライブラリですが、設計思想や機能範囲に大きな違いがあります。@formkit/vue は FormKit フレームワークの一部であり、フォーム全体の構築からバリデーション、UI コンポーネント、エラーハンドリングまでを包括的に提供します。一方、vuelidate は軽量で柔軟なバリデーション専用ライブラリで、既存のフォーム構造に最小限の侵入で検証ロジックを追加できるよう設計されています。
Vue.js でフォームを扱う際、入力値の検証は避けて通れない課題です。@formkit/vue と vuelidate は、どちらもこの問題を解決するためのライブラリですが、アプローチが根本的に異なります。一方は「フォーム全体のプラットフォーム」、もう一方は「バリデーション専用のツール」です。現場でどちらを選ぶべきか、実装レベルで比較してみましょう。
@formkit/vue は、FormKit というフルスタックなフォームフレームワークの Vue 向けバインディングです。フォームの構造定義、UI コンポーネント、バリデーション、エラー表示、状態管理をすべて一元的に扱います。
<!-- @formkit/vue: フォーム全体を <FormKit> で囲む -->
<template>
<FormKit type="form" @submit="handleSubmit">
<FormKit
type="text"
name="email"
label="メールアドレス"
validation="required|email"
/>
<FormKit
type="password"
name="password"
label="パスワード"
validation="required|length:8,100"
/>
</FormKit>
</template>
vuelidate は、バリデーションロジックのみを提供し、UI は完全に開発者の責任です。Vue のリアクティブシステムと連携して、検証結果を $v オブジェクトとして公開します。
<!-- vuelidate: 既存の input 要素に直接適用 -->
<template>
<form @submit="handleSubmit">
<input
v-model="form.email"
:class="{ error: $v.form.email.$error }"
type="email"
placeholder="メールアドレス"
/>
<div v-if="$v.form.email.$error">有効なメールアドレスを入力してください</div>
<input
v-model="form.password"
:class="{ error: $v.form.password.$error }"
type="password"
placeholder="パスワード"
/>
<div v-if="$v.form.password.$error">8文字以上で入力してください</div>
</form>
</template>
<script setup>
import { reactive } from 'vue'
import useVuelidate from '@vuelidate/core'
import { required, email, minLength } from '@vuelidate/validators'
const form = reactive({ email: '', password: '' })
const rules = {
email: { required, email },
password: { required, minLength: minLength(8) }
}
const $v = useVuelidate(rules, form)
const handleSubmit = () => {
$v.value.$validate()
if (!$v.value.$error) {
// 送信処理
}
}
</script>
@formkit/vue では、HTML 属性として文字列でルールを指定します。組み込みルール(required, email, matches など)に加え、カスタムルールも登録可能です。
<FormKit
type="text"
name="username"
validation="required|matches:/^[a-z0-9_]+$/"
:validation-messages="{
matches: '英数字とアンダースコアのみ使用できます'
}"
/>
vuelidate では、JavaScript オブジェクトでルールを宣言します。関数や正規表現、非同期検証も自然に記述できます。
import { helpers } from '@vuelidate/ validators'
const usernameRegex = /^[a-z0-9_]+$/
const rules = {
username: {
required,
format: helpers.regex('username', usernameRegex)
}
}
@formkit/vue は、各フィールドの状態(valid, errors, touched など)を内部で管理し、自動的にエラーメッセージを表示します。テーマやプラグインで外観をカスタマイズ可能ですが、DOM 構造は FormKit が制御します。
<!-- エラーメッセージは自動で表示される -->
<FormKit type="text" name="name" validation="required" />
<!-- 入力が空の場合、自動で「This field is required.」が表示 -->
vuelidate は、$v.fieldName.$error や $v.fieldName.$invalid といったフラグを提供するだけです。エラーメッセージの表示位置、スタイル、タイミングはすべて開発者が制御します。
<div v-if="$v.form.name.$error && !$v.form.name.required">
名前は必須です
</div>
@formkit/vue は、type="group" や type="list" といった特殊なタイプで、ネストや配列フィールドを簡単に扱えます。
<FormKit type="group" name="address">
<FormKit type="text" name="street" />
<FormKit type="text" name="city" />
</FormKit>
<FormKit type="list" name="hobbies">
<FormKit type="text" name="item" />
</FormKit>
vuelidate でも動的フォームは可能ですが、ルール定義とデータ構造を手動で同期させる必要があります。
// 配列のバリデーション
const rules = {
hobbies: {
$each: {
name: { required }
}
}
}
@formkit/vue では、独自の入力コンポーネントを FormKit の仕組みに登録する必要があります。createInput ユーティリティを使い、FormKit の状態システムと連携させます。
import { createInput } from '@formkit/vue'
const CustomDatePicker = createInput((props) => {
return h(DatePickerComponent, {
modelValue: props.context.value,
'onUpdate:modelValue': props.context.node.input
})
})
vuelidate は、どんな入力要素とも自由に組み合わせられます。v-model でバインドされた値に対して、別途ルールを定義するだけです。
<CustomDatePicker v-model="form.date" />
<!-- バリデーションルールは通常通り定義 -->
@formkit/vue では、async ルールを登録することで非同期検証を実現します。
FormKit.createConfig({
rules: {
uniqueEmail: async (value) => {
const response = await fetch(`/api/check-email?email=${value}`)
return response.ok
}
}
})
vuelidate では、非同期ルールを直接オブジェクトに記述できます。
const rules = {
email: {
required,
asyncUnique: async (value) => {
const res = await fetch(`/api/check-email?email=${value}`)
return res.ok
}
}
}
| 観点 | @formkit/vue | vuelidate |
|---|---|---|
| 目的 | フォーム全体の高速開発 | バリデーションロジックの追加 |
| UI制御 | FormKit が管理 | 開発者が完全制御 |
| 学習コスト | FormKit 固有の概念が必要 | Vue の知識があれば即導入 |
| 柔軟性 | フレームワーク内での柔軟性 | 任意のコンポーネントと統合可能 |
| 保守性 | 一貫性が高いがロックインリスク | ロジックとUIが分離されやすい |
@formkit/vue で開発速度を最大化。vuelidate で最小限の侵入で実装。@formkit/vue の恩恵を最大限享受。vuelidate の純粋な関数ベースのルールがテストしやすい。どちらも成熟したライブラリであり、メンテナンスも継続されています。重要なのは、プロジェクトの要件とチームの技術戦略に合致する方を選ぶことです。
@formkit/vue を選ぶべきなのは、フォームの UI コンポーネント、バリデーション、状態管理、エラーメッセージ表示などを一貫した方法で統合したい場合です。特に、複雑なフォーム(例:動的フィールド、ネストされたグループ、カスタム入力)を素早く構築し、一貫性のある UX を提供したいプロジェクトに向いています。ただし、FormKit の独自のコンポーネントモデルや設定方式に従う必要があるため、既存のフォーム構造に大きく依存している場合は移行コストが発生します。
vuelidate を選ぶべきなのは、既存の Vue コンポーネントやカスタム入力要素に、最小限の変更で柔軟なバリデーションロジックを追加したい場合です。シンプルなオブジェクトベースのルール定義と、Composition API との親和性が高く、細かい制御が必要なケースや、UI とバリデーションロジックを完全に分離したいアーキテクチャに適しています。ただし、UI やエラーメッセージの表示はすべて自分で実装する必要があります。
FormKit is a form-authoring framework for Vue developers that makes building high quality production-ready forms 10x faster. It is easy-to-learn and ships with production-ready scaffolding like inputs, forms, submission and error handling, and validation rules. To learn more check out the documentation website at: formkit.com.
FormKit — which supports its whole feature set for native HTML inputs (like select, checkbox, and textarea) — is and will always be an MIT-licensed
open source project. Please consider sponsoring FormKit so we can sustainably
and continually improve it! There are a variety of sponsor tiers and benefits for each sponsor.
Key features | |
|---|---|
☝️ Comprehensive packageFormKit is an all-in-one form-authoring framework with input scaffolding (labels, help text, etc.), validation, form UI & styling, error handling, generation, a11y, i18n, and more! |
😎 Developer happinessForms are everywhere, yet surprisingly tedious to author — well, not anymore. FormKit provides a powerful and flexible API to developers that makes complex form creation a breeze. |
🎯 Validation built inRidiculously easy validation out-of-the-box to handle 95% of use-cases. Help text, validation rules, and validation messages are simple props. Need more? You can add custom validation rules too. |
⚡️ Blazing-fast PerformanceFormKit can handle the most demanding forms — wizards, multi-step, deeply nested repeating groups, and more. |
🔌 Plugin systemExtend FormKit's functionality or reuse custom inputs, validation rules and messages across projects by tapping into the plugin system. Make your plugin open source to share with others! |
✨ Generate formsGenerate an entire form from a JSON-compatible schema. Schema allows you to render complex forms from JSON with conditional rendering, logic, dynamic data, groups, wrappers, HTML elements, and custom components. |
🎨 Robust ThemingFormKit works easily alongside your favorite UI frameworks like Bootstrap and utility-class tools like Tailwind. With numerous ways to modify classes and DOM structure, integrating FormKit plays nicely with any frontend. |
🌐 InternationalizationFormKit is made for all! Thanks to the FormKit community, FormKit ships with support for many languages. Don't see your language? Contribute one with our locale builder. |
Thank you for your willingness to contribute to this free and open source project! When contributing, consider first discussing your desired change with the core team via GitHub issues, Discord, or other method.
This project exists thanks to all the people who volunteer their time to contribute!
Copyright (c) 2021-present, FormKit, Inc.