vue.js

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript类库 > vue.js > vue中elementUI表单循环验证

vue中elementUI表单循环验证方式

作者:EstherNi

这篇文章主要介绍了vue中elementUI表单循环验证方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue中elementUI表单循环验证

在这里插入图片描述

进行验证的步骤

1、表单el-form 添加 :model="form" ref="form" :rules="rules",注意是 :model="form"不是v-model,而后每个el-form-item绑定prop

2、不循环的示例在官网可看

3、循环表单的验证:

注意不循环的表单每个el-form-item 不需要单独加:rules="rules.orderNum"     

然后prop的名字要与data中的rules数组对象的名称相同即可

 `this.$refs.form.validateField('orderNum', (valid) => {
      //valid返回的是规则集中写的错误提示信息,如果满足条件,返回的就是空
      if (valid) {
         return this.$refs["form"].clearValidate('orderNum');
        }
   });`
<el-form class="demo-form" :model="form" ref="form" :rules="rules" label-width="100px">
      <el-col :span="12">
        <el-form-item label="名称:" prop="name">
          <el-input clearable v-model="form.name" placeholder="请输入考评名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="选择:" prop="indexId">
          <el-select clearable v-model="form.indexId" placeholder="请选择指标" style="width:100%;" @change="removeHintChange(form.indexId,'indexId')">
            <el-option v-for="item in targetOptions" :key="item.value" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col class="wrap">
        <div class="step" v-for="(item, index) in form.addList" :key="index">
          <div class="stepName">
            步骤{{index+1}}:
          </div>
          <el-form-item label="考试考试考试考试:" class="evaluationPerson" :prop="`addList[${index}][personNameId]`" :rules="rules.personNameId">
            <el-select clearable v-model="item.personNameId" placeholder="请选择人员" @change="removeHintChange(item.personNameId,`addList[${index}][personNameId]`)">
              <el-option v-for="item in personOptions" :key="item.value" :label="item.label" :value="{value:item.value,label:item.label}">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="顺序:" class="order" :prop="`addList[${index}][orderNum]`" :rules="rules.orderNum">
            <el-input clearable v-model="item.orderNum" @input="changeMemberId($event,index,0)"></el-input>
          </el-form-item>
          <el-form-item label="顺1:" class="weight" :prop="`addList[${index}][ratio]`" :rules="rules.ratio">
            <el-input clearable v-model="item.ratio" @input="changeMemberId($event,index,1)"></el-input>
            <div class="percent">%</div>
          </el-form-item>
          <el-form-item label="备注:" class="remarks" :prop="`addList[${index}][remark]`" :rules="rules.remark">
            <el-input clearable type="textarea" v-model="item.remark" placeholder="部门负责人"></el-input>
            <div class="iconPic" @click="deleteInformation(index)"><i class="el-icon-delete-solid"></i></div>
          </el-form-item>
        </div>
      </el-col>
      <el-col class="addInfor">
        <el-button type="primary" plain size="mini" @click="addInformation">
          <i class="el-icon-plus"></i>添加
        </el-button>
      </el-col>
    </el-form>
data() {
    return {
      dialogShow1: true,
      // 指标选择
      indexId: "",
      targetOptions: [{ label: "11", value: 1 }],
      // 考评人员
      personNameId: "",
      personOptions: [{ label: "22", value: 1 }],
      form: {
        indexId: "",
        // 表单字段列表
        addList: [{ personNameId: "", orderNum: "", ratio: "", remark: "" }],
      },
      // 校验规则
      rules: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
        indexId: [
          { required: true, message: "请选择", trigger: "blur" },
        ],
        personNameId: [
          { required: true, message: "请选择(人员)", trigger: "blur" },
        ],
        orderNum: [{ required: true, message: "请输入顺序", trigger: "blur" }],
        ratio: [{ required: true, message: "请输入权重", trigger: "blur" }],
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      },
    };
  },
// 限制有的输入框只能输入数字
    changeMemberId(val, index, num) {
      if (num === 0) {
        this.form.addList[index].orderNum = val.replace(/[^\d]/g, "");
      } else if (num === 1) {
        this.form.addList[index].ratio = val.replace(/[^\d]/g, "");
      }
    },
    // 强制去掉验证
    removeHintChange(value, name) {
      if (value != null || "" || []) {
        this.$refs.form.validateField(name, (valid) => {
          //valid返回的是规则集中写的错误提示信息,如果满足条件,返回的就是空
          if (valid) {
            return this.$refs["form"].clearValidate(name);
          }
        });
      }
    },
    // 添加步骤
    addInformation() {
      this.form.addList.push({
        personNameId: "",
        orderNum: "",
        ratio: "",
        remark: "",
      });
    },
    // 弹窗 - 删除信息
    deleteInformation(val) {
      if (this.form.addList.length > 1) {
        this.form.addList.splice(val, 1);
      } else {
        this.$message({
          message: "不可全部删除,最少一条新增数据信息",
          type: "warning",
        });
      }
    },

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文