Skip to content

Instantly share code, notes, and snippets.

@roalcantara
Created April 24, 2018 15:57
Show Gist options
  • Select an option

  • Save roalcantara/b22c9c1d0a3b84bb8e0bd1a9734f5997 to your computer and use it in GitHub Desktop.

Select an option

Save roalcantara/b22c9c1d0a3b84bb8e0bd1a9734f5997 to your computer and use it in GitHub Desktop.
Get All Angular 2 Form Errors
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