UNPKG

@darkobits/formation

Version:
55 lines (44 loc) 2.35 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigurableValidator = undefined; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _constants = require('../../etc/constants'); var _utils = require('../../etc/utils'); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } /** * Allows for the creation of complex validators that may need to access other * controls in the form. The provided function will have a reference to the * Formation form controller, its scope, and the control's ngModel controller * attached to its 'this' value. * * @example * * const myValidator = new ConfigurableValidator(function (modelValue) { * const {form, ngModelCtrl} = this; * * // Do something with modelValue/ngModelCtrl/form. * }); */ var ConfigurableValidator = exports.ConfigurableValidator = function () { function ConfigurableValidator(validator) { _classCallCheck(this, ConfigurableValidator); (0, _utils.assertType)('ConfigurableValidator', [Function, undefined], 'validator', validator); this.validator = validator.bind(this); // Assign the CONFIGURABLE_VALIDATOR flag so that the instance can be // identified as such by Formation. This must be used because instanceof // does not work across execution contexts, this[_constants.CONFIGURABLE_VALIDATOR] = true; } _createClass(ConfigurableValidator, [{ key: 'configure', value: function configure(formationControl) { this.form = formationControl[_constants.FORM_CONTROLLER]; this.scope = this.form.$getScope(); this.ngModelCtrl = formationControl[_constants.NG_MODEL_CTRL]; return this.validator; } }]); return ConfigurableValidator; }();