Created
November 5, 2019 21:40
-
-
Save lydemann/791b0474c27ea16c9e6c3490709fb929 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export class QuestionFormGeneratorService { | |
constructor() {} | |
getQuestionsFormGroup(quesitons: Question[]): FormGroup { | |
const group: { | |
[key: string]: AbstractControl | |
} = {}; | |
quesitons.forEach(question => { | |
const validatiors = this.getValidationFunctions(question.validationRules); | |
group[question.externalQuestionId] = question.answerType.toUpperCase() === selectMulti ? new FormGroup(this.getAnswerOptionsControlsObj(question.answerOptions), validatiors) : new FormControl(question.answer || '', validatiors); | |
}); | |
return new FormGroup(group); | |
} | |
private getAnswerOptionsControlsObj(answerOptions: AnswerOption[]) { | |
const group: { | |
[key: string]: AbstractControl | |
} = {}; | |
answerOptions.forEach(answerOption => group[answerOption.optionCode] = new FormControl()); | |
return group; | |
} | |
private getValidationFunctions(validationRuleStrings: string[]): Array < (control: AbstractControl) => ValidationErrors | null > | null { | |
const validationRules = []; | |
for (const validationRuleStr of validationRuleStrings) { | |
const validationRule = ValidationRule.validationRulesMap.get(validationRuleStr.toUpperCase()); | |
if (validationRule !== undefined) { | |
validationRules.push(validationRule.validationFn); | |
} | |
} | |
return validationRules.length > 0 ? validationRules : null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment