diff --git a/src/app/exams/exams-add.component.ts b/src/app/exams/exams-add.component.ts index 3a33f295af..35d26bec25 100644 --- a/src/app/exams/exams-add.component.ts +++ b/src/app/exams/exams-add.component.ts @@ -131,7 +131,7 @@ export class ExamsAddComponent implements OnInit { addQuestion(question: any = { choices: [] }) { const choices = question.choices.map((choice) => { return new FormGroup({ - 'text': new FormControl(choice.text), + 'text': new FormControl(choice.text, Validators.required), 'id': new FormControl(choice.id) }); }); @@ -140,7 +140,7 @@ export class ExamsAddComponent implements OnInit { header: '', body: [ '', Validators.required ], type: 'input', - correctChoice: '' + correctChoice: [ '', CustomValidators.choiceSelected ] }, question, { diff --git a/src/app/exams/exams-question.component.ts b/src/app/exams/exams-question.component.ts index b5131be7d3..9e4ee585f2 100644 --- a/src/app/exams/exams-question.component.ts +++ b/src/app/exams/exams-question.component.ts @@ -2,7 +2,8 @@ import { Component, Input, OnInit, EventEmitter, Output } from '@angular/core'; import { FormGroup, FormControl, - FormArray + FormArray, + Validators } from '@angular/forms'; import { uniqueId } from '../shared/utils'; @@ -42,7 +43,7 @@ export class ExamsQuestionComponent implements OnInit { const newId = uniqueId(); this.correctCheckboxes[newId] = false; this.choices.push(new FormGroup({ - 'text': new FormControl(''), + 'text': new FormControl('', Validators.required), 'id': new FormControl(newId) })); } diff --git a/src/app/validators/custom-validators.ts b/src/app/validators/custom-validators.ts index e6429e0ce4..19ffe061d1 100755 --- a/src/app/validators/custom-validators.ts +++ b/src/app/validators/custom-validators.ts @@ -16,6 +16,19 @@ export class CustomValidators { return (ac.value > 0) ? null : { invalidPositive : true }; } + static choiceSelected(ac: AbstractControl): ValidationErrors { + if (!ac.parent) { + return null; + } + + const inputtype = ac.parent.get('type'); + if (inputtype.value !== 'input' && !ac.value) { + return { required: true }; + } else { + return null; + } + } + static hexValidator(ac: AbstractControl): ValidationErrors { if (!ac.value) {