From 5bdd6f4007de5a42dac3874354deb04c6d185d4f Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sat, 31 Jul 2021 22:41:36 +0800 Subject: [PATCH] fix: filter invalid form items #4435 --- components/form/FormItem.tsx | 35 +++++++++++++++++++++++++---------- v2-doc | 2 +- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/components/form/FormItem.tsx b/components/form/FormItem.tsx index af687fc34..bba61927c 100644 --- a/components/form/FormItem.tsx +++ b/components/form/FormItem.tsx @@ -1,4 +1,4 @@ -import type { PropType, ExtractPropTypes, ComputedRef } from 'vue'; +import { PropType, ExtractPropTypes, ComputedRef, watch } from 'vue'; import { defineComponent, computed, nextTick, ref, watchEffect, onBeforeUnmount } from 'vue'; import cloneDeep from 'lodash-es/cloneDeep'; import PropTypes from '../_util/vue-types'; @@ -271,16 +271,31 @@ export default defineComponent({ clearValidate, resetField, }); - formContext.addField(eventKey, { - fieldValue, - fieldId, + let registered = false; + watch( fieldName, - resetField, - clearValidate, - namePath, - validateRules, - rules: rulesRef, - }); + val => { + if (val) { + if (!registered) { + registered = true; + formContext.addField(eventKey, { + fieldValue, + fieldId, + fieldName, + resetField, + clearValidate, + namePath, + validateRules, + rules: rulesRef, + }); + } + } else { + registered = false; + formContext.removeField(eventKey); + } + }, + { immediate: true }, + ); onBeforeUnmount(() => { formContext.removeField(eventKey); }); diff --git a/v2-doc b/v2-doc index fc73e64bc..e67d63729 160000 --- a/v2-doc +++ b/v2-doc @@ -1 +1 @@ -Subproject commit fc73e64bcdf41be854c2f7a907c05b056ad652ab +Subproject commit e67d63729715291f481029fe9497f0109f328504