Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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;
}
@lipownski

This comment has been minimized.

Copy link

commented Jul 19, 2019

You might want to remove that space after the key "control " (drove me crazy)

@JohannesHoppe

This comment has been minimized.

Copy link
Owner Author

commented Jul 19, 2019

wow! that's evil! 😄

(fixed)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.