vue3在table里使用elementUI的form表單驗證的示例代碼
常規(guī)情況下。rules和formItem是一對一的。例如下面的情況,判斷表單內(nèi)的測試1和測試2是否為空。
<template>
<el-form
:model="formParams.form"
:rules="formParams.rules"
>
<el-form-item label="測試1" prop="input1">
<el-input v-model="formParams.form.input1"></el-input>
</el-form-item>
<el-form-item label="測試2" prop="input2">
<el-input v-model="formParams.form.input2"></el-input>
</el-form-item>
</el-form>
</template>
<script lang="ts" setup>
import {reactive} from "vue";
const formParams = reactive({
form:{
input1: "",
input2: ""
},
rules: {
input1: {
validator: validator_isEmpty,
trigger: 'change'
},
input2: {
validator: validator_isEmpty,
trigger: 'change'
}
}
})
function validator_isEmpty (rule: any, value: string, callback: any) {
if (isEmpty(value)) {
callback(new Error("必填項,請輸入數(shù)據(jù)"));
} else {
callback();
}
}
const isEmpty = function (value: any) {
if (
value === null ||
value === undefined ||
value === "" ||
value.toString().trim() === "" ||
value.length === 0 ||
Object.keys(value.toString()).length === 0 ||
JSON.stringify(value) === "[{}]"
) {
return true;
} else {
return false;
}
}
</script>
<style scoped lang="scss">
</style>但是,如果在table中就按照常規(guī)的寫法如下會發(fā)現(xiàn)不管如何輸入或刪除都將觸發(fā)valid=false校驗。如果在validator_isEmpty中打印value值會發(fā)現(xiàn),value一直未undefined。
<template>
<el-form
:model="tableData.data"
:rules="tableData.rules"
>
<el-table
:data="tableData.data">
<el-table-column label="測試1">
<template #default="scope">
<el-form-item prop="input1">
<el-input v-model="scope.row.input1"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="測試2">
<template #default="scope">
<el-form-item prop="input2">
<el-input v-model="scope.row.input2"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
</template>
<script lang="ts" setup>
import {reactive} from "vue";
let tableData = reactive({
data:[
{
input1: "",
input2: ""
}
],
rules: {
input1: {
validator: validator_isEmpty,
trigger: 'change'
},
input2: {
validator: validator_isEmpty,
trigger: 'change'
}
}
})
const formParams = reactive({
form: {
input1: "",
input2: ""
},
rules: {
input1: {
validator: validator_isEmpty,
trigger: 'change'
},
input2: {
validator: validator_isEmpty,
trigger: 'change'
}
}
})
function validator_isEmpty(rule: any, value: string, callback: any) {
if (isEmpty(value)) {
callback(new Error("必填項,請輸入數(shù)據(jù)"));
} else {
callback();
}
}
const isEmpty = function (value: any) {
if (
value === null ||
value === undefined ||
value === "" ||
value.toString().trim() === "" ||
value.length === 0 ||
Object.keys(value.toString()).length === 0 ||
JSON.stringify(value) === "[{}]"
) {
return true;
} else {
return false;
}
}
</script>
<style scoped lang="scss">
</style>所以使用下面的方式。但是有幾個重點。
1、外層的el-form的model需要關(guān)聯(lián)到tableData.data,否則使用validate方法將無法觸發(fā)
2、去掉外層el-form的rules屬性
3、對el-table-column里的el-form-item添加rules屬性,其中rules中的validator采用bind的方式傳遞行參數(shù)
4、對el-table-column里的el-form-item中prop屬性需要保留
之后的操作就跟普通的form驗證一樣
<template>
<el-form
:model="tableData.data"
>
<el-table
:data="tableData.data">
<el-table-column label="測試1">
<template #default="scope">
<el-form-item prop="input1" :rules="{
validator: validator_isEmpty_arg.bind(this, scope.row.input1),
trigger: 'change'
}">
<el-input v-model="scope.row.input1"></el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="測試2">
<template #default="scope">
<el-form-item prop="input2" :rules="{
validator: validator_isEmpty_arg.bind(this, scope.row.input2),
trigger: 'change'
}">
<el-input v-model="scope.row.input2"></el-input>
</el-form-item>
</template>
</el-table-column>
</el-table>
</el-form>
</template>
<script lang="ts" setup>
import {reactive} from "vue";
let tableData = reactive({
data: [
{
input1: "",
input2: ""
}
]
})
function validator_isEmpty_arg(rowValue: any, rule: any,value: string, callback: any){
if (isEmpty(rowValue)) {
callback(new Error("必填項,請輸入數(shù)據(jù)"));
} else {
callback();
}
}
const isEmpty = function (value: any) {
if (
value === null ||
value === undefined ||
value === "" ||
value.toString().trim() === "" ||
value.length === 0 ||
Object.keys(value.toString()).length === 0 ||
JSON.stringify(value) === "[{}]"
) {
return true;
} else {
return false;
}
}
</script>
<style scoped lang="scss">
</style>到此這篇關(guān)于vue3在table里使用elementUI的form表單驗證的文章就介紹到這了,更多相關(guān)vue3 elementUI的form表單驗證內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue加載讀取本地txt/json等文件的實現(xiàn)方式
這篇文章主要介紹了Vue加載讀取本地txt/json等文件的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-10-10
超詳細(xì)動手搭建一個VuePress 站點及開啟PWA與自動部署的方法
這篇文章主要介紹了超詳細(xì)動手搭建一個VuePress 站點及開啟PWA與自動部署的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-01-01
基于Vue+ELement搭建動態(tài)樹與數(shù)據(jù)表格實現(xiàn)分頁模糊查詢實戰(zhàn)全過程
這篇文章主要給大家介紹了關(guān)于如何基于Vue+ELement搭建動態(tài)樹與數(shù)據(jù)表格實現(xiàn)分頁模糊查詢的相關(guān)資料,Vue Element UI提供了el-pagination組件來實現(xiàn)表格數(shù)據(jù)的分頁功能,需要的朋友可以參考下2023-10-10
解決vue-cli項目開發(fā)運行時內(nèi)存暴漲卡死電腦問題
最近開發(fā)一個vue項目時遇到電腦卡死問題,突然間系統(tǒng)就非???,然后卡著卡著就死機了,鼠標(biāo)也動不了了,只能冷啟動。這篇文章主要介紹了vue-cli項目開發(fā)運行時內(nèi)存暴漲卡死電腦問題,需要的朋友可以參考下2019-10-10
vue中的Object.freeze()?優(yōu)化數(shù)據(jù)方式
這篇文章主要介紹了vue中的Object.freeze()優(yōu)化數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-04-04

