Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // utilities.service.ts
- const focus: string = UtilitiesService.getAllFormErrors(formGroup)[0];
- public static getAllFormErrors(formGroup: FormGroup): string[] {
- let fieldName: string[] = [];
- for (const value in formGroup.controls) {
- const ctrl = formGroup.get(value);
- if (ctrl instanceof FormGroup) {
- // tried calling recursive function here - this.getAllFormErrors(ctrl);
- // loop around new formControls in nested FormGroup
- for (const value in ctrl.controls) {
- const nestedCtrl = ctrl.get(value);
- if (nestedCtrl.errors !== null) {
- fieldName.push(value);
- }
- }
- } else if (ctrl.errors !== null) {
- fieldName.push(value);
- }
- }
- return fieldName;
- }
- // expect the 'focus' variable to return the first field throwing a validation error
Add Comment
Please, Sign In to add comment