import { Directive , forwardRef , Attribute } from '@angular/core' ; import { Validator , AbstractControl , NG_VALIDATORS } from '@angular/forms' ; @ Directive ( { selector : '[mdb-validateEqual][formControlName],[validateEqual][formControl],[validateEqual][ngModel]' , providers : [ { provide : NG_VALIDATORS , useExisting : forwardRef ( ( ) => EqualValidatorDirective ) , multi : true } ] } ) export class EqualValidatorDirective implements Validator { constructor ( @ Attribute ( 'validateEqual' ) public validateEqual , @ Attribute ( 'reverse' ) public reverse ) { } private get isReverse ( ) { if ( ! this . reverse ) { return false ; } return this . reverse === 'true' ? true : false ; } validate ( c ) { const setToNullValue = null ; const v = c . value ; const e = c . root . get ( this . validateEqual ) ; if ( e && v !== e . value ) { return { validateEqual : false } ; } if ( e && v === e . value && this . isReverse ) { delete e . errors [ 'validateEqual' ] ; if ( ! Object . keys ( e . errors ) . length ) { e . setErrors ( null ) ; } } if ( e && v !== e . value && this . isReverse ) { e . setErrors ( { validateEqual : false } ) ; } return setToNullValue ; } }