【vue element-ui】表单连锁验证,el-form validate函数失效问题

实现效果:连锁表单验证,在LED版本号选择为升级版LED时候,标题名称不超过8

实现代码:

    <el-form
      :model="configuration"
      ref="form"
      :rules="rule"
      size="small"
      label-width="130px"
      v-if="isture == 1"
    >
      <el-form-item label="LED版本号" prop="version">
        <el-select
          style="width: 100%"
          v-model="configuration.version"
          @change="changeBB"
        >
          <el-option
            v-for="item in versionOption"
            :key="item.value"
            :label="item.label"
            :value="item.value"
            >{{ item.label }}</el-option
          >
        </el-select>
      </el-form-item>
      
     
      <el-form-item label="标题名称" prop="titile">
        <el-input
          placeholder="请输入内容"
          v-model="configuration.titile"
        ></el-input>
      </el-form-item>
    </el-form>
  data() {
    // 表单校验规则
    var validatePass = (rule, value, callback) => {
      //console.log("当前表单数据", this.configuration.version, value.length);
      if (this.configuration.version == 2) {
        if (value.length > 10) {
          callback(new Error("标题字数过长"));
        } else {
          callback();
        }
      }
      if (value === "") {
        callback(new Error("标题不可为空"));
      } else {
        callback();
      }
    };
    return {
     
      //表单
      configuration: {
        version: "",
        titile:"",
      },
      versionOption: [
        {
          value: 1,
          label: "烽云智能LED"
        },
        {
          value: 2,
          label: "烽云升级版LED"
        }
      ],
      rule: {
        titile: [
          {
            //  required: true,
            validator: validatePass,
            // message: "标题有误",
            trigger: "blur"
          }
        ],
        
      }
    };
  },

表单提交代码:

    sumbit() {
      
      this.isture == 0;
      this.$refs["form"].validate(r => {
       
        if (r) {
         
          this.configuration.contentOne = this.configuration.contentOne.join(
            ","
          );
          this.configuration.index1 = this.configuration.index1.join(",");
          this.configuration.index2 = this.configuration.index2.join(",");
          this.configuration.index3 = this.configuration.index3.join(",");
          this.configuration.index4 = this.configuration.index4.join(",");
          this.configuration.index5 = this.configuration.index5.join(",");
         

          if (this.isadd == true) {
            this.doadd();
            
          } else {
            this.edit();
            
          }
        }
      });
    },

注意:如果点击表单的确认按钮,没有任何效果,validate函数不运行就需要确定验证是否出问题

每一个 if 都要对应了一个 else 并且每个条件都要 callback 确保在不同情况下都会执行callback

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值