vue中elementUI表单循环验证

vue中elementUI表单循环验证_第1张图片

进行验证的步骤

1、表单el-form 添加 :model="form" ref="form" :rules="rules",注意是 :model="form"不是v-model,而后每个el-form-item绑定prop
2、不循环的示例在官网可看
3、循环表单的验证:
 3-1、el-form-item 绑定 :prop="`addList[${index}][orderNum]`" :rules="rules.orderNum"
 	注意不循环的表单每个el-form-item 不需要单独加:rules="rules.orderNum"
 	然后prop的名字要与data中的rules数组对象的名称相同即可
 3-2、js判断时,此处的orderNum也就是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",
        });
      }
    },

<style scoped lang="scss">
// 弹窗中的内容
.addInfor {
  margin-bottom: 16px;
  display: flex;
  justify-content: end;
}
.wrap {
  max-height: 40vh;
  overflow-y: auto;
}
.step {
  display: flex;
  .stepName {
    white-space: nowrap;
    line-height: 36px;
    color: #606266;
    font-size: 14px;
    font-weight: 700;
  }
}
::v-deep .step .el-form-item__content {
  display: flex;
}
::v-deep .step .evaluationPerson .el-form-item__label {
  width: 150px !important;
}
::v-deep .step .order .el-form-item__label,
::v-deep .step .weight .el-form-item__label,
::v-deep .step .remarks .el-form-item__label {
  width: 70px !important;
}
::v-deep .step .order .el-form-item__content,
::v-deep .step .weight .el-form-item__content,
::v-deep .step .remarks .el-form-item__content {
  margin-left: 70px !important;
}
::v-deep .el-dialog__header {
  font-weight: 700;
}
::v-deep .el-textarea textarea {
  height: 36px;
}
.percent {
  margin-left: 10px;
}
.iconPic {
  margin-left: 10px;
  font-size: 16px;
  color: red;
}
</style>

你可能感兴趣的:(vue.js,elementui,前端)