Skip to content

Latest commit

 

History

History
38 lines (29 loc) · 981 Bytes

angular.md

File metadata and controls

38 lines (29 loc) · 981 Bytes

API Design

Table of Contents

Reference

Custom Validators

Method 1: Export an initializer function that will return the actual validator. Used this to create a generic method that returns actual validator depending on input parms.

export function onlyAllowThis(letter: string): ValidatorFn {
  return (control: AbstractControl): { [key: string]: any } | null => {
    if (control.value !== letter) {
      return { NOT_THIS: `This is not ${letter}!` };
    }
  };
}

Method 1: Export the validator directly

export const onlyAllowA = (
  control: AbstractControl
): { [key: string]: any } | null => {
  if (control.value !== "A") {
    return { NOT_A: "This is not A!" };
  }
};

Stackblitz example: here