What’s new in ngspot/ngx-errors 3.1.3?
Tags: angular, errors, libraries, javascript
Published: 2023-10-14 18:53:56

---
title: What’s new in ngspot/ngx-errors 3.1.3?
published: true
date: 2022-01-14 09:31:19 UTC
tags: angular,errorhandling,libraries,javascript
canonical_url: https://medium.com/@dmitrief/whats-new-in-ngspot-ngx-errors-3-1-3-d45ceea96d27
---

![](https://cdn-images-1.medium.com/max/272/1*Ubm7YB6ifLYs41XLdJJtqA.png)

### 🚀 Feature 1: integration with @angular/material

[@ngspot/ngx-errors](https://www.npmjs.com/package/@ngspot/ngx-errors) is a library that makes it easy to provide validation error messages in apps. The 3.1.3 release takes this functionality a step further for applications that use @angular/material.

Let's say that there is an application where the errors are configured to only show to the users when the form is submitted:

{% gist https://gist.github.com/DmitryEfimenko/56a48eedf7445c0a799e499efb165d47 %}

… and there is a component that uses a FormGroup inside of a template that has an Angular Material input within the form field with corresponding validation error:

{% gist https://gist.github.com/DmitryEfimenko/91b1baa194f6e86a4e09af89811b8dc2 %}

Everything is straightforward, however, if the user types something in the input and clicks away, the input gets highlighted red, but the error message isn’t shown!

![](https://cdn-images-1.medium.com/max/406/1*9kTe-tP3wuSrU0FdNUpdpA.gif)

It makes sense that the error message isn’t shown — the application was configured to show errors only when the form is submitted. So why is input getting highlighted prematurely? Turns out, Material Inputs has its [own logic that determines when inputs are in an error state](https://material.angular.io/components/input/overview#changing-when-error-messages-are-shown). By default, the input enters an error state when it’s “touched”. This is lucky because ngx-errors has the same default, but the moment the developer wants to customize behavior, things get out of sync.

Version 3.1.3 synchronizes the input error state with the errors actually shown to the user. The input will only get highlighted if there are errors shown to the user.

### 🚀Feature 2: Custom error state matchers

The @angular/material team has done a great job designing the API for `errorStateMatcher`. This API has the ability to provide a global errorStateMatcher or override it via an @Input at the <input /> level giving the developer a good amount of flexibility.

@ngspot/ngxerrors also provides flexibility to the developer since it is possible to provide a global configuration for when to show the errors as well as override it at lower levels — in the component templates where ngxErrors and ngxError directives are used. Here’s an example of this functionality from the docs:

{% gist https://gist.github.com/DmitryEfimenko/c37c35691b64062fca427e33cd83a36b %}

The showWhen input would allow the developer to override the default configuration. However, in the previous versions, the developer could only use set values for the showWhen input: 'touched' 'dirty' 'touchedAndDirty' and 'formIsSubmitted' .

With the release of version 3.1.3, the developer can now provide their configurations. [See the docs for more info](https://www.npmjs.com/package/@ngspot/ngx-errors#providing-custom-logic-for-displaying-errors).

### 🚀Feature 3: new config option — showMaxErrors

Finally, the icing on the cake — a new configuration option that enables developers to configure how many total errors are allowed to be displayed per the ngxErrors block at the same time.

Imagine that the form control has multiple validation rules associated with it — a pretty common scenario. Here’s how the template would look:

{% gist https://gist.github.com/DmitryEfimenko/4a375a2ff00c52d0a146edd6ff63ace1 %}

It is possible that these validation rules would be invalid at the same time, in which case, all error messages would be displayed to the user:

![](https://cdn-images-1.medium.com/max/214/1*iO7pF3jNO7VXtAkrP2qMQA.png)

This by itself does not sound like the end of the world. However, it gets ugly real fast if another input is added below the first one:

![](https://cdn-images-1.medium.com/max/211/1*8XYlIdzie8b45A6cb89eVA.png)

The problem has to do with the implementation of the current styles. [There’s been an open issue about it since 2017](https://github.com/angular/components/issues/4580)! And that’s why open source is great— people have been coming up with great workarounds for this issue. A developer could choose any of these or use the showMaxErrors configuration option of @ngspot/ngx-errors to only display one error at a time. This might be a good option if you’d like to ease the cognitive load on your users.

I wish you happy programming!

#### **👏 Special thanks to** [**Ana Boca**](https://github.com/anaboca) **for reviewing, testing, and providing some of the code for this article.**