1.3.1 • Published 11 months ago

ngx-forms-handle-errors v1.3.1

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

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"
}
1.3.1

11 months ago

1.3.0

11 months ago

1.2.2-rc

1 year ago

1.2.1-rc

1 year ago

1.2.0

1 year ago

1.2.2

1 year ago

1.1.0

2 years ago

1.0.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago