Skip to content

Commit

Permalink
fix: extra questions add validation for answer setting
Browse files Browse the repository at this point in the history
Change-Id: I4367bc5f9a8dc702e725d165dc317cdfc155fe95
  • Loading branch information
smarcet committed Mar 28, 2024
1 parent aa998e8 commit 4830cf1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
11 changes: 11 additions & 0 deletions app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionAnswer.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,17 @@ public function setValue($value): void
throw new ValidationException("Question is not set.");
}

if(!$this->question->isValidValue($value)){
throw new ValidationException
(
sprintf
(
"Invalid value (%s) for question %s",
json_encode($value),
$this->question->getId()
)
);
}
$res = $value;
if(is_array($res))
{
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Foundation/Main/ExtraQuestions/ExtraQuestionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,20 @@ private function getValueMaxOrder():int{
public function allowsValues():bool {
return in_array($this->type, ExtraQuestionTypeConstants::AllowedMultiValueQuestionType);
}

public function isValidValue($value):bool{
if(is_array($value)) {
if(!$this->allowsValues())
return false;
foreach ($value as $v) {
if(!$this->getValueById(intval($v)))
return false;
}
}
if($this->type === ExtraQuestionTypeConstants::RadioButtonQuestionType)
return empty($value) || $value === 'true';
return true;
}
/**
* @param ExtraQuestionTypeValue $value
* @throws ValidationException
Expand Down

0 comments on commit 4830cf1

Please sign in to comment.