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
  ],
});

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

相关推荐
广州华水科技2 分钟前
2026年高口碑GNSS变形监测一体机推荐:提升水库安全解决方案
前端
xiaoxue..3 分钟前
讲讲 浏览器的缓存机制
前端·缓存·面试·浏览器
Data_Journal7 分钟前
Node.js网络爬取指南——简单易上手!
大数据·linux·服务器·前端·javascript
a11177617 分钟前
可视化角色权限配置页面(html 开源)
前端·开源·html
Lee川25 分钟前
个人中心与 AI 头像生成:从页面到 DALL-E 的完整实现
前端·架构
tedcloud1236 小时前
UI-TARS-desktop部署教程:构建AI桌面自动化系统
服务器·前端·人工智能·ui·自动化·github
UXbot9 小时前
AI原型设计工具如何支持团队协作与快速迭代
前端·交互·个人开发·ai编程·原型模式
ZC跨境爬虫10 小时前
跟着MDN学HTML_day_48:(Node接口)
前端·javascript·ui·html·音视频
PieroPc11 小时前
CAMWATCH — 局域网摄像头监控系统 Fastapi + html
前端·python·html·fastapi·监控
巴巴博一12 小时前
2026 最新:Trae / Cursor 一键接入 taste-skill 完整教程(让 AI 前端告别“AI 味”)
前端·ai·ai编程