我的表单组件:
<Form ref="addBrand" :model="addBrand" :rules="addRules" :label-width="80" :inline="true">
<FormItem label="品牌名称:" prop="name">
<Select v-model="addBrand.name" @on-change="updateBrandList">
<Option v-for="(item,index) in brandInfoList" :value="index">{{item.brandName}}</Option>
</Select>
</FormItem>
</Form>
表单验证规则:
addRules: {
name: [{
required: true,
message:'请选择品牌',
trigger: 'change',
}],
line: [{
required: true,
message: '请选择所属线路',
trigger: 'blur',
}],
},
当我这样验证时:
this.$refs[name].validate((valid) => {
if (valid) {
//do something
} else {
this.$Message.error('表单验证失败!');
}
});