Created
April 24, 2018 15:57
-
-
Save roalcantara/b22c9c1d0a3b84bb8e0bd1a9734f5997 to your computer and use it in GitHub Desktop.
Get All Angular 2 Form Errors
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| getAllErrors(form: FormGroup | FormArray): { [key: string]: any; } | null { | |
| let hasError = false | |
| const result = Object.keys(form.controls).reduce((acc, key) => { | |
| const control = form.get(key) | |
| const errors = (control instanceof FormGroup || control instanceof FormArray) | |
| ? this.getAllErrors(control) | |
| : control.errors | |
| if (errors) { | |
| acc[key] = errors | |
| hasError = true | |
| } | |
| return acc | |
| }, {} as { [key: string]: any; }) | |
| return hasError ? result : null | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment