Skip to content

Commit

Permalink
Exam validate correct choice (fixes #2025) (#2064)
Browse files Browse the repository at this point in the history
  • Loading branch information
yubaraj poudel authored and paulbert committed Sep 20, 2018
1 parent 4a5b25f commit eec5f93
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/app/exams/exams-add.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
});
});
Expand All @@ -140,7 +140,7 @@ export class ExamsAddComponent implements OnInit {
header: '',
body: [ '', Validators.required ],
type: 'input',
correctChoice: ''
correctChoice: [ '', CustomValidators.choiceSelected ]
},
question,
{
Expand Down
5 changes: 3 additions & 2 deletions src/app/exams/exams-question.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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)
}));
}
Expand Down
13 changes: 13 additions & 0 deletions src/app/validators/custom-validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit eec5f93

Please sign in to comment.