vue表單vxe-form多字段聯(lián)動校驗的操作方法(對一個控件校驗多個關(guān)聯(lián)字段)
vue表單vxe-form如何多字段聯(lián)動校驗,對一個控件校驗多個關(guān)聯(lián)字段。正常的表單場景是一個控件一個字段,那么配置起來非常任意,一個字段對應(yīng)一個校驗規(guī)則。當時某些復雜場景就不一樣了,比如用戶控件,有id/code/role等。比如范圍日期選擇,一個控件是對應(yīng)2個字段的,開始日期和結(jié)束日期。這個時候就可以使用 rule 規(guī)則中 field 屬性來指定復雜的多字段校驗。
表單-日期范圍-多字段校驗
舉個例子,比如日期范圍選擇,有2個字段,先給控件綁定一個不存在的字段,然后在這個字段里面配置2條規(guī)則,分別校驗多個字段;當某個字段為空時都能被直接校驗并提示出來

<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: '請選擇結(jié)束時間' }
]
},
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條規(guī)則,分別校驗多個字段

<template>
<div>
<vxe-button @click="fullValidEvent">校驗全量數(shù)據(jù)</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: '請選擇結(jié)束時間' }
]
},
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
到此這篇關(guān)于vue表單vxe-form多字段聯(lián)動校驗的操作方法(對一個控件校驗多個關(guān)聯(lián)字段)的文章就介紹到這了,更多相關(guān)vue表單vxe-form多字段聯(lián)動校驗內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue項目Element表格對應(yīng)字段映射顯示方法:formatter格式化數(shù)據(jù)問題
- Vue如何實現(xiàn)u-form多個form表單同時校驗
- Vue3使用el-form嵌套el-table進行單條數(shù)據(jù)的表單校驗功能
- Vue3表單組件el-form校驗規(guī)則rules屬性示例詳解
- vue3+ant?design的form數(shù)組表單校驗方法
- vue實現(xiàn)多個el-form表單提交統(tǒng)一校驗的2個方法
- vue中的el-form表單rule校驗問題(特殊字符、中文、數(shù)字等)
- vue之el-form表單校驗以及常用正則詳解
- Vue 清除Form表單校驗信息的解決方法(清除表單驗證上次提示信息)
- Vue之ElementUI Form表單校驗
相關(guān)文章
基于vue 實現(xiàn)表單中password輸入的顯示與隱藏功能
這篇文章主要介紹了vue 實現(xiàn)表單中password輸入的顯示與隱藏功能 ,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2019-07-07
vue 實現(xiàn)websocket發(fā)送消息并實時接收消息
這篇文章主要介紹了vue 實現(xiàn)websocket發(fā)送消息并實時接收消息,項目結(jié)合vue腳手架和websocket來搭建,本文給大家分享實例代碼,需要的朋友可以參考下2019-12-12
element-resize-detector監(jiān)聽普通元素的實現(xiàn)示例
當涉及到網(wǎng)頁元素的實時尺寸變化監(jiān)測時,element-resize-detector?是一個值得推薦的開源庫,本文主要介紹了element-resize-detector監(jiān)聽普通元素的實現(xiàn)示例,感興趣的可以了解一下2024-07-07
vscode配置vue下的es6規(guī)范自動格式化詳解
這篇文章主要介紹了vscode配置vue下的es6規(guī)范自動格式化詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-03-03
減少 Vue 應(yīng)用白屏時間的經(jīng)驗總結(jié)
Vue 項目日益龐大,首屏渲染變慢、頁面打開時出現(xiàn)“白屏”的現(xiàn)象屢見不鮮,本文將系統(tǒng)講解白屏的成因,并提供多種實用優(yōu)化策略與示例代碼,幫助你在實際項目中顯著提升用戶體驗2025-08-08

