Top Related Projects
Vue Forms ⚡️ Supercharged
Build forms in React, without the tears 😭
📋 React Hooks for form state management and validation (Web + React Native)
Performance-focused API for React forms 🚀
🏁 Framework agnostic, high performance, subscription-based form state management
Simple, lightweight model-based validation for Vue.js
Quick Overview
Vee-Validate is a template-based validation framework for Vue.js applications. It provides a simple and extensible way to validate forms and inputs, with support for custom rules, localization, and integration with popular UI libraries.
Pros
- Easy to integrate with Vue.js projects
- Extensive set of built-in validation rules
- Supports custom validation rules and error messages
- Offers localization support for multiple languages
Cons
- Learning curve for complex validation scenarios
- Can increase bundle size in smaller projects
- Some users report performance issues with large forms
- Documentation can be overwhelming for beginners
Code Examples
- Basic form validation:
<template>
<Form @submit="onSubmit">
<Field name="email" as="input" :rules="emailRules" />
<ErrorMessage name="email" />
<button type="submit">Submit</button>
</Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate';
export default {
components: { Form, Field, ErrorMessage },
setup() {
const emailRules = 'required|email';
const onSubmit = values => {
console.log(values);
};
return { emailRules, onSubmit };
}
};
</script>
- Custom validation rule:
import { defineRule } from 'vee-validate';
defineRule('strong_password', value => {
if (!/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/.test(value)) {
return 'Password must contain at least 8 characters, including uppercase, lowercase, and numbers';
}
return true;
});
- Localization example:
import { configure } from 'vee-validate';
import { localize, setLocale } from '@vee-validate/i18n';
import en from '@vee-validate/i18n/dist/locale/en.json';
import fr from '@vee-validate/i18n/dist/locale/fr.json';
configure({
generateMessage: localize({ en, fr }),
});
setLocale('fr'); // Set French as the active locale
Getting Started
- Install Vee-Validate:
npm install vee-validate @vee-validate/rules
- Import and use in your Vue component:
<template>
<Form @submit="onSubmit">
<Field name="username" :rules="required" />
<ErrorMessage name="username" />
<button type="submit">Submit</button>
</Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate';
import { required } from '@vee-validate/rules';
export default {
components: { Form, Field, ErrorMessage },
setup() {
const onSubmit = values => {
alert(JSON.stringify(values, null, 2));
};
return { required, onSubmit };
}
};
</script>
This setup creates a simple form with a required username field and displays validation errors.
Competitor Comparisons
Vue Forms ⚡️ Supercharged
Pros of FormKit
- More comprehensive form-building solution with pre-built inputs and layouts
- Offers a plugin system for easy extensibility
- Provides a schema-based approach for defining forms, which can be useful for complex scenarios
Cons of FormKit
- Steeper learning curve due to its more opinionated structure
- Larger bundle size compared to Vee-Validate
- Less flexibility in terms of integration with existing form structures
Code Comparison
FormKit:
<FormKit
type="form"
:actions="false"
@submit="submitHandler"
>
<FormKit
type="text"
name="username"
label="Username"
validation="required|length:5"
/>
</FormKit>
Vee-Validate:
<Form @submit="submitHandler">
<Field
name="username"
rules="required|min:5"
v-slot="{ field, errors }"
>
<input v-bind="field" />
<span>{{ errors[0] }}</span>
</Field>
</Form>
Both libraries offer robust form validation capabilities, but FormKit provides a more all-in-one solution with pre-built components, while Vee-Validate focuses primarily on validation and offers more flexibility in terms of integration with existing form structures.
Build forms in React, without the tears 😭
Pros of Formik
- More popular and widely adopted in the React ecosystem
- Extensive documentation and community support
- Seamless integration with React components and hooks
Cons of Formik
- Limited built-in validation rules, often requiring additional libraries
- Steeper learning curve for complex form scenarios
- React-specific, not suitable for other frameworks
Code Comparison
Formik:
import { Formik, Form, Field } from 'formik';
<Formik initialValues={{ email: '' }} onSubmit={handleSubmit}>
<Form>
<Field name="email" type="email" />
<button type="submit">Submit</button>
</Form>
</Formik>
Vee-Validate:
<template>
<Form @submit="onSubmit">
<Field name="email" type="email" :rules="emailRules" />
<button>Submit</button>
</Form>
</template>
<script>
import { Form, Field } from 'vee-validate';
export default {
components: { Form, Field },
setup() {
const emailRules = 'required|email';
const onSubmit = values => {
// Handle form submission
};
return { emailRules, onSubmit };
}
};
</script>
Both libraries offer similar functionality for form handling and validation, but Formik is more React-centric, while Vee-Validate is designed to work with Vue.js. Formik provides a more React-idiomatic approach, while Vee-Validate offers built-in validation rules and a slightly more concise syntax for Vue applications.
📋 React Hooks for form state management and validation (Web + React Native)
Pros of react-hook-form
- Lightweight and performant, with minimal re-renders
- Easy integration with React's hooks system
- Flexible validation options, including Yup and Joi
Cons of react-hook-form
- Limited built-in UI components
- Steeper learning curve for complex form scenarios
- Primarily focused on React, less versatile across frameworks
Code Comparison
react-hook-form:
import { useForm } from "react-hook-form";
const { register, handleSubmit } = useForm();
const onSubmit = data => console.log(data);
<form onSubmit={handleSubmit(onSubmit)}>
<input {...register("firstName")} />
<input type="submit" />
</form>
vee-validate:
<template>
<Form @submit="onSubmit">
<Field name="firstName" />
<button type="submit">Submit</button>
</Form>
</template>
<script>
import { Form, Field } from 'vee-validate';
export default {
components: { Form, Field },
methods: {
onSubmit(values) {
console.log(values);
}
}
};
</script>
Summary
react-hook-form excels in performance and React integration, while vee-validate offers a more comprehensive solution with built-in UI components and cross-framework compatibility. react-hook-form may be preferred for React-specific projects prioritizing performance, whereas vee-validate might be chosen for its ease of use and versatility across different frameworks.
Performance-focused API for React forms 🚀
Pros of Unform
- Designed specifically for React, offering seamless integration with React components
- Supports complex form structures and nested fields out of the box
- Provides a more flexible and customizable API for form handling
Cons of Unform
- Less extensive documentation compared to Vee-Validate
- Smaller community and ecosystem, potentially leading to fewer third-party resources
- May have a steeper learning curve for developers new to React-specific form libraries
Code Comparison
Unform:
import { Form } from '@unform/web';
import Input from './components/Input';
function MyForm() {
function handleSubmit(data) {
console.log(data);
}
return (
<Form onSubmit={handleSubmit}>
<Input name="email" />
<button type="submit">Submit</button>
</Form>
);
}
Vee-Validate:
<template>
<Form @submit="onSubmit">
<Field name="email" as="input" />
<ErrorMessage name="email" />
<button>Submit</button>
</Form>
</template>
<script>
import { Form, Field, ErrorMessage } from 'vee-validate';
export default {
components: { Form, Field, ErrorMessage },
methods: {
onSubmit(values) {
console.log(values);
}
}
};
</script>
🏁 Framework agnostic, high performance, subscription-based form state management
Pros of Final Form
- Framework-agnostic, can be used with any UI library or framework
- Smaller bundle size, lightweight core with optional add-ons
- Powerful subscription model for efficient re-renders
Cons of Final Form
- Steeper learning curve due to its flexible and low-level nature
- Requires more setup and configuration compared to Vee-Validate
- Less out-of-the-box features, may need additional plugins
Code Comparison
Vee-Validate:
import { defineRule, Form, Field } from 'vee-validate';
defineRule('required', value => {
if (!value || !value.length) {
return 'This field is required';
}
return true;
});
Final Form:
import { Form, Field } from 'react-final-form';
const validate = values => {
const errors = {};
if (!values.username) {
errors.username = 'Required';
}
return errors;
};
Both libraries offer form validation, but Vee-Validate provides a more declarative approach with built-in rules, while Final Form offers a more programmatic and flexible validation system. Vee-Validate is Vue-specific, whereas Final Form can be used with any framework, including React as shown in the example.
Simple, lightweight model-based validation for Vue.js
Pros of Vuelidate
- Lightweight and minimalistic approach
- More flexible validation rules and custom validators
- Better integration with Vue's reactivity system
Cons of Vuelidate
- Steeper learning curve for complex validations
- Less out-of-the-box features compared to Vee-Validate
- Manual error handling and display
Code Comparison
Vuelidate:
import { required, minLength } from '@vuelidate/validators'
const rules = {
name: { required, minLength: minLength(4) }
}
Vee-Validate:
import { extend, ValidationProvider } from 'vee-validate'
import { required, min } from 'vee-validate/dist/rules'
extend('required', required)
extend('min', min)
Vuelidate takes a more programmatic approach, allowing for easier customization of validation rules. Vee-Validate, on the other hand, provides a more declarative syntax with built-in components like ValidationProvider, which can simplify form handling in templates.
Both libraries offer robust validation solutions for Vue applications, but they differ in their implementation and philosophy. Vuelidate focuses on flexibility and integration with Vue's reactivity, while Vee-Validate provides a more opinionated structure with additional features out of the box.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Painless Vue forms
Features
- ð Easy: Declarative validation that is familiar and easy to setup
- ð§ââï¸ Flexible: Synchronous, Asynchronous, field-level or form-level validation
- â¡ï¸ Fast: Build faster forms faster with intuitive API and small footprint
- ð Minimal: Only handles the complicated form concerns, gives you full control over everything else
- ð UI Agnostic: Works with native HTML elements or your favorite UI library components
- 𦾠Progressive: Works whether you use Vue.js as a progressive enhancement or in a complex setup
- â Built-in Rules: Companion lib with 25+ Rules that covers most needs in most web applications
- ð i18n: 45+ locales for built-in rules contributed by developers from all over the world
Getting Started
Installation
# Install with yarn
yarn add vee-validate
# Install with npm
npm install vee-validate --save
Vue version support
The main v4 version supports Vue 3.x only, for previous versions of Vue, check the following the table
vue Version | vee-validate version | Documentation Link |
---|---|---|
2.x | 2.x or 3.x | v2 or v3 |
3.x | 4.x | v4 |
Usage
vee-validate offers two styles to integrate form validation into your Vue.js apps.
Composition API
The fastest way to create a form and manage its validation, behavior, and values is with the composition API.
Create your form with useForm
and then use defineField
to create your field model and props/attributes and handleSubmit
to use the values and send them to an API.
<script setup>
import { useForm } from 'vee-validate';
// Validation, or use `yup` or `zod`
function required(value) {
return value ? true : 'This field is required';
}
// Create the form
const { defineField, handleSubmit, errors } = useForm({
validationSchema: {
field: required,
},
});
// Define fields
const [field, fieldProps] = defineField('field');
// Submit handler
const onSubmit = handleSubmit(values => {
// Submit to API
console.log(values);
});
</script>
<template>
<form @submit="onSubmit">
<input v-model="field" v-bind="fieldProps" />
<span>{{ errors.field }}</span>
<button>Submit</button>
</form>
</template>
You can do so much more than this, for more info check the composition API documentation.
Declarative Components
Higher-order components can also be used to build forms. Register the Field
and Form
components and create a simple required
validator:
<script setup>
import { Field, Form } from 'vee-validate';
// Validation, or use `yup` or `zod`
function required(value) {
return value ? true : 'This field is required';
}
// Submit handler
function onSubmit(values) {
// Submit to API
console.log(values);
}
</script>
<template>
<Form v-slot="{ errors }" @submit="onSubmit">
<Field name="field" :rules="required" />
<span>{{ errors.field }}</span>
<button>Submit</button>
</Form>
</template>
The Field
component renders an input
of type text
by default but you can control that
ð Documentation
Read the documentation and demos.
Contributing
You are welcome to contribute to this project, but before you do, please make sure you read the contribution guide.
Credits
- Inspired by Laravel's validation syntax
- v4 API Inspired by Formik's
- Nested path types by react-hook-form
- Logo by Baianat
Emeriti
Here we honor past contributors and sponsors who have been a major part on this project.
âï¸ License
Top Related Projects
Vue Forms ⚡️ Supercharged
Build forms in React, without the tears 😭
📋 React Hooks for form state management and validation (Web + React Native)
Performance-focused API for React forms 🚀
🏁 Framework agnostic, high performance, subscription-based form state management
Simple, lightweight model-based validation for Vue.js
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot