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 |
:
vee-validate
Comes out of the box with many validation rules, which are:
date_format
rule to be always present and must preceed them in the rules order. The date validators are not enabled by default, they require momentjs in order to work.
If your setup contains momentjs globally, it will be installed automatically for all validator instances. otherwise you may want to pass the moment reference to installDateTimeValidators(moment)
which is available both statically and on instances.
{}
like this: {param}
.
?
at the end: {optional?}
.
[]
. ex: [list]
.
args:
target:
The input name to be validated against. must have the same format as the date_format rule.args:
min:
The minimum value.max:
The maximum value.args:
target:
The name of the confirmation field.args:
min:
The minimum allowed value for date. must be in the same format as the date_format rule.max:
The maximum allowed value for date. must be in the same format as the date_format rule.args:
format:
The date format. See momentjs parsing.args:
decmials:
The maximum allowed number of decimal point numbers. Not passing the decmials will accept numeric data which may or may not contain decimal point numbers.args:
length:
The number of digits.args:
width:
The width of the image.height:
The height of the image.args:
Comma separated list of extensions. ex: ext:jpg,png,bmp,svg
.
args:
Comma separated list of values. ex in:1,2,3,4
args:
length:
A numeric value representing the maximum number of characters.args:
List of comma separated mime types. mimes:image/jpeg|image/png
mimes:image/*
will accept all image types.
args:
length:
A numeric value representing the minimum number of characters.args:
Comma separated list of invalid values. ex: not_in:1,2,3,4
args:
pattern:
A regular expressionflags:
list of regular expression flags (optional)args:
size:
The maximum file size in kilobytes.args:
domain:
Adds another check if the url belongs to a specific domain. tlds should improve the accuracy.
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.
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.
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.
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.
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.
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.
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.
dictionary.locale.message
.