Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save domagoj03/befeb17c17ff3ede2d36b8f59f0ad6a6 to your computer and use it in GitHub Desktop.
Save domagoj03/befeb17c17ff3ede2d36b8f59f0ad6a6 to your computer and use it in GitHub Desktop.
Get all validation errors for Angular FormGroup
import {FormGroup, ValidationErrors} from '@angular/forms';
export interface IFormError {
control: string;
error: string;
value: any;
}
export function getFormValidationErrors(form: FormGroup) {
const result = [];
Object.keys(form.controls).forEach(key => {
const formProperty = form.get(key);
if (formProperty instanceof FormGroup) {
result.push(...getFormValidationErrors(formProperty))
}
const controlErrors: ValidationErrors = formProperty.errors;
if (controlErrors) {
Object.keys(controlErrors).forEach(keyError => {
result.push({
'control': key,
'error': keyError,
'value': controlErrors[keyError]
});
});
}
});
return result;
}
@baleeds
Copy link

baleeds commented Jan 7, 2021

It seems this won't catch cross validations on the form groups. Is that true?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment