赞
踩
在vue项目的data中,我们可能会声明如下结构的数据:
people: {
companyId: "",
linkman: {
name: "",
phone: "",
}
},
这时候,以下的表单校验方法会失效:
<a-form-model :rules="rules" :model="people" ref="linkmanForm">
<a-form-model-item label="姓名" prop="name">
<a-input v-model="people.linkman.name"/>
</a-form-model-item>
<a-form-model-item label="电话" prop="phone">
<a-input v-model="people.linkman.phone"/>
</a-form-model-item>
</a-form-model>
rules: {
name: [
{required: true, message: '请输入联系人姓名', trigger: 'blur'},
],
phone: [
{validator: checkPhone, trigger: 'blur'},
],
}
我们做如下修改
<a-form-model-item></a-form-model-item>
的prop从第二层数据开始写 <a-form-model :rules="rules" :model="people" ref="linkmanForm">
<a-form-model-item label="姓名" prop="linkman.name">
<a-input v-model="people.linkman.name"/>
</a-form-model-item>
<a-form-model-item label="电话" prop="linkman.phone">
<a-input v-model="people.linkman.phone"/>
</a-form-model-item>
</a-form-model>
2.rules里面的每一个规则名称使用单引号包裹起来
rules: {
'linkman.name': [
{required: true, message: '请输入联系人姓名', trigger: 'blur'},
],
'linkman.phone': [
{validator: checkPhone, trigger: 'blur'},
],
}
注意:a-form-model的prop、data里linkman的属性名(name、age)和rules里面的属性名(name、age)都必须一致
最后,大工告成,可以成功进行校验了
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。