Skip to content

Commit

Permalink
fix: 小程序无法清空验证信息
Browse files Browse the repository at this point in the history
  • Loading branch information
BeiQiaoT committed Jan 25, 2022
1 parent b70c404 commit e1188a5
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 7 additions & 1 deletion pages/componentsC/form/form.nvue
Original file line number Diff line number Diff line change
Expand Up @@ -420,8 +420,14 @@
})
},
reset() {
this.$refs.form1.clearValidate()
const validateList = ['userInfo.name', 'userInfo.sex', 'radiovalue1', 'checkboxValue1', 'intro',
'hotel', 'code', 'userInfo.birthday']
this.$refs.form1.resetFields()
this.$refs.form1.clearValidate()
setTimeout(()=>{
this.$refs.form1.clearValidate(validateList)
// 或者使用 this.$refs.form1.clearValidate()
},10)
},
hideKeyboard() {
uni.hideKeyboard()
Expand Down
11 changes: 6 additions & 5 deletions uni_modules/uview-ui/components/u-form/u-form.vue
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@
setRules(rules) {
// 判断是否有规则
if (Object.keys(rules).length === 0) return;
if (Object.keys(this.model).length === 0) {
uni.$u.error('设置rules,model必须设置!');
if (process.env.NODE_ENV === 'development' && Object.keys(this.model).length === 0) {
uni.$u.error('设置rules,model必须设置!如果已经设置,请刷新页面。');
return;
};
this.formRules = rules;
Expand All @@ -116,15 +116,16 @@
props = [].concat(props);
this.children.map((child) => {
// 如果u-form-item的prop在props数组中,则清除对应的校验结果信息
if (props.includes(child.props)) {
if (props[0] === undefined || props.includes(child.prop)) {
child.message = null;
}
});
},
// 对部分表单字段进行校验
async validateField(value, callback, event = null) {
if (Object.keys(this.formRules).length === 0) {
uni.$u.error('未设置rules,请看文档说明!');
// 开发环境才提示,生产环境不会提示
if (process.env.NODE_ENV === 'development' && Object.keys(this.formRules).length === 0) {
uni.$u.error('未设置rules,请看文档说明!如果已经设置,请刷新页面。');
return;
}
// $nextTick是必须的,否则model的变更,可能会延后于此方法的执行
Expand Down

0 comments on commit e1188a5

Please sign in to comment.