UNPKG

1.34 kBJavaScriptView Raw
1import { Injectable } from '@angular/core';
2import { FormControl, FormGroup, Validators } from '@angular/forms';
3var FieldService = (function () {
4 function FieldService() {
5 }
6 FieldService.prototype.createFormControl = function (field) {
7 if (field.required) {
8 return new FormControl(field.value, Validators.required);
9 }
10 else {
11 return new FormControl(field.value);
12 }
13 };
14 FieldService.prototype.toFormGroup = function (formFields, subscriptions) {
15 var _this = this;
16 var group = {};
17 formFields.forEach(function (field) {
18 var formControl;
19 if (field.controlType === 'checkbox') {
20 formControl = new FormControl(field.value);
21 }
22 else {
23 formControl = _this.createFormControl(field);
24 }
25 group[field.id] = formControl;
26 if (subscriptions) {
27 subscriptions.push(formControl.valueChanges.subscribe(function (value) { return field.value = value; }));
28 }
29 });
30 return new FormGroup(group);
31 };
32 FieldService.decorators = [
33 { type: Injectable },
34 ];
35 /** @nocollapse */
36 FieldService.ctorParameters = function () { return []; };
37 return FieldService;
38}());
39export { FieldService };