Skip to content

Commit

Permalink
fix(manual-validation): update status after manual update of errors
Browse files Browse the repository at this point in the history
  • Loading branch information
makarov-roman committed Sep 24, 2018
1 parent 060749f commit 0239e1b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
18 changes: 16 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ export abstract class AbstractControl {
} else {
this.errors[error.code] = true
}
this._updateStatus()
}

/**
Expand All @@ -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()
}

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
20 changes: 17 additions & 3 deletions test/mobx-reactive-forms.test.ts
Original file line number Diff line number Diff line change
@@ -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()
})
})
})

0 comments on commit 0239e1b

Please sign in to comment.