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) { | |
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; | |
} |
wow! that's evil! 😄
(fixed)
Thanks. I needed support for nested forms so I forked your gist.
https://gist.github.com/domagoj03/befeb17c17ff3ede2d36b8f59f0ad6a6
It can even be modified to namespace errors depending on form but right now this seems sufficient.
Useful, thanks !
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You might want to remove that space after the key "control " (drove me crazy)