ElementUI⾃定义Validator
我们可以在formitem上指定验证规则,有时候我们需要校验⽤户数据的值是否有效,这时候就需要⽤到⾃定义的validator <el-form ref="form" :model="form" :rules="rules" label-width="260px">
rule的写法如下,这⾥分别加了必填验证,数字格式验证(使⽤的正则表达式),⾃定义校验:
rules: {
ClientNumber: [
{
required: true,
message: 'The client number cannot be empty',
trigger: 'blur',
},
{
type: 'string',
pattern: /^[A-Za-z0-9]{1,25}$/,
message:
'Client number should be a number or letter with maximum length of 25.',
trigger: 'blur',
},
{
validator: ClientNumberValidator,
trigger: 'blur',
},
],
},
⾃定义⽅法的写法:
export default {
...
data() {
var ClientNumberValidator = async (rule, value, callback) => {
const { code, data } = await checkClientNumberExists({
elementui登录界面param1: value1,
param2: value2,
})
if (code && code === 200 && data == true) {
callback(
new Error(
'The Number has already been used .'
)
)
}
}
return {
...

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。