UNPKG

ngx-forms-builder

Version:

A small library that adds validation with decorators and build angular forms.

292 lines (277 loc) 11 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('reflect-metadata'), require('@angular/core'), require('@angular/forms')) : typeof define === 'function' && define.amd ? define('ngx-forms-builder', ['exports', 'reflect-metadata', '@angular/core', '@angular/forms'], factory) : (factory((global['ngx-forms-builder'] = {}),null,global.ng.core,global.ng.forms)); }(this, (function (exports,reflectMetadata,core,forms) { 'use strict'; /*! ***************************************************************************** Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT. See the Apache Version 2.0 License for specific language governing permissions and limitations under the License. ***************************************************************************** */ function __values(o) { var m = typeof Symbol === "function" && o[Symbol.iterator], i = 0; if (m) return m.call(o); return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; } /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @template T */ var ModelFormBuilder = /** @class */ (function () { function ModelFormBuilder() { } /** * @param {?} target * @param {?=} options * @return {?} */ ModelFormBuilder.prototype.build = /** * @param {?} target * @param {?=} options * @return {?} */ function (target, options) { var e_1, _a; var _this = this; /** @type {?} */ var fg = new forms.FormGroup({}, options); var _loop_1 = function (propertyKey) { /** @type {?} */ var decorators = Reflect.getMetadataKeys(target, propertyKey); if (decorators.indexOf('exclude') > 0) { return "continue"; } fg.addControl(propertyKey, new forms.FormControl(target[propertyKey])); /** @type {?} */ var validators = []; decorators .filter(( /** * @param {?} x * @return {?} */function (x) { return x !== 'design:type'; })) .forEach(( /** * @param {?} decorator * @return {?} */function (decorator) { /** @type {?} */ var validator = _this.getValidatorByType(decorator, target, propertyKey); if (validator !== null) { validators.push(validator); } })); fg.get(propertyKey).setValidators(validators); }; try { for (var _b = __values(Object.keys(target)), _c = _b.next(); !_c.done; _c = _b.next()) { var propertyKey = _c.value; _loop_1(propertyKey); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (_c && !_c.done && (_a = _b.return)) _a.call(_b); } finally { if (e_1) throw e_1.error; } } return fg; }; /** * @param {?} metadataKey * @param {?} target * @param {?} propertyKey * @return {?} */ ModelFormBuilder.prototype.getValidatorByType = /** * @param {?} metadataKey * @param {?} target * @param {?} propertyKey * @return {?} */ function (metadataKey, target, propertyKey) { /** @type {?} */ var validatorsType = { required: ( /** * @param {?} _ * @return {?} */function (_) { return forms.Validators.required; }), email: ( /** * @param {?} _ * @return {?} */function (_) { return forms.Validators.email; }), min: ( /** * @param {?} min * @return {?} */function (min) { return forms.Validators.min(min); }), max: ( /** * @param {?} max * @return {?} */function (max) { return forms.Validators.max(max); }), pattern: ( /** * @param {?} pattern * @return {?} */function (pattern) { return forms.Validators.pattern(pattern); }), customValidator: ( /** * @param {?} custom * @return {?} */function (custom) { return custom; }) }; /** @type {?} */ var metadataValue = Reflect.getMetadata(metadataKey, target, propertyKey); return validatorsType[metadataKey](metadataValue); }; ModelFormBuilder.decorators = [ { type: core.Injectable } ]; return ModelFormBuilder; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ var NgxFormsBuilderModule = /** @class */ (function () { function NgxFormsBuilderModule() { } /** * @return {?} */ NgxFormsBuilderModule.forRoot = /** * @return {?} */ function () { return { ngModule: NgxFormsBuilderModule, providers: [ModelFormBuilder] }; }; NgxFormsBuilderModule.decorators = [ { type: core.NgModule, args: [{ declarations: [], imports: [], exports: [] },] } ]; return NgxFormsBuilderModule; }()); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @enum {string} */ var Validations = { REQUIRED: 'required', EMAIL: 'email', MIN: 'min', MAX: 'max', PATTERN: 'pattern', CUSTOMVALIDATOR: 'customValidator', EXCLUDE: 'exclude', }; /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** @type {?} */ var createValidationDecorator = ( /** * @param {?} validation * @return {?} */function (validation) { return ( /** * @return {?} */function () { return ( /** * @param {?} target * @param {?} propertyKey * @return {?} */function (target, propertyKey) { Reflect.defineMetadata(String(validation), true, target, propertyKey); }); }); }); /** @type {?} */ var createValidationDecoratorWithValue = ( /** * @template T * @param {?} validation * @return {?} */function (validation) { return ( /** * @param {?=} value * @return {?} */function (value) { return ( /** * @param {?} target * @param {?} propertyKey * @return {?} */function (target, propertyKey) { Reflect.defineMetadata(String(validation), value, target, propertyKey); }); }); }); /** @type {?} */ var Required = createValidationDecorator(Validations.REQUIRED); /** @type {?} */ var Email = createValidationDecorator(Validations.EMAIL); /** @type {?} */ var Exclude = createValidationDecorator(Validations.EXCLUDE); /** @type {?} */ var Min = createValidationDecoratorWithValue(Validations.MIN); /** @type {?} */ var Max = createValidationDecoratorWithValue(Validations.MAX); /** @type {?} */ var Pattern = createValidationDecoratorWithValue(Validations.PATTERN); /** @type {?} */ var CustomValidator = createValidationDecoratorWithValue(Validations.CUSTOMVALIDATOR); /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ /** * @fileoverview added by tsickle * @suppress {checkTypes,constantProperty,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc */ exports.NgxFormsBuilderModule = NgxFormsBuilderModule; exports.ModelFormBuilder = ModelFormBuilder; exports.createValidationDecorator = createValidationDecorator; exports.createValidationDecoratorWithValue = createValidationDecoratorWithValue; exports.Required = Required; exports.Email = Email; exports.Exclude = Exclude; exports.Min = Min; exports.Max = Max; exports.Pattern = Pattern; exports.CustomValidator = CustomValidator; exports.ɵa = ModelFormBuilder; Object.defineProperty(exports, '__esModule', { value: true }); }))); //# sourceMappingURL=ngx-forms-builder.umd.js.map