import { Directive } from '@angular/core';
import {AbstractControl, NG_VALIDATORS, ValidationErrors, Validator, ValidatorFn} from "@angular/forms";

export const checkValidator: ValidatorFn = (control: AbstractControl): ValidationErrors | null => {
  var password = control.get('password');
  var confirmedPassword = control.get('confirmedPassword');
  return password && confirmedPassword && password.value !== confirmedPassword.value ? { notSame: true } : null;
};

@Directive({
  selector: '[appCheckPassword]',
  providers: [{ provide: NG_VALIDATORS, useExisting: CheckPasswordDirective, multi: true }]
})
export class CheckPasswordDirective implements Validator {
  validate(control: AbstractControl): ValidationErrors | null {
    return checkValidator(control);
  }
}

