elementUI表达自定义校验,校验在v-for中

注意:本帖为公开技术贴,不得用做任何商业用途

html 复制代码
<el-form :inline="true" :rules="rules" :model="formData" ref="formRef" class="mt-[20px]">
        <el-form-item label="选择区域" prop="area">
          <el-select v-model="formData.area" placeholder="请选择" @change="areaChange">
            <el-option v-for="item in areaList" :key="item.value" :label="item.name" :value="item.value"></el-option>
          </el-select>
        </el-form-item>
        <el-row v-for="(item, index) in formData.content" :key="index" class="my-[20px]">
          <div>
            <div class="mb-[10px]">{{ item.name }}:</div>
            <div
              class="border w-[400px] p-[10px] min-h-[45px]"
              style="border-radius: 5px; white-space: normal; word-break: break-all; border-color: #47536b"
              text="[14px]"
              @click="choiceSensor(item.name, item.ids)"
            >
              {{ item.ids ? item.ids : "点击选择传感器" }}
            </div>
          </div>
          <div class="ml-[10px] flex items-center">
            <el-form-item
              label="取值方式"
              :prop="`content[${index}].valueWay`"
              style="margin-bottom: 0; margin-top: 20px"
              :rules="[{ required: true, message: '请选择', trigger: 'blur' }]"
            >
              <el-select v-model="item.valueWay" placeholder="请选择">
                <el-option v-for="item in valueWayList" :key="item.value" :label="item.name" :value="item.value"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item
              label="环境阈值"
              :prop="`content[${index}].threshold`"
              style="margin-bottom: 0; margin-top: 20px"
              :rules="[
                { required: true, message: '请输入环境阈值', trigger: 'blur' },
                { pattern: rule0_1, message: '请输入大于等于1的1位小数', trigger: 'blur' },
              ]"
            >
              <el-input v-model="item.threshold" placeholder="环境阈值" />
            </el-form-item>
            <el-form-item
              label="浮动值"
              :prop="`content[${index}].floating`"
              style="margin-bottom: 0; margin-top: 20px"
              :rules="[
                { required: true, message: '请输入环境阈值', trigger: 'blur' },
                { pattern: rule0_1, message: '请输入大于等于1的1位小数', trigger: 'blur' },
              ]"
            >
              <el-input v-model="item.floating" placeholder="浮动值" />
            </el-form-item>
          </div>
        </el-row>
      </el-form>

这里ts/js部分

javascript 复制代码
const formRef = ref();
// 保留大于等于0的1位小数
export const rule0_1 = /^(0|[1-9]\d*)(\.\d)?$/
const formData = ref<any>({
  area: "",
  content: [
    { name: "温度", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // 温度
    { name: "湿度", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // 湿度
    { name: "co", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // co
    { name: "co2", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // co2
    { name: "照度", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // 照度
    { name: "PM2.5", ids: "", valueWay: "", threshold: "", floating: "", rule: rule0_1 }, // PM2.5
  ],
});

其中取值方式,环境阈值,浮动值,均在循环中完成,自定义的的表单校验,同类型同一种校验

相关推荐
geovindu2 小时前
css: Timeline scribble
前端·css·iphone
liangshiwei8 小时前
ABP x Vue:介绍 ABP Vue UI
vue·abp
沙漠之主10 小时前
C++编程教学设计资料:从入门到实战的完整课程方案
java·前端·c++
hasty10 小时前
不上传新包,也能改变用户拿到的版本:npm dist-tag 的 OIDC 权限治理
前端·npm·node.js
Csvn11 小时前
diff 算法(虚拟 DOM Reconciliation)
前端
前端snow12 小时前
ai agent --- 文件存储
前端
Frag0ut12 小时前
Chrome四大版本获取及共存指南:Stable/Beta/Dev/Canary
前端·chrome·浏览器·dev·beta·canary·共存版
IT_陈寒14 小时前
SpringBoot自动配置坑了我一把,原来是这样绕过去的
前端·人工智能·后端
广州华水科技14 小时前
单北斗GNSS变形监测在大坝安全监测中的应用与优势
前端
zeng不错14 小时前
B站 173 个投稿活动看到眼瞎?我写了个扩展,主打一个精准打击
前端