ngx-forms-builder
Version:
A small library that adds validation with decorators and build angular forms.
257 lines (247 loc) • 7.82 kB
JavaScript
import { __values } from 'tslib';
import 'reflect-metadata';
import { Injectable, NgModule } from '@angular/core';
import { FormGroup, FormControl, Validators } from '@angular/forms';
/**
* @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 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 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 Validators.required; }),
email: (/**
* @param {?} _
* @return {?}
*/
function (_) { return Validators.email; }),
min: (/**
* @param {?} min
* @return {?}
*/
function (min) { return Validators.min(min); }),
max: (/**
* @param {?} max
* @return {?}
*/
function (max) { return Validators.max(max); }),
pattern: (/**
* @param {?} pattern
* @return {?}
*/
function (pattern) { return 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: 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: 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
*/
export { NgxFormsBuilderModule, ModelFormBuilder, createValidationDecorator, createValidationDecoratorWithValue, Required, Email, Exclude, Min, Max, Pattern, CustomValidator, ModelFormBuilder as ɵa };
//# sourceMappingURL=ngx-forms-builder.js.map