【问题标题】:VeeValidate use custom rule globallyVeeValidate 全局使用自定义规则
【发布时间】:2020-04-11 11:44:29
【问题描述】:

我有一个自定义的 veevalidate 规则,用于查看输入的值是否已经在该组件的数组中。我想在具有不同数组的不同组件中使用此规则。有没有办法做到这一点?这是我目前在一个组件中的规则

const isUnique = (value) => {
      const reg = new RegExp(`^${value}$`, 'i');
      const inputValue = this.myArray.filter(str => reg.test(str));

      if (inputValue.length > 0) {
        return {
          valid: false,
          data: {
            message: `The ${inputValue}  already exists.`,
          },
        };
      }
      return { valid: true };
    };

    Validator.extend('unique', {
      validate: isUnique,
      getMessage: (field, params, data) => data.message,
    });

【问题讨论】:

    标签: vue.js vee-validate


    【解决方案1】:

    您当然可以 - 您可以使用名为 oneOf 的现有规则,该规则记录在 here 中,或者您可以更改您的规则以接受参数。看起来您的规则不区分大小写,因此您可能想坚持下去。您需要做的就是接受isUnique 函数的第二个参数,然后使用它代替this.myArray

    const isUnique = (value, values) => {
          const reg = new RegExp(`^${value}$`, 'i');
          const inputValue = values.filter(str => reg.test(str));
    

    然后在您的模板中,您可以这样称呼它:

    <ValidationProvider :rules="{isUnique:myArray}">
    

    【讨论】:

    猜你喜欢
    • 2020-07-23
    • 1970-01-01
    • 2019-08-04
    • 2018-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-21
    • 2018-07-23
    相关资源
    最近更新 更多