Skip to content

Commit 4830cf1

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

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

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

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

111+
if(!$this->question->isValidValue($value)){
112+
throw new ValidationException
113+
(
114+
sprintf
115+
(
116+
"Invalid value (%s) for question %s",
117+
json_encode($value),
118+
$this->question->getId()
119+
)
120+
);
121+
}
111122
$res = $value;
112123
if(is_array($res))
113124
{

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,20 @@ 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(is_array($value)) {
268+
if(!$this->allowsValues())
269+
return false;
270+
foreach ($value as $v) {
271+
if(!$this->getValueById(intval($v)))
272+
return false;
273+
}
274+
}
275+
if($this->type === ExtraQuestionTypeConstants::RadioButtonQuestionType)
276+
return empty($value) || $value === 'true';
277+
return true;
278+
}
265279
/**
266280
* @param ExtraQuestionTypeValue $value
267281
* @throws ValidationException

0 commit comments

Comments
 (0)