From 82af5e1697e7ad96a67670df340a659e184a9799 Mon Sep 17 00:00:00 2001 From: ryuurock Date: Mon, 25 Apr 2022 17:52:15 +0800 Subject: [PATCH] fix(core): fix field validateFirst not working (#3071) --- packages/core/src/__tests__/field.spec.ts | 45 +++++++++++++++++++++++ packages/core/src/shared/internals.ts | 2 +- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/packages/core/src/__tests__/field.spec.ts b/packages/core/src/__tests__/field.spec.ts index fedd080739d..5b0688963da 100644 --- a/packages/core/src/__tests__/field.spec.ts +++ b/packages/core/src/__tests__/field.spec.ts @@ -2107,3 +2107,48 @@ test('destroy field need auto remove initialValues', () => { expect(form.initialValues.aa).toBeUndefined() expect(form.values.aa).toBeUndefined() }) + +test('validateFirst', async () => { + const form = attach(createForm({ + validateFirst: false + })) + const aaValidate = jest.fn(() => 'aaError') + const aa = attach( + form.createField({ + name: 'aa', + validateFirst: true, + validator: [ + aaValidate, + aaValidate, + ] + }) + ) + await aa.onInput('aa') + const bbValidate = jest.fn(() => 'bbError') + const bb = attach( + form.createField({ + name: 'bb', + validator: [ + bbValidate, + bbValidate, + ], + validateFirst: false, + }) + ) + await bb.onInput('bb') + const ccValidate = jest.fn(() => 'ccError') + const cc = attach( + form.createField({ + name: 'cc', + validator: [ + ccValidate, + ccValidate, + ], + }) + ) + await cc.onInput('cc') + + expect(aaValidate).toBeCalledTimes(1) + expect(bbValidate).toBeCalledTimes(2) + expect(ccValidate).toBeCalledTimes(2) +}) diff --git a/packages/core/src/shared/internals.ts b/packages/core/src/shared/internals.ts index 22019670587..d5321b121ad 100644 --- a/packages/core/src/shared/internals.ts +++ b/packages/core/src/shared/internals.ts @@ -305,7 +305,7 @@ export const validateToFeedbacks = async ( ) => { const results = await validate(field.value, field.validator, { triggerType, - validateFirst: field.props.validateFirst || field.form.props.validateFirst, + validateFirst: field.props.validateFirst ?? field.form.props.validateFirst, context: { field, form: field.form }, })