Created
March 3, 2021 04:52
-
-
Save jvinhit/18fb8d31d6c7aedc6bdc016c1b2b9478 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment