Skip to content

Instantly share code, notes, and snippets.

@JohannesHoppe
Last active May 31, 2024 20:55
Show Gist options
  • Save JohannesHoppe/e8d07d63fc345a5fdfdf4fc4989ef2e4 to your computer and use it in GitHub Desktop.
Save JohannesHoppe/e8d07d63fc345a5fdfdf4fc4989ef2e4 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;
}
@DavidSmith777
Copy link

This is the best. I have so many nested components it's a pain to hunt down errors.

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