UNPKG

ng-bootstrap-form-generator

Version:

Angular 2 + Bootstrap 4 Form Generator. Library provides Angular components that help quickly generate Bootstrap Form from JavaScript object. Component supports validators, help messages, and error messages.

61 lines 2.61 kB
var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; import { FormControl, Validators } from '@angular/forms'; import { ValidationMessage } from './ValidationMessage'; import { BsfControlOptions } from './bsf.options'; export var BsfControl = (function (_super) { __extends(BsfControl, _super); function BsfControl(options) { _super.call(this); this.errors = []; var formState = options.defaultValue; var validator = options.validator; var asyncValidator = options.asyncValidator; this.fc = new FormControl(formState, validator, asyncValidator); this.applyOptions(this.fc, options); this.subcribeToFormChanges(); } BsfControl.prototype.subcribeToFormChanges = function () { var _this = this; var statusChanges$ = this.fc.statusChanges; statusChanges$.subscribe(function (s) { return _this._onStatusChanged(s); }); }; BsfControl.prototype._onStatusChanged = function (status) { if (this.fc.errors === null || this.fc.pristine) { this.errors = []; return; } var vm = this.validationMessage; var fcErrors = this.fc.errors; var self = this; this.errors = Object.keys(fcErrors) .filter(function (k) { return vm.hasOwnProperty(k); }) .map(function (k) { return (typeof vm[k] === 'string' ? vm[k] : vm[k](fcErrors[k], self)); }); }; BsfControl.prototype.applyOptions = function (fc, o) { Object.assign(this, o, { elId: o.elId || o.field }); this.validationMessage = Object.assign({}, ValidationMessage.DEFAULT, o.validationMessage); if (o.disabled) { fc.disable({ onlySelf: true, emitEvent: false }); } var validators = fc.validator ? [fc.validator] : []; if (o.required) { validators.push(Validators.required); } if (o.maxlength) { validators.push(Validators.maxLength(o.maxlength)); } if (o.minlength) { validators.push(Validators.minLength(o.minlength)); } if (o.requiredTrue) { validators.push(Validators.requiredTrue); } fc.setValidators(Validators.compose(validators)); }; return BsfControl; }(BsfControlOptions)); //# sourceMappingURL=bsf.control.js.map