ngx-forms-handle-errors
TypeScript icon, indicating that this package has built-in type declarations

1.6.2 • Public • Published

NgxFormsHandleErrors

This module is going to bind the errors from an object to your form inputs.

npm

Demo.

Getting Started

Install with npm:

npm i ngx-forms-handle-errors

Import and Use

import { updateFormErrors } from 'ngx-forms-handle-errors';
// Example of errors returned from an API
const errors = {
  "fields": {
    "username": [
      "Username has already been taken"
    ],
    "email": [
      "E-mail address already exists"
    ]
  }
}

export class AppComponent {
  item: any = {};

  async save(form: NgForm) {
    if (form.valid) {
      // Simulating API request
      setTimeout(() => {
        // Binding API returned errors
        updateFormErrors(form, errors);
      }, 2000);
    }
  }
}
<form #form="ngForm" novalidate (submit)="save(form)">
  <div class="control">
    <input type="text" #username="ngModel" [(ngModel)]="item.username" name="username">
    <pre>{{ username?.errors?.custom }}</pre>
  </div>
  
  <div class="control">
    <input type="email" #email="ngModel" [(ngModel)]="item.email" name="email">
    <pre>{{ email?.errors?.custom }}</pre>
  </div>
</form>

Then, you'll have the following structure:

{
  custom: "Username has already been taken"
}

Package Sidebar

Install

npm i ngx-forms-handle-errors

Weekly Downloads

15

Version

1.6.2

License

MIT

Unpacked Size

4.4 kB

Total Files

7

Last publish

Collaborators

  • clmeida
  • nerpatricia