packages/ui/src/lib/io/input-errors/input-errors.component.ts
This component keeps track of a form control's errors and displays them. It is meant to be used beneath a form control.
selector | ec-input-errors |
templateUrl | ./input-errors.component.html |
Properties |
|
Methods |
|
Inputs |
constructor(symbol: SymbolService)
|
||||||
Imported error messages.
Parameters :
|
control | |
Type : FormControl
|
|
The form control that should be tracked |
Public getErrors |
getErrors()
|
This method will iterate over the control errors and generate objects for the template.
Returns :
any
|
Public symbol |
Type : SymbolService
|
import { Component, Input } from '@angular/core';
import { FormControl } from '@angular/forms';
import { SymbolService } from '../../symbol/symbol.service';
/** This component keeps track of a form control's errors and displays them. It is meant to be used beneath a form control. */
@Component({
selector: 'ec-input-errors',
templateUrl: './input-errors.component.html',
})
export class InputErrorsComponent {
/** The form control that should be tracked */
@Input() control: FormControl;
/** Imported error messages. */
constructor(public symbol: SymbolService) {}
/** This method will iterate over the control errors and generate objects for the template. */
public getErrors() {
return Object.keys(this.control.errors).reduce((errs, key) => {
let message;
if (key === 'custom') {
message = this.control.errors[key];
} else {
message = this.symbol.resolve('error.input.' + key) || this.symbol.resolve('error.input.invalid');
}
errs.push({
key: key,
error: this.control.errors[key],
message,
});
return errs;
}, []);
}
}
<ul *ngIf="control?.errors&&control.touched" class="ec-input-errors">
<li *ngFor="let error of getErrors()" class="ec-input-error">{{error.message}}</li>
</ul>