一个表单,有很多个必填项,需求:这些必填项如果有一个有填其他的全变为不是必填项,如果都没有填,则都是必填项
export default {
data () {
return {
dynamicValidateForm: {
value: '',
email: ''
},
rules: {
email: [{ required: true, message: '请输入邮箱地址', trigger: 'blur' }],
value: [{ required: true, message: '请输入正确的邮箱地址', trigger: 'blur' }]
}
}
},
watch: {
dynamicValidateForm: {
handler (val, oval) {
console.log(val, oval)
for (var i in this.dynamicValidateForm) {
console.log(this.dynamicValidateForm[i])
if (i) {
this.rules[i][0].required = true
} if (!i) {
this.rules[i][0].required = false
}
},
deep: true
}
}
},
methods: {
submitForm (formName) {
this.$refs[formName].validate((valid) => {
if (valid) {
alert('submit!')
} else {
console.log('error submit!!')
return false
}
})
}
}
}
请问一下watch里该怎么写,给出感谢
大概这样写: