Last active
February 27, 2017 22:12
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
here is the customValidators.ts referenced in line 3 above
https://gist.github.com/Foovanadil/296683d90fe6332642f18afefdfd64b3#file-customvalidators-ts