Written by: Geoffrey Callaghan
VeeValidate tutorial
Veevalidate Tutorial
VeeValidate is a popular form validation library for Vue.js, designed to make form validation easy and intuitive. This tutorial will guide you through the basics of using VeeValidate in a Vue.js application.
First, make sure you have a Vue.js application set up. You can create a new Vue project using Vue CLI:
npm install -g @vue/cli
vue create my-veevalidate-app
cd my-veevalidate-app
Install VeeValidate using npm or yarn:
npm install vee-validate
# or
yarn add vee-validate
To get started, let’s create a simple form to collect user information (name and email) and validate it using VeeValidate.
import { createApp } from 'vue';
import App from './App.vue';
import { Field, Form, ErrorMessage, defineRule, configure } from 'vee-validate';
import * as rules from '@vee-validate/rules';
import { localize, setLocale } from '@vee-validate/i18n';
import en from '@vee-validate/i18n/dist/locale/en.json';
// Define all rules
Object.keys(rules).forEach(rule => {
defineRule(rule, rules[rule]);
});
// Configure VeeValidate
configure({
generateMessage: localize({ en }), // Load the locale object
validateOnInput: true, // Validate on input
});
setLocale('en');
const app = createApp(App);
app.component('Field', Field);
app.component('Form', Form);
app.component('ErrorMessage', ErrorMessage);
app.mount('#app');
MyForm.vue
):<template>
<div>
<Form @submit="handleSubmit">
<div>
<label for="name">Name</label>
<Field name="name" rules="required|max:15" />
<ErrorMessage name="name" />
</div>
<div>
<label for="email">Email</label>
<Field name="email" rules="required|email" />
<ErrorMessage name="email" />
</div>
<button type="submit">Submit</button>
</Form>
</div>
</template>
<script>
export default {
methods: {
handleSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
You can define custom validation rules in VeeValidate.
import { defineRule } from 'vee-validate';
defineRule('noAdmin', value => {
if (value === 'admin') {
return 'Username cannot be "admin"';
}
return true;
});
<template>
<div>
<Form @submit="handleSubmit">
<div>
<label for="username">Username</label>
<Field name="username" rules="required|noAdmin" />
<ErrorMessage name="username" />
</div>
<div>
<label for="email">Email</label>
<Field name="email" rules="required|email" />
<ErrorMessage name="email" />
</div>
<button type="submit">Submit</button>
</Form>
</div>
</template>
<script>
export default {
methods: {
handleSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
},
};
</script>
VeeValidate supports multiple languages and localization.
npm install @vee-validate/i18n
# or
yarn add @vee-validate/i18n
import { configure } from 'vee-validate';
import { localize } 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('en'); // Set default locale
<template>
<div>
<select v-model="locale" @change="changeLocale">
<option value="en">English</option>
<option value="fr">French</option>
</select>
<Form @submit="handleSubmit">
<div>
<label for="name">Name</label>
<Field name="name" rules="required|max:15" />
<ErrorMessage name="name" />
</div>
<div>
<label for="email">Email</label>
<Field name="email" rules="required|email" />
<ErrorMessage name="email" />
</div>
<button type="submit">Submit</button>
</Form>
</div>
</template>
<script>
import { setLocale } from '@vee-validate/i18n';
export default {
data() {
return {
locale: 'en'
};
},
methods: {
handleSubmit(values) {
alert(JSON.stringify(values, null, 2));
},
changeLocale() {
setLocale(this.locale);
}
}
};
</script>
VeeValidate is a comprehensive form validation library for Vue.js, providing a range of features to handle form validation efficiently. This tutorial covers the basics, but VeeValidate offers a lot more functionality that you can explore in its official documentation.