Skip to content

Instantly share code, notes, and snippets.

@Eonasdan
Created January 24, 2022 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Eonasdan/1a2d00c9aed9678dfe7e737515b79657 to your computer and use it in GitHub Desktop.
Save Eonasdan/1a2d00c9aed9678dfe7e737515b79657 to your computer and use it in GitHub Desktop.

It is possible to get an Angular component through the Chrome console. This allows you to inspect form controls, errors, etc.

  1. Select the Angular component in the DevTools DOM
  2. In the console you can use ng.getComponent($0).formGroup.valid where formGroup is the name of a public property that's an Angular FormGroup

You can use this function to get a list of all the errors in a form group.

var getFormErrors = (form) => {
    if (!form.controls) {
      // Return FormControl errors or null
      return form.errors ?? null;
    }
    
      const groupErrors = form.errors;
      // Form group can contain errors itself, in that case add 'em
      const formErrors = groupErrors ? { groupErrors } : {};
      Object.keys(form.controls).forEach((key) => {
        // Recursive call of the FormGroup fields
        const error = this.getFormErrors(form.get(key));
        if (error !== null) {
          // Only add error if not null
          formErrors[key] = error;
        }
      });
      // Return FormGroup errors or null
      return Object.keys(formErrors).length > 0 ? formErrors : null;
    }

getFormErrors(ng.getComponent($0).formGroup);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment