Skip to content

Instantly share code, notes, and snippets.

@Damian96
Created March 30, 2026 07:06
Show Gist options
  • Select an option

  • Save Damian96/ffea6821dd5fdce3cfff57b2782a70cb to your computer and use it in GitHub Desktop.

Select an option

Save Damian96/ffea6821dd5fdce3cfff57b2782a70cb to your computer and use it in GitHub Desktop.
Collect and Print Form Errors in an Angular Reactive Form

Hereโ€™s a JavaScript console helper you can paste into the browser DevTools.

You pass:

  • The Angular component instance (usually accessible as ng.getComponent($0) in dev mode)
  • The FormGroup property name (string)

It will recursively collect all controls with errors, including nested FormGroup and FormArray, and print them clearly.


โœ… Usage Example (Angular Dev Mode)

  1. Select the component element in Chrome DevTools.
  2. Run:
const cmp = ng.getComponent($0);
printFormErrors(cmp, 'myForm'); // replace 'myForm' with your FormGroup name

๐Ÿง  Console Script

function printFormErrors(component, formGroupName) {
  if (!component) {
    console.error('Component instance not provided.');
    return;
  }

  const form = component[formGroupName];

  if (!form) {
    console.error(`FormGroup "${formGroupName}" not found on component.`);
    return;
  }

  const errorsList = [];

  function collectErrors(control, path = '') {
    if (!control) return;

    // If control has errors, store them
    if (control.errors) {
      errorsList.push({
        controlPath: path,
        errors: control.errors,
        value: control.value,
        status: control.status
      });
    }

    // Handle FormGroup or FormArray
    if (control.controls) {
      if (Array.isArray(control.controls)) {
        // FormArray
        control.controls.forEach((child, index) => {
          collectErrors(child, `${path}[${index}]`);
        });
      } else {
        // FormGroup
        Object.keys(control.controls).forEach(key => {
          const newPath = path ? `${path}.${key}` : key;
          collectErrors(control.controls[key], newPath);
        });
      }
    }
  }

  collectErrors(form);

  if (errorsList.length === 0) {
    console.log('โœ… No form control errors found.');
  } else {
    console.group(`โŒ Found ${errorsList.length} control(s) with errors`);
    errorsList.forEach(err => {
      console.group(`Control: ${err.controlPath}`);
      console.log('Errors:', err.errors);
      console.log('Value:', err.value);
      console.log('Status:', err.status);
      console.groupEnd();
    });
    console.groupEnd();
  }

  return errorsList;
}

๐Ÿ”Ž What It Handles

  • FormControl

  • FormGroup (nested)

  • FormArray

  • Deeply nested structures

  • Prints:

    • Full control path
    • Errors object
    • Current value
    • Status

--

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment