????????常規(guī)情況下。rules和formItem是一對(duì)一的。例如下面的情況,判斷表單內(nèi)的測(cè)試1和測(cè)試2是否為空。
<template>
<el-form
:model="formParams.form"
:rules="formParams.rules"
>
<el-form-item label="測(cè)試1" prop="input1">
<el-input v-model="formParams.form.input1"></el-input>
</el-form-item>
<el-form-item label="測(cè)試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("必填項(xiàng),請(qǐng)輸入數(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ī)的寫法如下會(huì)發(fā)現(xiàn)不管如何輸入或刪除都將觸發(fā)valid=false校驗(yàn)。如果在validator_isEmpty中打印value值會(huì)發(fā)現(xiàn),value一直未undefined。
<template>
<el-form
:model="tableData.data"
:rules="tableData.rules"
>
<el-table
:data="tableData.data">
<el-table-column label="測(cè)試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="測(cè)試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("必填項(xiàng),請(qǐng)輸入數(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>
? ? ? ? 所以使用下面的方式。但是有幾個(gè)重點(diǎn)。
1、外層的el-form的model需要關(guān)聯(lián)到tableData.data,否則使用validate方法將無法觸發(fā)
2、去掉外層el-form的rules屬性
3、對(duì)el-table-column里的el-form-item添加rules屬性,其中rules中的validator采用bind的方式傳遞行參數(shù)
4、對(duì)el-table-column里的el-form-item中prop屬性需要保留
之后的操作就跟普通的form驗(yàn)證一樣文章來源:http://www.zghlxwxcb.cn/news/detail-759727.html
<template>
<el-form
:model="tableData.data"
>
<el-table
:data="tableData.data">
<el-table-column label="測(cè)試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="測(cè)試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("必填項(xiàng),請(qǐng)輸入數(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>
?文章來源地址http://www.zghlxwxcb.cn/news/detail-759727.html
到了這里,關(guān)于vue3在table里使用elementUI的form表單驗(yàn)證的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!