Skip to content

Commit 05552d1

Browse files
committed
fix: extra questions add validation for answer setting
Change-Id: I4367bc5f9a8dc702e725d165dc317cdfc155fe95
1 parent aa998e8 commit 05552d1

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionAnswer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,28 @@ public function setValue($value): void
108108
throw new ValidationException("Question is not set.");
109109
}
110110

111+
if($this->question->isMandatory() && empty($value)){
112+
throw new ValidationException
113+
(
114+
sprintf
115+
(
116+
"Question %s is mandatory.",
117+
$this->question->getId()
118+
)
119+
);
120+
}
121+
122+
if(!$this->question->isValidValue($value)){
123+
throw new ValidationException
124+
(
125+
sprintf
126+
(
127+
"Invalid value (%s) for question %s",
128+
json_encode($value),
129+
$this->question->getId()
130+
)
131+
);
132+
}
111133
$res = $value;
112134
if(is_array($res))
113135
{

app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionType.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,21 @@ private function getValueMaxOrder():int{
262262
public function allowsValues():bool {
263263
return in_array($this->type, ExtraQuestionTypeConstants::AllowedMultiValueQuestionType);
264264
}
265+
266+
public function isValidValue($value):bool{
267+
if(empty($value) && !$this->mandatory) return true;
268+
if($this->allowsValues()) {
269+
if(!is_array($value))
270+
$value = explode(self::QuestionChoicesCharSeparator, $value);
271+
foreach ($value as $v) {
272+
if(!$this->getValueById(intval($v)))
273+
return false;
274+
}
275+
}
276+
if($this->type === ExtraQuestionTypeConstants::RadioButtonQuestionType)
277+
return empty($value) || $value === 'true';
278+
return true;
279+
}
265280
/**
266281
* @param ExtraQuestionTypeValue $value
267282
* @throws ValidationException

0 commit comments

Comments
 (0)