el-form表单自定义规则导致-validate不能执行

el-form表单自定义规则导致-validate不能执行

  • demo
    • js逻辑

一定要在自定义规则之后加上else{callback()}

demo

js逻辑

{内容,8}和{内容}来区分
也可以用正则匹配
{内容,8}{([\u4E00-\u9FA5]|[1-9]| |[a-z]){1,4}(,|,){1}\d*}
{内容} {([\u4E00-\u9FA5]|[1-9]|[a-z]|\d| ){1,3}

rules = {content: [{ validator: this.validatePass, trigger: ['blur'] }],};

validatePass(rule: any, value: any, callback: any) {
    this.commaIndex = [];
    if (value.length == 0) {
      callback(new Error('请重新输入模板内容'));
    }
    this.leftIndex = value.indexOf('{');
    this.rightIndex = value.indexOf('}');
    value.split('').forEach((item: string, index: number) => {
      if (item == ',') {
        this.commaIndex.push({ index: index, name: item });
      }
      if (item == ',') {
        this.commaIndex.push({ index: index, name: item });
      }
    });

    if (this.form.varType == 1) {
      //具名{内容,8}
      if (this.leftIndex != -1) {
        if (this.commaIndex.length == 0) {
          callback(new Error('请输入符合模板规则的内容'));
        } else {
          let temp = 0;
          this.commaIndex.forEach((item, index) => {
            if (this.leftIndex < item.index && this.rightIndex > item.index) {
              temp++;
            }
          });
          if (temp > 0) {
            callback();
          } else {
            callback(new Error('请输入符合模板规则的内容'));
          }
          console.log(temp, 'temp');
        }
      } else {
        callback(new Error('请输入符合模板规则的内容'));
      }
    } else if (this.form.varType == 0) {
    //正常模式{内容,8}
      if (this.leftIndex != -1) {
        let temp = 0;
        this.commaIndex.forEach((item, index) => {
          if (this.leftIndex < item.index && this.rightIndex > item.index) {
            temp++;
          }
        });
        if (temp) {
          callback(new Error('请输入符合模板规则的内容'));
        } else {
          callback();
        }
      }
    }
  }

链接: https://www.cnblogs.com/hjdjs/p/element.html.

你可能感兴趣的:(element-ui,ui,javascript,前端)