An open API service indexing awesome lists of open source software.

https://github.com/venipa/ngx-formcontrol-errors-async


https://github.com/venipa/ngx-formcontrol-errors-async

Last synced: 2 months ago
JSON representation

Awesome Lists containing this project

README

        

# AsyncFormControlErrors

Handle your form control errors like a champ.
Async is the way!!!

# Example

We recommend using [@angular/material](https://material.angular.io/) for using this Library but its optional

```typescript
import { Component, OnInit } from "@angular/core";
import { FormGroup, FormControl, Validators } from "@angular/forms";
import {
prepareFormErrorObservables,
useFormErrorObservable
} from "ngx-formcontrol-errors-async";

@prepareFormErrorObservables()
@Component({
selector: "app-test-async-controls",
template: `




{{ error.content | translate }}





{{ error.content | translate }}



`,
})
export class TestAsyncControlsComponent implements OnInit {
group = new FormGroup({
name: new FormControl("", [Validators.required, Validators.minLength(3)]),
description: new FormControl("", [
Validators.required,
Validators.maxLength(255),
]),
});
constructor() {}

ngOnInit(): void {}
getErrorObservable(controlName: string) {
return useFormErrorObservable(this)(
controlName,
() => this.group.controls[controlName],
{
required: (error, ctrl) => ({
content: `The ${controlName} field is required`,
}),
minLength: (error, ctrl) => ({
content: `The ${controlName} must be greater than or equal ${error.requiredLength} characters.`,
}),
maxLength: (error, ctrl) => ({
content: `The ${controlName} must be less than ${error.requiredLength} characters`,
}),
}
);
}
}
```