Forked from JohannesHoppe/get-form-validation-errors.ts
Last active
November 6, 2021 08:33
-
-
Save SebaM90/c30c5df2ea532af9bf25f53d93ebda50 to your computer and use it in GitHub Desktop.
Get all validation errors for Angular FormGroup
This file contains 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
import { FormGroup, ValidationErrors } from '@angular/forms'; | |
export function getFormValidationErrors(form: FormGroup): Array<any> { | |
const result: Array<any> = []; | |
Object.keys(form.controls).forEach((key) => { | |
const controlErrors: (ValidationErrors|null|undefined) = form.get(key)?.errors; | |
if (controlErrors) { | |
Object.keys(controlErrors).forEach((keyError) => { | |
result.push({ | |
control: key, | |
error: keyError, | |
value: controlErrors[keyError], | |
}); | |
}); | |
} | |
}); | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment