ng-validate-equal
Version:
An Angular directive to validate equality of two template-driven form fields
107 lines (101 loc) • 4.9 kB
JavaScript
import * as i0 from '@angular/core';
import { forwardRef, Directive, Attribute, NgModule } from '@angular/core';
import { NG_VALIDATORS } from '@angular/forms';
import { Subscription } from 'rxjs';
class ValidateEqualDirective {
constructor(otherControl) {
this.otherControl = otherControl;
this.valueChangesSub = new Subscription();
}
ngOnDestroy() {
if (this.valueChangesSub) {
this.valueChangesSub.unsubscribe();
}
}
validate(selfControl) {
const selfControlValue = selfControl.value;
const otherControl = selfControl.root.get(this.otherControl);
if (!otherControl) {
throw new Error('ng-validate-equal: "otherControl" is not defined, please pass the name of the main field to the directive put on the secondary field like this example: ngValidateEqual="passwordFieldName"');
}
if (this.valueChangesSub) {
this.valueChangesSub.unsubscribe();
}
// this code works when user types in other control putting the error in self control
this.valueChangesSub = otherControl.valueChanges.subscribe(otherControlValue => {
if (!this.isEqual(selfControlValue, otherControlValue) && (selfControl.touched || selfControl.dirty)) {
selfControl.setErrors({
notEqual: true
});
}
else {
if (selfControl.errors && selfControl.hasError('notEqual')) {
delete selfControl.errors['notEqual'];
if (!Object.keys(selfControl.errors).length) {
selfControl.setErrors(null);
}
}
}
});
// this code works when the user types in self control putting the error in self control
if (!this.isEqual(selfControlValue, otherControl.value) && (selfControl.touched || selfControl.dirty)) {
return {
notEqual: true
};
}
return null;
}
isEqual(val1, val2) {
val1 = this.unifyEmptyStrings(val1);
val2 = this.unifyEmptyStrings(val2);
return val1 === val2;
}
unifyEmptyStrings(val) {
const isDefined = this.isDefined;
if (val === null || !isDefined(val)) {
val = '';
}
return val;
}
isDefined(value) {
const defaultUndefined = void (0);
return value !== defaultUndefined;
}
}
ValidateEqualDirective.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualDirective, deps: [{ token: 'ngValidateEqual', attribute: true }], target: i0.ɵɵFactoryTarget.Directive });
ValidateEqualDirective.ɵdir = i0.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.0", type: ValidateEqualDirective, selector: "[ngValidateEqual]", providers: [
{ provide: NG_VALIDATORS, useExisting: forwardRef(() => ValidateEqualDirective), multi: true }
], ngImport: i0 });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualDirective, decorators: [{
type: Directive,
args: [{
selector: '[ngValidateEqual]',
providers: [
{ provide: NG_VALIDATORS, useExisting: forwardRef(() => ValidateEqualDirective), multi: true }
]
}]
}], ctorParameters: function () { return [{ type: undefined, decorators: [{
type: Attribute,
args: ['ngValidateEqual']
}] }]; } });
class ValidateEqualModule {
}
ValidateEqualModule.ɵfac = i0.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualModule, deps: [], target: i0.ɵɵFactoryTarget.NgModule });
ValidateEqualModule.ɵmod = i0.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualModule, declarations: [ValidateEqualDirective], exports: [ValidateEqualDirective] });
ValidateEqualModule.ɵinj = i0.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualModule, imports: [[]] });
i0.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0, type: ValidateEqualModule, decorators: [{
type: NgModule,
args: [{
declarations: [ValidateEqualDirective],
imports: [],
exports: [ValidateEqualDirective]
}]
}] });
/*
* Public API Surface of ng-validate-equal
*/
/**
* Generated bundle index. Do not edit.
*/
export { ValidateEqualDirective, ValidateEqualModule };
//# sourceMappingURL=ng-validate-equal.js.map