diff --git a/src/index.ts b/src/index.ts index 4609d14..5794b47 100644 --- a/src/index.ts +++ b/src/index.ts @@ -214,6 +214,7 @@ export abstract class AbstractControl { } else { this.errors[error.code] = true } + this._updateStatus() } /** @@ -224,9 +225,13 @@ export abstract class AbstractControl { */ @action public removeError(error: IValidationError) { - if (this.errors && this.errors[error.code]) { + if (!this.errors && !this.errors[error.code]) return + if (Object.keys(this.errors).length !== 1) { delete this.errors[error.code] + } else { + this.errors = null } + this._updateStatus() } /** @@ -308,7 +313,7 @@ export abstract class AbstractControl { if (this.enabled) { const validationResult = this._runValidator() this.errors = validationResult ? validationResult : null - this.status = this._calculateStatus() + this._updateStatus(true) } if (this.parent && !onlySelf) { @@ -377,6 +382,15 @@ export abstract class AbstractControl { return x } + /** @internal */ + _updateStatus(onlySelf?: boolean): void { + this.status = this._calculateStatus() + + if (this.parent && !onlySelf) { + this.parent._updateStatus() + } + } + /** @internal */ public _updateTouched(onlySelf?: boolean): void { this.touched = this._anyControlsTouched() diff --git a/test/mobx-reactive-forms.test.ts b/test/mobx-reactive-forms.test.ts index dbc9555..3d93ec9 100644 --- a/test/mobx-reactive-forms.test.ts +++ b/test/mobx-reactive-forms.test.ts @@ -1,8 +1,22 @@ /** * Dummy test */ -describe('Dummy test', () => { - it('works if true is truthy', () => { - expect(true).toBeTruthy() +import { FormControl } from '../src' +import { NOT_EMPTY_ERROR } from '../src/validators' + +describe('Abstract control', () => { + describe('Manual validation', () => { + it('invalid if error was set', () => { + const control = new FormControl('') + control.setError({ code: NOT_EMPTY_ERROR }) + expect(control.invalid).toBeTruthy() + }) + it('valid if only error was removed', () => { + const control = new FormControl('') + control.setError({ code: NOT_EMPTY_ERROR }) + control.removeError({ code: NOT_EMPTY_ERROR }) + console.log(control.status, control.errors) + expect(control.valid).toBeTruthy() + }) }) })