Syntax

The validation rules have a very simple syntax similar to Laravel validation syntax. A validation expression is a string of a series of validators separated by a pipe |: var single = 'required'; // single rule. var multiple = 'required|numeric' // multiple rules. Some rules can have parameters, which are passed in a comma separated list without spaces. var someRule = 'in:1,2,3,4';

Note: The validators (rules) and their parameters are statically saved, in order to update some validation rule, you have to attach it again with the new parameters and it will be overwritten.

Available Rules

vee-validate Comes out of the box with 21 validation rules, which are:

Note: In the rule signature required parameters are enclosed within {} like this: {param}.
Optional parameters have a ? at the end: {optional?}.
Lists are enclosed withn brackets []. ex: [list].
alpha
The field under validation may only contain alphabetic characters.
{{ errors.first('alpha_field') }}
alpha_dash
The field under validation may contain alphabetic characters, numbers, dashes or underscores.
{{ errors.first('alpha_dash_field') }}
alpha_num
The field under validation may contain alphabetic characters or numbers.
{{ errors.first('alpha_num_field') }}
between:{min},{max}
The field under validation must have a numeric value bounded by a minimum value and a maximum value.
args:
  • min: The minimum value.
  • max: The maximum value.
{{ errors.first('between_field') }}
confirmed:{target}
The field under validation must have the exact same value as the confirmation field.
args:
  • target: The name of the confirmation field.
{{ errors.first('confirm_field') }}
digits:{length}
The field under validation must be numeric and have the specified number of digits.
args:
  • length: The number of digits.
{{ errors.first('digits_field') }}
dimensions:{width},{height}
The file added to the field under validation must be an image (jpg,svg,jpeg,png,bmp,gif) have the exact specified dimensions.
args:
  • width: The width of the image.
  • height: The height of the image.
{{ errors.first('dimensions_field') }}
email
The field under validation must be a valid email.
{{ errors.first('email_field') }}
ext:[extensions]
The file added the field under validation must have one of the extensions specified.
args: Comma separated list of extensions. ex: ext:jpg,png,bmp,svg.
{{ errors.first('ext_field') }}
image
The file added the field under validation must have an image mime type (image/*).
{{ errors.first('image_field') }}
in:[list]
The field under validation must have a value that is in the specified list.
args: Comma separated list of values. ex in:1,2,3,4
{{ errors.first('in_field') }}
ip
The field under validation must have a string that is a valid ipv4 value.
{{ errors.first('ip_field') }}
max:{length}
The field under validation length may not exceed the specified length.
args:
  • length: A numeric value representing the maximum number of characters.
{{ errors.first('max_field') }}
mimes:[list]
The file type added to the field under validation should have one of the specified mime types.
args: List of comma separated mime types. mimes:image/jpeg|image/png
{{ errors.first('mimes_field') }}
Note: You can use '*' to specify a wild card, something like mimes:image/* will accept all image types.
min:{length}
The field under validation length should not be less than the specified length.
args:
  • length: A numeric value representing the minimum number of characters.
{{ errors.first('min_field') }}
not_in:[list]
The field under validation length should not have any value within the specified values.
args: Comma separated list of invalid values. ex: not_in:1,2,3,4
{{ errors.first('not_in_field') }}
numeric
The field under validation must only consist of numbers.
{{ errors.first('numeric_field') }}
regex:{pattern}
The field under validation must match the specified regular expression.
args:
  • pattern: A regular expression
  • flags: list of regular expression flags (optional)
{{ errors.first('regex_field') }}
Note: Currently you should not use the pipe | within your regular expression as it will cause a conflict with how parsing validators work. A workaround is in progress.
required
The field under validation must have more than 0 characters.
{{ errors.first('required_field') }}
size:{kb}
The file size added to the field under validation must not exceed the specified size in kilobytes.
args:
  • size: The maximum file size in kilobytes.
{{ errors.first('size_field') }}
url:{domain?}
The field under validation must be a valid url. A domain may be optionally passed. supports subdomains.
args:
  • domain: Adds another check if the url belongs to a specific domain. tlds should improve the accuracy.
{{ errors.first('url_field') }}

Custom Rules

You can easily add custom rules to the validators, but your custom validation rules must adhere to a contract, or certain structure:

Function Form: This is the most basic custom validator form, it consists of only a function that returns either a Boolean or a promise. However it will have a default error message. const validator = (value, args) => { // Return a Boolean or a Promise. } Object Form: const validator = { getMessage(field, args) { // will be added to default English messages. // Returns a message. }, validate(value, args) { // Returns a Boolean or a Promise. } }; Localized Object Form: const validator = { messages: { en: (field, args) => { // Returns a message. }, cn: (field, args) => { // Returns a Chinese message. } }, validate(value, args) { // Returns a Boolean or a Promise. } }; This validator form must have a validate method, and either a getMessage method, or a messages object. The only difference that the latter will allow you to add localized messages, the former only adds it to the English dictionary.

Note: Notice how the messages methods gets passed the field which is the name of the field under validation as a first parameter. And how the validate method gets passed the value as a first parameter. And both receive the args which are the parameters (arguments) that were configured with the validation rule. for example look at the actual implementation of the min rule.

Note: As you can see a validation rule must implement one of the three forms discussed above. Not doing so will throw a ValidatorException with a suitable error message detailing what were you missing.

After creating your validator, You can add it to the list of rules using extend(name, validator) method in the validator instance. import { Validator } from 'vee-validate'; // These are 'ES6' arrow functions. Validator.extend('truthy', { getMessage: field => 'The ' + field + ' value is not truthy.', validate: value => !! value }); let instance = new Validator({ trueField: 'truthy' }); // Also there is an instance 'extend' method for convience. instance.extend('falsy', (value) => ! value); instance.attach('falseField', 'falsy');

Note: Using any of the extend either statically or on an instance will extend all validators with the new validation rule. extending a new rule that have the same name as an existing rule will throw a ValidatorException with an error message.

Custom Messages and Localization

Of course you might need to overwrite the error messages, or add new ones. The Validator class and its instances provide an updateDictionary method. which will merge the messages with the internal dictionary, overwriting any duplicates.

Note: Any merges will have an effect on all validator instances as the messages dictionary is shared.
import { Validator } from 'vee-validate'; const dictionary = { en: { alpha: () => 'Some English Message' }, ar: { alpha: () => 'Some Arabic Message' } }; Validator.updateDictionary(dictionary); const validator = new Validator({ first_name: 'alpha' }); validator.setLocale('ar'); // now this validator will generate messages in arabic.

Usually you would stucture your language files for your app rather than adding hardcoded strings like the example above, check the localization guide for a better approach.

Note: You must provide the messages in an object path like: dictionary.locale.message.