From e5b930837412f848209c55e6d2d727b378f87d4d Mon Sep 17 00:00:00 2001 From: tangjinzhou <415800467@qq.com> Date: Sat, 11 Jun 2022 21:56:41 +0800 Subject: [PATCH] fix: global form message not work, close #5693 --- components/config-provider/context.ts | 4 ++-- components/form/Form.tsx | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/components/config-provider/context.ts b/components/config-provider/context.ts index 2d763e7d9..f1661a215 100644 --- a/components/config-provider/context.ts +++ b/components/config-provider/context.ts @@ -1,5 +1,5 @@ import type { ExtractPropTypes, InjectionKey, PropType, Ref } from 'vue'; -import { inject, provide } from 'vue'; +import { computed, inject, provide } from 'vue'; import type { ValidateMessages } from '../form/interface'; import type { RequiredMark } from '../form/Form'; import type { RenderEmptyHandler } from './renderEmpty'; @@ -17,7 +17,7 @@ export const useProvideGlobalForm = (state: GlobalFormCOntextProps) => { }; export const useInjectGlobalForm = () => { - return inject(GlobalFormContextKey, {}); + return inject(GlobalFormContextKey, { validateMessages: computed(() => undefined) }); }; export const GlobalConfigContextKey: InjectionKey = diff --git a/components/form/Form.tsx b/components/form/Form.tsx index 36b00e835..ac33a8b5c 100755 --- a/components/form/Form.tsx +++ b/components/form/Form.tsx @@ -30,6 +30,7 @@ import useConfigInject from '../_util/hooks/useConfigInject'; import { useProvideForm } from './context'; import type { SizeType } from '../config-provider'; import useForm from './useForm'; +import { useInjectGlobalForm } from '../config-provider/context'; export type RequiredMark = boolean | 'optional'; export type FormLayout = 'horizontal' | 'inline' | 'vertical'; @@ -127,10 +128,11 @@ const Form = defineComponent({ return true; }); const mergedColon = computed(() => props.colon ?? contextForm.value?.colon); + const { validateMessages: globalValidateMessages } = useInjectGlobalForm(); const validateMessages = computed(() => { return { ...defaultValidateMessages, - ...contextForm.value?.validateMessages, + ...globalValidateMessages.value, ...props.validateMessages, }; });