UNPKG

ng-validate-equal

Version:

An Angular directive to validate equality of two template-driven form fields

142 lines (132 loc) 7.23 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/forms'), require('rxjs')) : typeof define === 'function' && define.amd ? define('ng-validate-equal', ['exports', '@angular/core', '@angular/forms', 'rxjs'], factory) : (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['ng-validate-equal'] = {}, global.ng.core, global.ng.forms, global.rxjs)); }(this, (function (exports, i0, forms, rxjs) { 'use strict'; function _interopNamespace(e) { if (e && e.__esModule) return e; var n = Object.create(null); if (e) { Object.keys(e).forEach(function (k) { if (k !== 'default') { var d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } }); } n['default'] = e; return Object.freeze(n); } var i0__namespace = /*#__PURE__*/_interopNamespace(i0); var ValidateEqualDirective = /** @class */ (function () { function ValidateEqualDirective(otherControl) { this.otherControl = otherControl; this.valueChangesSub = new rxjs.Subscription(); } ValidateEqualDirective.prototype.ngOnDestroy = function () { if (this.valueChangesSub) { this.valueChangesSub.unsubscribe(); } }; ValidateEqualDirective.prototype.validate = function (selfControl) { var _this = this; var selfControlValue = selfControl.value; var 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(function (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; }; ValidateEqualDirective.prototype.isEqual = function (val1, val2) { val1 = this.unifyEmptyStrings(val1); val2 = this.unifyEmptyStrings(val2); return val1 === val2; }; ValidateEqualDirective.prototype.unifyEmptyStrings = function (val) { var isDefined = this.isDefined; if (val === null || !isDefined(val)) { val = ''; } return val; }; ValidateEqualDirective.prototype.isDefined = function (value) { var defaultUndefined = void (0); return value !== defaultUndefined; }; return ValidateEqualDirective; }()); ValidateEqualDirective.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualDirective, deps: [{ token: 'ngValidateEqual', attribute: true }], target: i0__namespace.ɵɵFactoryTarget.Directive }); ValidateEqualDirective.ɵdir = i0__namespace.ɵɵngDeclareDirective({ minVersion: "12.0.0", version: "12.0.0", type: ValidateEqualDirective, selector: "[ngValidateEqual]", providers: [ { provide: forms.NG_VALIDATORS, useExisting: i0.forwardRef(function () { return ValidateEqualDirective; }), multi: true } ], ngImport: i0__namespace }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualDirective, decorators: [{ type: i0.Directive, args: [{ selector: '[ngValidateEqual]', providers: [ { provide: forms.NG_VALIDATORS, useExisting: i0.forwardRef(function () { return ValidateEqualDirective; }), multi: true } ] }] }], ctorParameters: function () { return [{ type: undefined, decorators: [{ type: i0.Attribute, args: ['ngValidateEqual'] }] }]; } }); var ValidateEqualModule = /** @class */ (function () { function ValidateEqualModule() { } return ValidateEqualModule; }()); ValidateEqualModule.ɵfac = i0__namespace.ɵɵngDeclareFactory({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualModule, deps: [], target: i0__namespace.ɵɵFactoryTarget.NgModule }); ValidateEqualModule.ɵmod = i0__namespace.ɵɵngDeclareNgModule({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualModule, declarations: [ValidateEqualDirective], exports: [ValidateEqualDirective] }); ValidateEqualModule.ɵinj = i0__namespace.ɵɵngDeclareInjector({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualModule, imports: [[]] }); i0__namespace.ɵɵngDeclareClassMetadata({ minVersion: "12.0.0", version: "12.0.0", ngImport: i0__namespace, type: ValidateEqualModule, decorators: [{ type: i0.NgModule, args: [{ declarations: [ValidateEqualDirective], imports: [], exports: [ValidateEqualDirective] }] }] }); /* * Public API Surface of ng-validate-equal */ /** * Generated bundle index. Do not edit. */ exports.ValidateEqualDirective = ValidateEqualDirective; exports.ValidateEqualModule = ValidateEqualModule; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ng-validate-equal.umd.js.map