import { FormGroup } from '@angular/forms';

export function MatchingValidator(passwordName: string, password2Name: string) {

    return (formGroup: FormGroup) => {

        const password = formGroup.controls[passwordName];
        const password2 = formGroup.controls[password2Name];

        if (password2.errors && !password2.errors.NOT_MATCH) {
            return;
        }

        if (password.value !== password2.value) {
            password2.setErrors({ NOT_MATCH: true });
        } else {
            password2.setErrors(null);
        }

    }

}