常规情况下。rules和formItem是一对一的。例如下面的情况,判断表单内的测试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("必填项,请输入数据"));
} 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中就按照常规的写法如下会发现不管如何输入或删除都将触发valid=false校验。如果在validator_isEmpty中打印value值会发现,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("必填项,请输入数据"));
} 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需要关联到tableData.data,否则使用validate方法将无法触发
2、去掉外层el-form的rules属性
3、对el-table-column里的el-form-item添加rules属性,其中rules中的validator采用bind的方式传递行参数
4、对el-table-column里的el-form-item中prop属性需要保留
之后的操作就跟普通的form验证一样文章来源:https://www.toymoban.com/news/detail-759727.html
<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("必填项,请输入数据"));
} 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>
文章来源地址https://www.toymoban.com/news/detail-759727.html
到了这里,关于vue3在table里使用elementUI的form表单验证的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!