Created
September 3, 2019 06:06
-
-
Save anantl05/a2fbe7f0011455b9c34224a3dd1e6e8e to your computer and use it in GitHub Desktop.
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 { Validator, AbstractControl, NG_VALIDATORS, ValidatorFn } from '@angular/forms'; | |
import { Directive, Input } from "@angular/core"; | |
export function equalPassValidator(appEqualValidator: string): ValidatorFn { | |
return (c: AbstractControl): { [key: string]: any | null } => { | |
console.log("control object: ", c, appEqualValidator) | |
const controlToCompare = c.root.get(appEqualValidator); | |
console.log('control to compare: ', controlToCompare); | |
if (controlToCompare && controlToCompare.value === c.value) return { "equal": true }; | |
return { "equal": false } | |
} | |
} | |
@Directive({ | |
selector: "[appEqualValidator]", | |
providers: [{ | |
provide: NG_VALIDATORS, | |
useExisting: EqualValidatorDirective, | |
multi: true | |
}] | |
}) | |
export class EqualValidatorDirective implements Validator { | |
@Input() appEqualValidator: string; | |
validate(c: AbstractControl): { [key: string]: any | null } { | |
return this.appEqualValidator ? equalPassValidator(this.appEqualValidator)(c) : null; | |
} | |
registerOnValidatorChange(fn: () => void): void { | |
// throw new Error("Method not implemented"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment