Angular - Dumb Error Dumper

Article Information

Article Content

Angular - Dumb Error Dumper

Angular - Dumb error dump.

Getting Validators right for a complex reactive form can be a challenge so being able to just dump all validation errors is very useful as a diagnostic.

getAllErrors(): object[] {
  const returner: object[] = [];
  Object.keys(this.controls).forEach((key) => {
    const controlErrors: ValidationErrors | null | undefined = this.get(key)?.errors;
    if (controlErrors) {
      Object.keys(controlErrors).forEach((keyError) => {
        returner.push({
          'key': key,
          'error': keyError,
          'error value': controlErrors[keyError],
        });
      });
    } // controlErrors?
  }); // forEach
  return returner;
}

This function is intended to be added inside a class that extends FormGroup. For more complex forms, we find it better to pull the form into its own class so it doesn't clutter up the component driving the page.

It just finds all controls for the form, checks for errors, then adds them to a big array it sends back to the calling page.

Very useful with PrettyJsonPipe.

Our Mission

At Wildlink, our mission is to deliver the best business solutions we can to enable our clients to gather and unlock the answers in their data.
We are committed to fostering long-term partnerships built on trust, integrity, and a relentless pursuit of innovation.