rule自定义验证表单
还在纠结表单中input中rule规则太多,提示语不准确或者不全等问题么,直接用自定义校验。
<template>
<el-form :model="form" :rules="rules">
<el-form-item
label="名称:"
prop="name"
>
<el-input
v-model="form.name"
size="small"
maxlength="50"
></el-input>
</el-form-item>
</el-form>
</template>
<script>
export default {
data() {
return {
forms: {},
rules: {
name: [
{
required: true,
validator: (rules, value, callback) => {
if (!value) {
return callback(new Error("*名称不能为空"));
}
else if (value.length > 6) {
return callback(new Error("*名称不能超过6位"));
}
else if ...
return callback();
},
trigger: "blur"
}
]
},
};
},
}
</script>