Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mjurincic/25dec97f30e68e2aaaf647adf24a6a44 to your computer and use it in GitHub Desktop.
Save mjurincic/25dec97f30e68e2aaaf647adf24a6a44 to your computer and use it in GitHub Desktop.
Get all validation errors for Angular FormGroup
import { FormGroup, ValidationErrors } from '@angular/forms';
export function getFormValidationErrors(form: FormGroup) {
const result = [];
Object.keys(form.controls).forEach(key => {
const controlErrors: ValidationErrors = 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