場(chǎng)景
表單中,有的校驗(yàn)元素不是標(biāo)準(zhǔn)的輸入框,如:


這種場(chǎng)景下,el-form-item上prop指定的值已經(jīng)變化后,不會(huì)觸發(fā)重新校驗(yàn),導(dǎo)致選中之后,錯(cuò)誤信息不消失
原因
跟蹤el的源碼,發(fā)現(xiàn)原因是,el自己的表單輸入元素在值改變后,會(huì)觸動(dòng)去觸發(fā)上層的el-form-item組件的
‘el.form.change’事件,el-from-item接收到此事件后,會(huì)重新校驗(yàn)
解決方法
對(duì)需要特殊輸入組件的綁定值,進(jìn)行watch,監(jiān)聽到變化后,用代碼去觸發(fā)上層el-form-item的‘el.form.change'事件
- 在組件上為el-from-item添加ref
<el-form-item label="選擇征信報(bào)告" prop="creditReportId" v-if="!infoDisabled" ref="creditReportItem">
<el-button
v-show="applyModel.creditReportId==''||applyModel.creditReportId==null"
type="text"
@click="checkCreditReport('apply')"
>選擇</el-button>
<el-button
v-show="!(applyModel.creditReportId==''||applyModel.creditReportId==null)"
type="text"
@click="previewReportFlag=true;"
>查看征信報(bào)告</el-button>
<el-button
v-show="!(applyModel.creditReportId==''||applyModel.creditReportId==null)"
type="text"
@click="checkCreditReport('apply')"
>變更征信報(bào)告</el-button>
</el-form-item>
- 監(jiān)聽值的變化
'applyModel.creditReportId'(){
this.$refs.creditReportItem.$emit('el.form.change'); //非el的輸入組件,值變化時(shí)不會(huì)觸發(fā)el-from-item的重新校驗(yàn),需要手動(dòng)觸發(fā)el.form.change事件
}
注意:
這個(gè)值的rule中,trigger必須是change