实现效果:连锁表单验证,在LED版本号选择为升级版LED时候,标题名称不超过8
实现代码:
<el-form
:model="configuration"
ref="form"
:rules="rule"
size="small"
label-width="130px"
v-if="isture == 1"
>
<el-form-item label="LED版本号" prop="version">
<el-select
style="width: 100%"
v-model="configuration.version"
@change="changeBB"
>
<el-option
v-for="item in versionOption"
:key="item.value"
:label="item.label"
:value="item.value"
>{{ item.label }}</el-option
>
</el-select>
</el-form-item>
<el-form-item label="标题名称" prop="titile">
<el-input
placeholder="请输入内容"
v-model="configuration.titile"
></el-input>
</el-form-item>
</el-form>
data() {
// 表单校验规则
var validatePass = (rule, value, callback) => {
//console.log("当前表单数据", this.configuration.version, value.length);
if (this.configuration.version == 2) {
if (value.length > 10) {
callback(new Error("标题字数过长"));
} else {
callback();
}
}
if (value === "") {
callback(new Error("标题不可为空"));
} else {
callback();
}
};
return {
//表单
configuration: {
version: "",
titile:"",
},
versionOption: [
{
value: 1,
label: "烽云智能LED"
},
{
value: 2,
label: "烽云升级版LED"
}
],
rule: {
titile: [
{
// required: true,
validator: validatePass,
// message: "标题有误",
trigger: "blur"
}
],
}
};
},
表单提交代码:
sumbit() {
this.isture == 0;
this.$refs["form"].validate(r => {
if (r) {
this.configuration.contentOne = this.configuration.contentOne.join(
","
);
this.configuration.index1 = this.configuration.index1.join(",");
this.configuration.index2 = this.configuration.index2.join(",");
this.configuration.index3 = this.configuration.index3.join(",");
this.configuration.index4 = this.configuration.index4.join(",");
this.configuration.index5 = this.configuration.index5.join(",");
if (this.isadd == true) {
this.doadd();
} else {
this.edit();
}
}
});
},
注意:如果点击表单的确认按钮,没有任何效果,validate函数不运行就需要确定验证是否出问题
每一个 if 都要对应了一个 else 并且每个条件都要 callback 确保在不同情况下都会执行callback