vuelidate 所需的功能 - Vye.js

vuelidate required function - Vye.js

我使用 vuelidate 插件:

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

我有方法

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

而且我有 vuelidate 插件。我想发送 required 我的函数,但出现错误。

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

如何发送所需的功能?

您可以像这样创建自定义验证器:

  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
           }  
    }

根据 docs 提供的示例,尝试像这样重写您的代码

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

或者使用纯函数

validations: {
    finishedAtYear: {
        isFinishedFill: (value) => { /* do some checks here */ },
    },
}