File

packages/ui/src/lib/io/input-errors/input-errors.component.ts

Description

This component keeps track of a form control's errors and displays them. It is meant to be used beneath a form control.

Metadata

selector ec-input-errors
templateUrl ./input-errors.component.html

Index

Properties
Methods
Inputs

Constructor

constructor(symbol: SymbolService)

Imported error messages.

Parameters :
Name Type Optional
symbol SymbolService No

Inputs

control
Type : FormControl

The form control that should be tracked

Methods

Public getErrors
getErrors()

This method will iterate over the control errors and generate objects for the template.

Returns : any

Properties

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>
Legend
Html element
Component
Html element with directive

result-matching ""

    No results matching ""