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

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

相关推荐
哆啦A梦15889 小时前
商城后台管理系统 03 登录布局
javascript·vue.js·elementui
曼巴UE59 小时前
UE FString, FName ,FText 三者转换,再次学习,官方文档理解
服务器·前端·javascript
行走的陀螺仪10 小时前
高级前端 Input 公共组件设计方案(Vue3 + TypeScript)
前端·javascript·typescript·vue·组件设计方案
一颗不甘坠落的流星10 小时前
【Antd】基于 Upload 组件,导入Json文件并转换为Json数据
前端·javascript·json
LYFlied10 小时前
Vue2 与 Vue3 虚拟DOM更新原理深度解析
前端·javascript·vue.js·虚拟dom
Lucky_Turtle10 小时前
【Node】npm install报错npm error Cannot read properties of null (reading ‘matches‘)
前端·npm·node.js
小飞侠在吗11 小时前
vue shallowRef 与 shallowReacitive
前端·javascript·vue.js
惜分飞11 小时前
sql server 事务日志备份异常恢复案例---惜分飞
前端·数据库·php
GISer_Jing11 小时前
WebGL实例化渲染:性能提升策略
前端·javascript·webgl