Skip to content

Instantly share code, notes, and snippets.

@nicobytes
Last active June 14, 2021 21:18
Show Gist options
  • Save nicobytes/12eba093a5f3392ee8772bc70c6cb512 to your computer and use it in GitHub Desktop.
Save nicobytes/12eba093a5f3392ee8772bc70c6cb512 to your computer and use it in GitHub Desktop.
Custom validations in Angular
import { AbstractControl } from '@angular/forms';

export class MyValidations {

  static age(control: AbstractControl) {
    const value = control.value;
    if (value < 18) {
      return {isYoung: true};
    }
    return null;
  }

  static ageWithParam(max: number) {
    return (control: AbstractControl) => {
      const value = control.value;
      if (value < max) {
        return {isYoung: true};
      }
      return null;
    };
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment