2016-03-05 17:53:34 -05:00
|
|
|
// #docregion
|
2016-05-03 14:06:32 +02:00
|
|
|
import { Injectable } from '@angular/core';
|
2016-06-13 00:41:33 +02:00
|
|
|
import { FormBuilder, Validators } from '@angular/common';
|
2016-05-03 14:06:32 +02:00
|
|
|
import { QuestionBase } from './question-base';
|
2016-03-05 17:53:34 -05:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class QuestionControlService {
|
2016-06-08 01:06:25 +02:00
|
|
|
constructor(private fb: FormBuilder) { }
|
2016-03-05 17:53:34 -05:00
|
|
|
|
2016-06-08 01:06:25 +02:00
|
|
|
toControlGroup(questions: QuestionBase<any>[] ) {
|
2016-03-05 17:53:34 -05:00
|
|
|
let group = {};
|
|
|
|
|
|
|
|
questions.forEach(question => {
|
2016-04-15 20:34:18 +01:00
|
|
|
group[question.key] = question.required ? [question.value || '', Validators.required] : [question.value || ''];
|
2016-03-05 17:53:34 -05:00
|
|
|
});
|
2016-05-03 14:06:32 +02:00
|
|
|
return this.fb.group(group);
|
2016-03-05 17:53:34 -05:00
|
|
|
}
|
|
|
|
}
|