From 24b04fa09da082036809a99d2df48ab4fad09dde Mon Sep 17 00:00:00 2001 From: danranvm Date: Mon, 15 Aug 2022 20:42:00 +0800 Subject: [PATCH] feat(cdk:forms): setError supports path param --- .../forms/__tests__/abstractControl.spec.ts | 117 ++++++++++++++++-- .../cdk/forms/__tests__/formGroup.spec.ts | 6 + packages/cdk/forms/src/controls.ts | 19 ++- 3 files changed, 130 insertions(+), 12 deletions(-) diff --git a/packages/cdk/forms/__tests__/abstractControl.spec.ts b/packages/cdk/forms/__tests__/abstractControl.spec.ts index 86c38bcd2..ca0dd1a55 100644 --- a/packages/cdk/forms/__tests__/abstractControl.spec.ts +++ b/packages/cdk/forms/__tests__/abstractControl.spec.ts @@ -68,15 +68,27 @@ describe('abstractControl.ts', () => { expect(control.trigger).toEqual('change') }) - test('setValidators work', async () => { - const { required, minLength, email } = Validators + test('validators changes work', async () => { + const { required, email, minLength, maxLength } = Validators + const minLength5 = minLength(5) + const maxLength10 = maxLength(10) + + // setValidators start control.setValidators(required) expect(await control.validate()).toEqual({ required: { message: zhCNMessages.required({}, control) } }) - control.setValidators([email, minLength(5)]) + control.setValidators([email]) control.setValue('test') + expect(await control.validate()).toEqual({ + email: { actual: 'test', message: zhCNMessages.email({ actual: 'test' }, control) }, + }) + // setValidators end + + // addValidators start + control.addValidators(minLength5) + expect(await control.validate()).toEqual({ email: { actual: 'test', message: zhCNMessages.email({ actual: 'test' }, control) }, minLength: { @@ -86,27 +98,107 @@ describe('abstractControl.ts', () => { message: zhCNMessages.minLength({ actual: 4, isArray: false, minLength: 5 }, control), }, }) + + control.addValidators([maxLength10]) + control.setValue('test@idux.com') + + expect(await control.validate()).toEqual({ + maxLength: { + actual: 13, + isArray: false, + maxLength: 10, + message: zhCNMessages.maxLength({ actual: 13, isArray: false, maxLength: 10 }, control), + }, + }) + // addValidators end + + // removeValidators start + control.removeValidators([maxLength10]) + + expect(await control.validate()).toEqual(undefined) + + control.removeValidators(minLength5) + control.setValue('test') + + expect(await control.validate()).toEqual({ + email: { actual: 'test', message: zhCNMessages.email({ actual: 'test' }, control) }, + }) + // removeValidators end + + // hasValidator start + expect(control.hasValidator(email)).toEqual(true) + // hasValidator end + + // clearValidators start + control.clearValidators() + expect(await control.validate()).toEqual(undefined) + // clearValidators end }) - test('setAsyncValidators work', async () => { - const _asyncValidator = (key: string, error: unknown): AsyncValidatorFn => { + test('asyncValidators changes work', async () => { + const getAsyncValidator = (key: string, error: unknown): AsyncValidatorFn => { return (_: unknown) => { return Promise.resolve({ [key]: error } as ValidateErrors) } } const message1 = { message: 1 } const message2 = { message: 2 } + const message3 = { message: 3 } + const message4 = { message: 4 } + + const asyncValidator1 = getAsyncValidator('message1', message1) + const asyncValidator2 = getAsyncValidator('message2', message2) + const asyncValidator3 = getAsyncValidator('message3', message3) + const asyncValidator4 = getAsyncValidator('message4', message4) + + // setAsyncValidators start + control.setAsyncValidators(asyncValidator1) + + expect(await control.validate()).toEqual({ message1: message1 }) + + control.setAsyncValidators([asyncValidator2]) + + expect(await control.validate()).toEqual({ message2: message2 }) + + // setAsyncValidators end + + // addAsyncValidators start + control.addAsyncValidators(asyncValidator3) + + expect(await control.validate()).toEqual({ message2: message2, message3: message3 }) + + control.addAsyncValidators([asyncValidator4]) - control.setAsyncValidators(_asyncValidator('a', message1)) + expect(await control.validate()).toEqual({ + message2: message2, + message3: message3, + message4: message4, + }) + // addAsyncValidators end + + // removeAsyncValidators start + control.removeAsyncValidators([asyncValidator4]) + + expect(await control.validate()).toEqual({ message2: message2, message3: message3 }) + + control.removeAsyncValidators(asyncValidator3) - expect(await control.validate()).toEqual({ a: message1 }) + expect(await control.validate()).toEqual({ + message2: message2, + }) + // removeAsyncValidators end - control.setAsyncValidators([_asyncValidator('a', message1), _asyncValidator('b', message2)]) + // hasAsyncValidator start + expect(control.hasAsyncValidator(asyncValidator2)).toEqual(true) + // hasAsyncValidator end - expect(await control.validate()).toEqual({ a: message1, b: message2 }) + // clearAsyncValidators start + control.clearAsyncValidators() + expect(await control.validate()).toEqual(undefined) + // clearAsyncValidators end }) - test('setErrors, getError and hasError work', () => { + test('setErrors, getError, hasError and clearErrors work', () => { expect(control.errors.value).toBeUndefined() expect(control.getError('required')).toBeUndefined() expect(control.hasError('required')).toEqual(false) @@ -121,6 +213,11 @@ describe('abstractControl.ts', () => { expect(control.hasError('required')).toEqual(true) expect(control.hasError('max')).toEqual(false) + + control.clearErrors() + + expect(control.hasError('required')).toEqual(false) + expect(control.errors.value).toEqual(undefined) }) test('setParent work', () => { diff --git a/packages/cdk/forms/__tests__/formGroup.spec.ts b/packages/cdk/forms/__tests__/formGroup.spec.ts index 675b0c75b..442b64a4b 100644 --- a/packages/cdk/forms/__tests__/formGroup.spec.ts +++ b/packages/cdk/forms/__tests__/formGroup.spec.ts @@ -156,6 +156,12 @@ describe('formGroup.ts', () => { expect(group.get(['array', 2])).toBeUndefined() expect(group.get('group.control.test')).toBeUndefined() }) + + test('setErrors with path work', async () => { + group.setErrors({ test: {} }, 'control') + + expect(group.get('control').hasError('test')).toEqual(true) + }) }) describe('trigger work', () => { diff --git a/packages/cdk/forms/src/controls.ts b/packages/cdk/forms/src/controls.ts index 20218df9b..bd6728bf0 100644 --- a/packages/cdk/forms/src/controls.ts +++ b/packages/cdk/forms/src/controls.ts @@ -486,9 +486,24 @@ export abstract class AbstractControl { /** * Sets errors on a form control when running validations manually, rather than automatically. + * + * If you want to clear errors, you can pass in a undefined. + * + */ + setErrors(errors?: ValidateErrors, path?: ControlPathType): void { + if (!isNil(path)) { + this.get(path)?.setErrors(errors) + } else { + this._errors.value = errors + } + } + + /** + * Empties out the errors. + * */ - setErrors(errors?: ValidateErrors): void { - this._errors.value = errors + clearErrors(path?: ControlPathType): void { + this.setErrors(undefined, path) } /**