Search code examples
javascriptvue.jsvuejs2vue-componentvuelidate

vuelidate required function - Vye.js


I use vuelidate plugin:

import { required, maxLength } from 'vuelidate/lib/validators';

I have method

methods: {
  isFinishedFill() {
    return !!this.disabledFinishedAt || !!this.finishedAtYear;
  }
}

And I have vuelidate plugin. I want send required my function, but I get error.

validations: {
  finishedAtYear: {
    required: this.isFinishedFill,
  },
}

How I can send required function?


Solution

  • You could create a custom validator like so :

      import { required, maxLength } from 'vuelidate/lib/validators';
      //custom validator
      const isFinishedFill =(value, vm) =>  !!vm.disabledFinishedAt || !!vm.finishedAtYear;
       //vm represents the Vue instance 
      export default{
          ...
         validations:{
                finishedAtYear: {
                   required,
                   isFinishedFill  //<---- use  your custom validator
               }  
        }