What is vee-validate?

This is a lightweight plugin for VueJS that allows you to validate input fields, and display errors.

You don't have to do anything fancy in your app, most of the work goes into the html, You only need to specify for each input what kind of validators should be used when the value changes. You will then get informed of the errors for each field.

Although most of the validations occur automatically, you can use the validator however you see fit. The validator object has no dependencies and is a standalone object.

Currently there are over 20 validation rules available in the plugin. This plugin is inspired by PHP Framework Laravel's validation syntax.

It also supports Vue 2.0, All examples here are built using Vue 2.0 with the plugin.

Installation

Vue 1.x npm install vee-validate --save Vue 2.x npm install vee-validate@next --save

Note: The latest version of this plugin will keep pointing to the Vue 1.x version, until Vue 2.0 is released which is soon, then the latest will point to Vue 2.0 while another tag will be used for the Vue 1.x versions.

Then you may use it like this: <script src="path/to/vue.js"></script> <script src="path/to/vee-validate.js"></script> <script> Vue.use(VeeValidate); // good to go. </script> or you may import it using ES6: import Vue from 'vue'; import VeeValidate from 'vee-validate'; Vue.use(VeeValidate);

Basic Example

All you need is to add the v-validate directive to the input you wish to validate.

Then Add a data-rules attribute which contains a list of validation rules separated by a pipe '|'. For the following example the validation rules are straight forward, use required to indicate that the field is required. And email to indicate that the field must be an email. To combine both rules we assign the value required|email to the data-rules data-set attribute.


Note: The field name that appears in the error messages is usually the input's name attribute, unless you passed a value from the vue instance $data object, The name will be then the expression name, check the data validation example.

Rendering Errors

Naturally, you would want to display the errors to your users, The plugin augments your Vue instance with a private validator object and a public errors data object. You are responsible for how the errors should be rendered.

The errors object exposes a simple methods to help you render errors:

There are a few more methods that you can use to manipulate the errors object.

Available Rules

There are more than 20 rules available to validate your inputs:

Visit the rules documentation to learn more about how to use each rule, and how to create your own.

Configuration

You may need to configure some options to tweak some of the plugin internals, this is not required, but could cause conflicts. For example: if you are using a property called errors on your vue instance this may cause conflicts. here is how you would set up the options along with the default values:

import Vue from 'vue'; import VeeValidate from 'vee-validate'; const config = { errorBagName: 'errors', // change if property conflicts. delay: 0, locale: 'en', messages: null, strict: true }; Vue.use(VeeValidate, config);