html 部分
选 是 进行必填校验
<el-row>
<el-col :span="17">
<el-form-item
v-if="form.researchInfoVos[index].state === 1"
label=""
:prop="'researchInfoVos.'+ index + '.answer'"
:rules="{
required: true, validator:(rule, value, cb) => answerFN(rule, value, cb, index), trigger: 'blur'
}"
>
<el-input v-model="form.researchInfoVos[index].answer" placeholder="如是.需要说明详细情况……" />
</el-form-item>
</el-col>
</el-row>
// 带上校验回调
answerFN(rule, value, cb, index) {
if (this.form.researchInfoVos[index].state === 1 && value === '') {
cb(new Error('请输入内容'))
} else {
cb()
}
},
let field = []
if (this.anum === 1) {
field = ['employeeInfoVo.name', 'employeeInfoVo.phoneNumber']
// 更改为 动态的 校验添加判断
this.form.researchInfoVos.forEach((e, i) => {
if (e.state === 1) {
field.push('researchInfoVos.' + i + '.answer')
} else {
var index = field.indexOf('researchInfoVos.' + i + '.answer')
if (index !== -1) {
field.slice(index, 1)
}
}
})
}
this.anum === 4 && (field = ['postInfoVo.entryTime', 'postInfoVo.departmentId', 'postInfoVo.companyId', 'postInfoVo.immediateSuperiorId', 'postInfoVo.state'])
Promise.all(
field.map(item => {
const pList = new Promise((res, rej) => {
// 部分表单的校验
this.$refs['form'].validateField(item, valid => {
res(valid)
})
})
return pList
})
).then(results => {
results = results.filter(item => item)
if (!results.length) { // 为空校验通过
this.anum++
}
})