File tree Expand file tree Collapse file tree
app/Models/Foundation/Main/ExtraQuestions Expand file tree Collapse file tree Original file line number Diff line number Diff 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 {
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments