Search code examples
vuejs2custom-validators

vee-validate Custom validation rules not working


Versions:

  • VueJs: 2.2.6
  • Vee-Validate: ^2.0.0-beta.25

Description:

I am working on a project, where I use laravel-vue-starter as a base template.

I wants to use a custom validation for password. So I created a resources\assets\js\validators\passwordValidators.js file with code:

import { Validator } from 'vee-validate';

Validator.extend('password', {
    getMessage: field => 'Insert a strong password, it should contain Uppercase letter, lowercase letter, number and special character',
    validate: value => /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.[\W]).{8,}$/.test(value)
});

But when I am addingv-validate="'password'" It produce an error[vee-validate] No such validator 'password' exists

Any help will be appreciated.


Solution

  • I think the answer is simple, you are now using a rule name 'password' , but your rule is password.

    So try this in your markup (string notation), remove the single quotes.

    v-validate = "password"
    

    Or you can also use the object notation when you have more then 1 and complex rules.

    v-validate = "{password: true}"
    

    Regards Ben