Guest User

Untitled

a guest
Jan 22nd, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. // utilities.service.ts
  2.  
  3. const focus: string = UtilitiesService.getAllFormErrors(formGroup)[0];
  4.  
  5.  
  6. public static getAllFormErrors(formGroup: FormGroup): string[] {
  7. let fieldName: string[] = [];
  8.  
  9. for (const value in formGroup.controls) {
  10. const ctrl = formGroup.get(value);
  11. if (ctrl instanceof FormGroup) {
  12.  
  13. // tried calling recursive function here - this.getAllFormErrors(ctrl);
  14.  
  15. // loop around new formControls in nested FormGroup
  16. for (const value in ctrl.controls) {
  17.  
  18. const nestedCtrl = ctrl.get(value);
  19. if (nestedCtrl.errors !== null) {
  20. fieldName.push(value);
  21. }
  22. }
  23. } else if (ctrl.errors !== null) {
  24. fieldName.push(value);
  25. }
  26. }
  27. return fieldName;
  28. }
  29.  
  30. // expect the 'focus' variable to return the first field throwing a validation error
Add Comment
Please, Sign In to add comment