Skip to content

Instantly share code, notes, and snippets.

@Foovanadil
Last active February 27, 2017 22:12
Show Gist options
  • Save Foovanadil/7b6c171daeda0a795d55c59a2dc4860c to your computer and use it in GitHub Desktop.
Save Foovanadil/7b6c171daeda0a795d55c59a2dc4860c to your computer and use it in GitHub Desktop.
Min Max validator directives for Angular 2 (since they don't exist currently in angular 2)
import { Directive, forwardRef, Attribute, Injectable, ElementRef, Input } from '@angular/core';
import { NG_VALIDATORS, Validator, AbstractControl, FormControl } from '@angular/forms';
import { CustomValidators } from './customValidators';
@Directive({
selector: '[min][formControlName],[min][formControl],[min][ngModel]',
providers: [{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => MinValidator),
multi: true
}]
})
export class MinValidator implements Validator {
constructor(private validators: CustomValidators) {
}
@Input() min : number;
validate(control: AbstractControl) {
return this.validators.min(control, this.min);
}
}
@Directive({
selector: '[max][formControlName],[max][formControl],[max][ngModel]',
providers: [{
provide: NG_VALIDATORS,
useExisting: forwardRef(() => MaxValidator),
multi: true
}]
})
export class MaxValidator implements Validator {
constructor(private validators: CustomValidators) {
}
@Input() max : number;
validate(control: AbstractControl) {
return this.validators.max(control, this.max);
}
}
@Foovanadil
Copy link
Author

Foovanadil commented Feb 27, 2017

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment