UNPKG

ng2-validation-manager

Version:

ng2-validation-manager is validation library for Angular 2 based on Laravel Validation method.

310 lines 13.3 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var forms_1 = require("@angular/forms"); var validators_1 = require("./validators"); var ValidationManager = (function () { function ValidationManager(formValidations, displayError) { if (displayError === void 0) { displayError = ['invalid', 'dirty', 'submitted']; } var _this = this; this.displayError = displayError; this.controls = {}; this.errors = {}; this.submitted = false; this.children = {}; this.formGroup = new forms_1.FormGroup({}); this._fb = new forms_1.FormBuilder(); for (var key in formValidations) { if (typeof formValidations[key] == 'string') { this.controls[key] = this.buildControl(key, formValidations[key]); } else if (formValidations[key] instanceof ValidationManager) { this.children[key] = formValidations[key]; this.controls[key] = { control: formValidations[key].getForm(), messages: {} }; } else if (formValidations[key] instanceof Array) { this.children[key] = []; var formArray = this._fb.array([]); for (var _i = 0, _a = formValidations[key]; _i < _a.length; _i++) { var group = _a[_i]; if (group instanceof ValidationManager) { formArray.push(group.getForm()); this.children[key].push(group); } else formArray.push(new forms_1.FormControl(group)); } this.controls[key] = { control: formArray, messages: {} }; } else if (typeof formValidations[key] == 'object') { if (!formValidations[key].value) formValidations[key].value = ''; this.controls[key] = this.buildControl(key, formValidations[key].rules, formValidations[key].value); } this.formGroup.addControl(key, this.controls[key].control); this.errors[key] = ''; } this.formGroup.valueChanges.subscribe(function (data) { return _this.onValueChanged(); }); } ValidationManager.prototype.getForm = function () { return this.formGroup; }; ValidationManager.prototype.getChildGroup = function (field, index) { if (index === void 0) { index = null; } if (index !== null) return this.children[field][index]; return this.children[field]; }; ValidationManager.prototype.getChildren = function (field) { return this.children[field]; }; ValidationManager.prototype.addChildGroup = function (field, mgr) { if (this.formGroup.controls[field] && this.formGroup.controls[field] instanceof forms_1.FormArray) { var control = this.formGroup.controls[field]; if (mgr instanceof ValidationManager) { control.push(mgr.getForm()); this.children[field].push(mgr); } else control.push(new forms_1.FormControl(mgr)); return control.length - 1; } else { this.children[field] = mgr; this.formGroup.addControl(field, mgr.getForm()); return -1; } }; ValidationManager.prototype.removeChildGroup = function (field, index) { if (index === void 0) { index = null; } if (!this.formGroup.controls[field]) { return; } if (index !== null) { var control = this.formGroup.controls[field]; control.removeAt(index); this.children[field].splice(index, 1); } else { this.formGroup.removeControl(field); delete this.children[field]; } }; ValidationManager.prototype.isValid = function () { this.submitted = true; this.__setOnChild('submitted', true); this.onValueChanged(); return !this.formGroup.invalid; }; ValidationManager.prototype.hasError = function (field) { return this.errors[field] ? true : false; }; ValidationManager.prototype.getError = function (field) { return this.errors[field]; }; ValidationManager.prototype.getErrors = function () { for (var child in this.children) { if (this.children[child] instanceof Array) { this.errors[child] = {}; for (var subChild in this.children[child]) this.errors[child][subChild] = this.children[child][subChild].errors; } else this.errors[child] = this.children[child].errors; } return this.errors; }; ValidationManager.prototype.reset = function () { this.submitted = false; this.formGroup.reset(); this.__setOnChild('submitted', false); for (var fld in this.children) { for (var _i = 0, _a = this.children[fld]; _i < _a.length; _i++) { var child = _a[_i]; child.formGroup.reset(); } } }; ValidationManager.prototype.onValueChanged = function (displayError) { if (displayError === void 0) { displayError = null; } if (!this.formGroup) { return; } var form = this.formGroup; var _loop_1 = function (field) { var control = form.get(field); this_1.errors[field] = ''; if (displayError == null) displayError = this_1.displayError; if (control && displayError.length && (displayError.every(function (element) { return (element == "submitted") ? true : control[element]; }) || this_1.submitted)) { for (var rule in control.errors) { this_1.errors[field] = this_1.getErrorMessage(field, rule); } } }; var this_1 = this; for (var field in this.errors) { _loop_1(field); } this.__callOnChild('onValueChanged'); }; ValidationManager.prototype.setValue = function (values, value) { if (value === void 0) { value = null; } console.log(typeof values, values); if (typeof values === "string") { var control = this.formGroup.get(values); if (!control || control instanceof forms_1.FormArray) { return; } if (value !== null) { this.formGroup.get(values).setValue(value.toString()); this.formGroup.get(values).markAsTouched(); this.formGroup.get(values).markAsDirty(); } } if (typeof values === "object") { for (var key in values) { if (this.formGroup.get(key)) { this.setValue(key, values[key]); } } } }; ValidationManager.prototype.getValue = function (controlKey) { return this.formGroup.value[controlKey]; }; ValidationManager.prototype.getData = function () { return this.formGroup.value; }; ValidationManager.prototype.getControl = function (controlName) { if (!this.formGroup.controls[controlName]) return; return this.formGroup.controls[controlName]; }; ValidationManager.prototype.buildControl = function (name, rules, value) { var _this = this; if (value === void 0) { value = null; } var controlRules = []; var messages = {}; rules = rules.replace(/pattern:(\/.+\/)(\|?)/, function (a, b, c) { return 'pattern:' + btoa(b) + c; }); rules.split('|').forEach(function (rule) { if (rule) { var rule_spilted = rule.split(':'); var rule_name = rule_spilted[0]; var rule_vars = []; if (rule_spilted[1]) rule_vars = rule_spilted[1].split(','); if (!validators_1.Validators[rule_name]) throw new TypeError('Validation rule [' + rule_name + '] does not exists.'); if (rule_vars.length > 1) controlRules.push(validators_1.Validators[rule_name](rule_vars)); else if (rule_vars.length == 1) { if (rule_name == 'pattern' && isBase64(rule_vars[0])) rule_vars[0] = atob(rule_vars[0]).slice(1, -1); controlRules.push(validators_1.Validators[rule_name](rule_vars[0])); } else controlRules.push(validators_1.Validators[rule_name]); messages[rule_name.toLowerCase()] = _this.buildMessage(name, rule_name, rule_vars); } }); var formControl = new forms_1.FormControl(value, controlRules); return { control: formControl, messages: messages }; }; ValidationManager.prototype.getErrorMessage = function (field, rule) { if (!this.controls[field].messages[rule.toLowerCase()]) throw Error('Message not found inside the control:' + field + ' message:' + rule.toLowerCase()); return this.controls[field].messages[rule.toLowerCase()]; }; ValidationManager.prototype.setErrorMessage = function (field, rule, message) { if (this.controls[field].messages[rule.toLowerCase()]) this.controls[field].messages[rule.toLowerCase()] = message; }; ValidationManager.prototype.buildMessage = function (name, rule, arg) { if (arg === void 0) { arg = []; } if (!this.getMessage(rule)) throw Error('Validation message is missing for: ' + rule); var message = this.getMessage(rule); message = message.replace(/%n/g, ucFirst(name)).replace(/_/g, ' '); if (arg.length) { arg.forEach(function (arg, key) { message = message.replace('%' + key, arg); }); } return message; }; ValidationManager.prototype.getMessage = function (rule) { return exports.VALIDATION_MESSAGES[rule.toLowerCase()]; }; ValidationManager.prototype.__callOnChild = function (funct) { for (var fld in this.children) { if (this.children[fld] instanceof Array) { for (var _i = 0, _a = this.children[fld]; _i < _a.length; _i++) { var child = _a[_i]; child[funct].apply(child, Array.prototype.slice.call(arguments, 1)); } } else { this.children[fld][funct].apply(this.children[fld], Array.prototype.slice.call(arguments, 1)); } } }; ValidationManager.prototype.__setOnChild = function (field, value) { for (var fld in this.children) { if (this.children[fld] instanceof Array) { for (var _i = 0, _a = this.children[fld]; _i < _a.length; _i++) { var child = _a[_i]; child[field] = value; } } else { this.children[fld][field] = value; } } }; return ValidationManager; }()); exports.ValidationManager = ValidationManager; exports.VALIDATION_MESSAGES = { 'required': '%n is required', 'minlength': '%n must be at least %0 characters long.', 'maxlength': '%n cannot be more than %0 characters long.', 'alpha': '%n accepts only alphabetic characters.', 'alphaspace': '%n accepts only alphabetic characters and space.', 'alphanum': '%n accepts only alphabetic characters and numbers.', 'alphanumspace': '%n accepts only alphabetic characters, numbers and space.', 'url': '%n is not valid url.', 'number': '%n is not valid number.', 'digits': '%n is not valid number.', 'creditcard': '%n is not valid credit card.', 'range': '%n must be between %0 and %1.', 'rangelength': '%n must be between %0 and %1.', 'max': '%n must be equal or lower then %0', 'min': '%n must be equal or higher then %0', 'email': '%n is not valid email.', 'date': '%n is not valid date.', 'mindate': 'The minimum date allowed in %n is %0', 'maxdate': 'The maximum date allowed in %n is %0', 'dateiso': '%n is not valid ISO date[yyyy-mm-dd].', 'equal': '%n should be equal to %0', 'equalto': '%n must be equal to %0', 'json': '%n is not valid json.', 'pattern': '%n does not match the pattern.', 'count': '%n must count %0' }; function ucFirst(str) { var firstLetter = str.substr(0, 1); return firstLetter.toUpperCase() + str.substr(1); } function isBase64(str) { try { return btoa(atob(str)) == str; } catch (err) { return false; } } //# sourceMappingURL=validation-manager.js.map