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