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.
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.
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.
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:
first('field')
Fetches the first error message associated with that field.collect('field')
Fetches all error messages associated with that field.has('field')
Checks if there are any errors associated with that field.all()
Gets all error messages.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.
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:
errorBagName:
The name of the ErrorBag object that will be injected in each of Vue's instances' data.delay:
The default debounce time for all inputs (only affects validations).locale:
The default language for the validation messages.messages:
The messages to be generated for the validation errors, check custom messages section.