if (value.length > 15 || value.length < 2) {
callback(new Error('长度必须为2~8个字符'))
} else {
var reg = new RegExp("[`~!@#$^&*()=|{}':',[].<>《》/?~!@#¥……&*()——|{}【】‘;:”“'。,、?]")
if (reg.test(value)) {
callback(new Error('不能含有特殊字符'))
} else {
callback()
}
}
},
trigger: 'change'
}
],
moreNotifyOjbectEmail: [{ required: true, message: '请输入邮箱地址', trigger: 'blur' },
{ type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
] }
这里需要注意的是:rules是每个表单都要都要添加的,有多个的话就要给每个表单绑定一个规则
<el-form-item class="rules" label="通知对象:" :prop="'moreNotifyObject.' + index +'.notifyobject'" :rules="moreNotifyOjbectRules.moreNotifyOjbectName">
<el-form-item class="rules" label="邮箱:" :prop="'moreNotifyObject.'+ index +'.email'" :rules="moreNotifyOjbectRules.moreNotifyOjbectEmail">另外要注意的是:prop,正常表单验证单项是依靠prop,但是动态生成话要用:prop。
书写的语法是:
prop="'moreNotifyObject.' + index +'.notifyobject'",moreNotifyObject是v-for绑定的数组,index是索引,notifyobject是表单绑定的v-model的名称,然后用.把他们链接起来。所以总结起来的语法就是:
prop="'v-for绑定的数组
.' + index + '.v-model绑定的变量'”还有一个需要注意就是v-for的写法,要将表单的model名写进去
<div class="moreRulesIn" v-for="(item, index) in ruleForm.moreNotifyObject" :key="item.key">还有要注意的就是v-for绑定的数组也要在表单的对象里,写在表单对象外是验证不了的,在data里添加
ruleform:{
moreNotifyObject: [{
notifyobject: '',
email: ''
}]}然后新增联系人的函数应该这样写
addUser() {
this.ruleForm.moreNotifyObject.push({
notifyobject: '',
email: ''
})
}同理删除联系人也是
deleteRules(item, index) {
this.index = this.ruleForm.moreNotifyObject.indexOf(item)
if (index !== -1) {
this.ruleForm.moreNotifyObject.splice(index, 1)
}
}如果一开始只想让默认必填的表单显示,而新增的不显示,如文章最开头的表现一样,则可以在methods中初始化v-for绑定的数组










