npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nationalbankbelgium/ngx-form-errors

v2.0.1

Published

Display validation errors for Angular Reactive forms

Downloads

1,210

Readme

An Angular (6+) library to handle validation messages for Reactive Forms in a simple and centralized way

NPM version npm Build Status Coverage Status Dependency Status devDependency Status taylor swift Commitizen friendly code style: prettier License

NgxFormErrors is heavily inspired in these projects:

  • valdr: a model centric approach to AngularJS form validation
  • ngx-errors: a declarative validation errors library for Angular Reactive Forms
  • ngx-valdemort: consistent validation error messages for Angular Reactive forms

Why NgxFormErrors?

Let's just have a look at the following example:

Plain Reactive Forms approach

<input type="text" formControlName="foo" />

<!-- You need to add an element for each and every error to display a different message -->
<div *ngIf="form.get('foo').hasError('required') && form.get('foo').touched">
	Field is required
</div>
<div *ngIf="form.get('foo').hasError('minlength') && form.get('foo').dirty">
	Min length is 5
</div>
<div *ngIf="form.get('foo').hasError('pattern') && form.get('foo').dirty">
	Field must contain at least one uppercase, one lowercase, and one number
</div>

This easily becomes messy and cumbersome as soon as you have multiple fields. And... it is definitely not DRY :-1:

NgxFormErrors approach

Your component template is cleaner :wink:

<input type="text" formControlName="foo" />
<!--or-->
<input type="text" [formControl]="formGroup.get('foo')" />

<!-- ngxFormErrors creates dynamically an Error component (that you define) displaying all the different validation errors -->
<ng-template ngxFormErrors="foo"></ng-template>

You decide how to display the messages by defining your own Error component :sunglasses:

<!-- Error component's template -->

<!-- you can simply display the message 'as is' -->
<div *ngFor="let error of errors" class="awesome-error-message">
	{{ error.message }}
</div>

<!-- or you can use the error's data/properties to do something fancy -->
<div *ngFor="let error of errors" [ngClass]="getErrorClass(error)">
	{{ constructDisplayedErrorMessage(error) }}
</div>

And the messages are centralized in a service :astonished:

import { FormsModule, ReactiveFormsModule } from "@angular/forms";
import { NgxFormErrorsMessageService, NgxFormErrorsModule } from "@nationalbankbelgium/ngx-form-errors";

@NgModule({
	declarations: [AppComponent, YourCustomErrorComponent],
	imports: [
		FormsModule,
		ReactiveFormsModule,
		NgxFormErrorsModule.forRoot({
			formErrorComponent: YourCustomErrorComponent // your own Error component
		})
	],
	entryComponents: [YourCustomErrorComponent], // add the Error component here so it can be created dynamically
	bootstrap: [AppComponent]
})
export class AppModule {
	public constructor(formErrorsMessageService: NgxFormErrorsMessageService) {
		// add the different validation messages to the NgxFormErrorsMessageService
		formErrorsMessageService.addErrorMessages({
			required: "Field is required",
			minlength: "Min length is 5",
			"fooField.pattern": "Field must contain at least one uppercase, one lowercase, and one number"
		});

		// optionally, add the field names to the NgxFormErrorsMessageService
		// so you can display this name in the validation message instead of the real field name!
		formErrorsMessageService.addFieldNames({
			fooField: "Dummy foo field"
		});
	}
}

Installation

Install NgxFormErrors from npm:

npm install @nationalbankbelgium/ngx-form-errors

NgxFormErrors packaging

NgxFormErrors is built with ng-packagr which means that the final package implements the Angular Package Format providing the following bundles:

  • FESM2015
  • FESM5
  • ESM2015
  • ESM5
  • UMD

So it can be consumed by Angular CLI, Webpack or SystemJS.

Releases

NgxFormErrors releases are available on npm: https://www.npmjs.com/settings/nationalbankbelgium/packages

Contributing

Please follow our contribution guidelines.

To know how to release NgxFormErrors, refer to this page.

Authors

Christopher Cortes

Alexis Georges

License

This project and all associated source code is licensed under the terms of the MIT License.

Documentation

Thank you notes :)

We'd like to thank the following companies who support the open source community with great tools and platforms.

Jetbrains

We're supported by Jetbrains and their awesome support for open source, thanks to which we are able to use the best products on the market to work on this open source project!

GitHub Actions

We're supported by GitHub Actions