页面传多个参数,写成 函数()=>{ } 形式:
{ validator: (rule: any, value: any, callback: any) => {
idNumVerification(rule, value, form.idType, callback) }, trigger: 'blur' }
完整代码如下:
<script lang="ts" setup>
import { reactive, toRefs } from 'vue';
let state = reactive({
form: {
idNum: '',
idType: ''
}
})
let { form } = toRefs(state)
const idNumVerification = (rule: any, value: any, type: any, callback: any) => {
if (type == 1) {
if (!/^([a-zA-z]|[0-9]){5,17}$/.test(value)) {
return callback(new Error('证件号码不合规'))
} else {
callback()
}
}
// else if (这里写type等于其它值时){
// 这里是type等于其它值时的校验规则
// }
else {
callback()
}
}
</script>
<template>
<el-card>
<h2 style="margin:20px;">表单自定义校验传参</h2>
<el-form :inline="true" :model="form" ref="formRef">
<el-form-item prop="idType" label="证件类型">
<el-input v-model="form.idType" placeholder="请输入证件类型" clearable />
</el-form-item>
<el-form-item prop="idNum" label="证件号码"
:rules="[{ required: true, message: '请填写证件号码', trigger: 'blur' },
{ validator: (rule: any, value: any, callback: any) => { idNumVerification(rule, value, form.idType, callback) }, trigger: 'blur' }]">
<el-input v-model="form.idNum" placeholder="请输入证件号码" clearable />
</el-form-item>
</el-form>
</el-card>
</template>
主要是传参这块的格式要写对了 ,自定义方法中取到type, 做不同的正则判断