Read Time:36 Second
Reference 1: https://gist.github.com/JohannesHoppe/e8d07d63fc345a5fdfdf4fc4989ef2e4
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; }
Reference 2:
public ngOnInit() { this.form.valueChanges.subscribe(data => this.onValueChanged(data)); } public onValueChanged(data?: any): void { this.getFormValidationErrors(); } getFormValidationErrors() { Object.keys(this.form.controls).forEach(key => { const controlErrors: ValidationErrors = this.form.get(key).errors; if (controlErrors != null) { Object.keys(controlErrors).forEach(keyError => { console.log('Key control: ' + key + ', keyError: ' + keyError + ', err value: ', controlErrors[keyError]); }); } }); }