File

src/app/_helpers/custom-error-state-matcher.ts

Description

Custom provider that defines how form controls behave with regards to displaying error messages.

Implements

ErrorStateMatcher

Index

Methods

Methods

isErrorState
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null)

Checks whether an invalid input has been made and an error should be made.

Parameters :
Name Type Optional Description
control FormControl | null No
  • Tracks the value and validation status of an individual form control.
form FormGroupDirective | NgForm | null No
  • Binding of an existing FormGroup to a DOM element.
Returns : boolean

true - If an invalid input has been made to the form control.

import { ErrorStateMatcher } from '@angular/material/core';
import { FormControl, FormGroupDirective, NgForm } from '@angular/forms';

/**
 * Custom provider that defines how form controls behave with regards to displaying error messages.
 *
 */
export class CustomErrorStateMatcher implements ErrorStateMatcher {
  /**
   * Checks whether an invalid input has been made and an error should be made.
   *
   * @param control - Tracks the value and validation status of an individual form control.
   * @param form - Binding of an existing FormGroup to a DOM element.
   * @returns true - If an invalid input has been made to the form control.
   */
  isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
    const isSubmitted: boolean = form && form.submitted;
    return !!(control && control.invalid && (control.dirty || control.touched || isSubmitted));
  }
}

result-matching ""

    No results matching ""