@nationalbankbelgium/ngx-form-errors
v2.0.1
Published
Display validation errors for Angular Reactive forms
Downloads
1,145
Readme
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
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