From 7aed5b3bebd67a60b44b293bf385667ee63ad318 Mon Sep 17 00:00:00 2001 From: hans000 Date: Wed, 17 May 2023 18:37:42 +0800 Subject: [PATCH] feat: support groupId and ignore rule --- docs/examples/validate.tsx | 47 ++++++++++++++++++++++++++++++++++---- src/Field.tsx | 10 +++++++- src/interface.ts | 4 ++++ 3 files changed, 55 insertions(+), 6 deletions(-) diff --git a/docs/examples/validate.tsx b/docs/examples/validate.tsx index 3a3b29f9..338dadde 100644 --- a/docs/examples/validate.tsx +++ b/docs/examples/validate.tsx @@ -50,7 +50,7 @@ export default () => { return ( - + { @@ -64,8 +64,10 @@ export default () => { ({ + groupId: 'GroupA', + id: '3', validator(_, __, callback) { if (context.isFieldTouched('password2')) { context.validateFields(['password2']); @@ -85,8 +87,10 @@ export default () => { ({ + groupId: 'GroupB', + id: '5', validator(rule, value, callback) { const { password } = context.getFieldsValue(true); if (password !== value) { @@ -102,7 +106,7 @@ export default () => { {password2Error} - + {(control, meta) => (
Use Meta: @@ -117,8 +121,9 @@ export default () => { name="validateTrigger" validateTrigger={['onSubmit', 'onChange']} rules={[ - { required: true, validateTrigger: 'onSubmit' }, + { required: true, validateTrigger: 'onSubmit', id: '7' }, { + id: '8', validator(rule, value, callback) { setTimeout(() => { if (Number(value).toString() === value) { @@ -155,6 +160,38 @@ export default () => { > Validate All + + + diff --git a/src/Field.tsx b/src/Field.tsx index ce706ff9..1cb5d9e2 100644 --- a/src/Field.tsx +++ b/src/Field.tsx @@ -363,7 +363,7 @@ class Field extends React.Component implements F const namePath = this.getNamePath(); const currentValue = this.getValue(); - const { triggerName, validateOnly = false } = options || {}; + const { triggerName, validateOnly = false, groupId, ignore } = options || {}; // Force change to async to avoid rule OOD under renderProps field const rootPromise = Promise.resolve().then(() => { @@ -387,6 +387,14 @@ class Field extends React.Component implements F }); } + if (groupId) { + filteredRules = filteredRules.filter(rule => rule.groupId === groupId) + } + + if (ignore) { + filteredRules = filteredRules.filter(rule => !ignore(rule)) + } + const promise = validateRules( namePath, currentValue, diff --git a/src/interface.ts b/src/interface.ts index 2fd74a4c..70707cd4 100644 --- a/src/interface.ts +++ b/src/interface.ts @@ -68,6 +68,8 @@ interface BaseRule { transform?: (value: StoreValue) => StoreValue; type?: RuleType; whitespace?: boolean; + id?: string + groupId?: string /** Customize rule level `validateTrigger`. Must be subset of Field `validateTrigger` */ validateTrigger?: string | string[]; @@ -131,6 +133,8 @@ export interface ValidateOptions { * Validate only and not trigger UI and Field status update */ validateOnly?: boolean; + groupId?: string + ignore?: (rule: RuleObject) => boolean } export type ValidateFields = {