An Angular (6+) library to handle validation messages for Reactive Forms in a simple and centralized way
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
Let's just have a look at the following example:
<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 👎
Your component template is cleaner 😉
<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 😎
<!-- 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 😲
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"
});
}
}
Install NgxFormErrors from npm:
npm install @nationalbankbelgium/ngx-form-errors
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.
NgxFormErrors releases are available on npm: https://www.npmjs.com/settings/nationalbankbelgium/packages
Please follow our contribution guidelines.
To know how to release NgxFormErrors, refer to this page.
This project and all associated source code is licensed under the terms of the MIT License.
We'd like to thank the following companies who support the open source community with great tools and platforms.
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!
We're supported by GitHub Actions