vue表单vxe-form如何对一个规则同时多字段联动校验,对一个控件校验多个关联字段

vue表单vxe-form如何对一个规则同时多字段联动校验,对一个控件校验多个关联字段。正常的表单场景是一个控件一个字段,那么配置起来非常任意,一个字段对应一个校验规则。当时某些复杂场景就不一样了,比如用户控件,有id/code/role等。比如范围日期选择,一个控件是对应2个字段的,开始日期和结束日期。这个时候就可以使用 rule 规则中 field 属性来指定复杂的多字段校验。

https://vxetable.cn

表单-日期范围-多字段校验

举个例子,比如日期范围选择,有2个字段,先给控件绑定一个不存在的字段,然后在这个字段里面配置2条规则,分别校验多个字段;当某个字段为空时都能被直接校验并提示出来

html 复制代码
<template>
  <div>
    <vxe-form v-bind="formOptions" v-on="formEvents" ></vxe-form>
  </div>
</template>

<script setup>
import { reactive } from 'vue'
import { VxeUI } from 'vxe-pc-ui'

const formOptions = reactive({
  titleWidth: 120,
  data: {
    name: 'test1',
    startDate: '',
    endDate: ''
  },
  rules: {
    _startAndEnd: [
      { field: 'startDate', required: true, message: '请选择开始时间' },
      { field: 'endDate', required: true, message: '请选择结束时间' }
    ]
  },
  items: [
    { field: 'name', title: '名称', span: 24, itemRender: { name: 'VxeInput' } },
    { field: '_startAndEnd', title: '2个字段格式', span: 24, itemRender: { name: 'VxeDateRangePicker', startField: 'startDate', endField: 'endDate' } },
    {
      align: 'center',
      span: 24,
      itemRender: {
        name: 'VxeButtonGroup',
        options: [
          { type: 'submit', content: '提交', status: 'primary' },
          { type: 'reset', content: '重置' }
        ]
      }
    }
  ]
})

const formEvents = {
  submit () {
    VxeUI.modal.message({ content: '保存成功', status: 'success' })
  },
  reset () {
    VxeUI.modal.message({ content: '重置事件', status: 'info' })
  }
}
</script>

表格-日期范围-多字段校验

同样先给控件绑定一个不存在的字段,然后在这个字段里面配置2条规则,分别校验多个字段

html 复制代码
<template>
  <div>
    <vxe-button @click="fullValidEvent">校验全量数据</vxe-button>
 
    <vxe-grid ref="gridRef" v-bind="gridOptions"></vxe-grid>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { VxeUI } from 'vxe-table'

const gridRef = ref()

const gridOptions = reactive({
  border: true,
  showOverflow: true,
  keepSource: true,
  height: 300,
  editConfig: {
    trigger: 'click',
    mode: 'row',
    showStatus: true
  },
  editRules: {
    _startAndEnd: [
      { field: 'startDate', required: true, message: '请选择开始时间' },
      { field: 'endDate', required: true, message: '请选择结束时间' }
    ]
  },
  columns: [
    { type: 'checkbox', width: 60 },
    { type: 'seq', width: 70 },
    { field: 'name', title: 'Name', editRender: { name: 'VxeInput' } },
    { field: '_startAndEnd', title: '多字段校验', editRender: { name: 'VxeDateRangePicker', startField: 'startDate', endField: 'endDate' } },
    { field: 'sex', title: 'Sex', editRender: { name: 'VxeInput' } },
    { field: 'age', title: 'Age', editRender: { name: 'VxeInput' } },
    { field: 'date', title: 'Date', editRender: { name: 'VxeInput' } }
  ],
  data: [
    { id: 10001, name: 'Test1', startDate: '', endDate: '', sex: '0', age: 28, address: 'test abc' },
    { id: 10002, name: '', startDate: '2026-03-01', endDate: '2026-04-01', sex: '1', age: 22, address: 'Guangzhou' },
    { id: 10003, name: 'Test3', startDate: '', endDate: '', sex: '', age: 32, address: 'Shanghai' },
    { id: 10004, name: 'Test4', startDate: '2026-01-01', endDate: '2026-01-10', sex: '', age: 23, address: 'test abc' },
    { id: 10005, name: '', startDate: '2026-08-14', endDate: '2026-08-26', sex: '1', age: 30, address: 'Shanghai' },
    { id: 10006, name: 'Test6', startDate: '2026-10-10', endDate: '026-12-10', sex: '1', age: 21, address: 'test abc' }
  ]
})

const fullValidEvent = async () => {
  const $grid = gridRef.value
  if ($grid) {
    const errMap = await $grid.validate(true)
    if (errMap) {
      VxeUI.modal.message({ status: 'error', content: '校验不通过!' })
    } else {
      VxeUI.modal.message({ status: 'success', content: '校验成功!' })
    }
  }
}
</script>

https://gitee.com/x-extends/vxe-table

相关推荐
空&白3 小时前
vue暗黑模式
javascript·vue.js
css趣多多4 小时前
一个UI内置组件el-scrollbar
前端·javascript·vue.js
-凌凌漆-4 小时前
【vue】pinia中的值使用 v-model绑定出现[object Object]
javascript·vue.js·ecmascript
大橙子额6 小时前
【解决报错】Cannot assign to read only property ‘exports‘ of object ‘#<Object>‘
前端·javascript·vue.js
LYFlied7 小时前
从 Vue 到 React,再到 React Native:资深前端开发者的平滑过渡指南
vue.js·react native·react.js
B站_计算机毕业设计之家8 小时前
豆瓣电影数据采集分析推荐系统 | Python Vue Flask框架 LSTM Echarts多技术融合开发 毕业设计源码 计算机
vue.js·python·机器学习·flask·echarts·lstm·推荐算法
xjt_09019 小时前
基于 Vue 3 构建企业级 Web Components 组件库
前端·javascript·vue.js
我是伪码农9 小时前
Vue 2.3
前端·javascript·vue.js
跳动的梦想家h10 小时前
环境配置 + AI 提效双管齐下
java·vue.js·spring
Mr Xu_11 小时前
Vue 3 中 watch 的使用详解:监听响应式数据变化的利器
前端·javascript·vue.js