@@ -473,7 +473,7 @@ private function escapeCSV(string $value): string {
473473 * @param array $questions Array of the questions of the form
474474 * @param array $answers Array of the submitted answers
475475 * @param string $formOwnerId Owner of the form
476- * @throw \InvalidArgumentException if validation failed
476+ * @throws \InvalidArgumentException if validation failed
477477 */
478478 public function validateSubmission (array $ questions , array $ answers , string $ formOwnerId ): void {
479479 // Check by questions
@@ -767,10 +767,10 @@ private function validateConditionalQuestion(array $question, array $answerData,
767767 throw new \InvalidArgumentException (sprintf ('Conditional question "%s" is missing trigger type configuration. ' , $ question ['text ' ]));
768768 }
769769
770- // Find the active branch based on trigger answer
771- $ activeBranch = $ this ->findActiveBranch ($ triggerType , $ triggerAnswer , $ branches , $ question ['options ' ] ?? []);
770+ // Find the active branches based on trigger answer
771+ $ activeBranches = $ this ->findActiveBranches ($ triggerType , $ triggerAnswer , $ branches , $ question ['options ' ] ?? []);
772772
773- if ($ activeBranch === null && !empty ($ branches )) {
773+ if (\count ( $ activeBranches ) === 0 && !empty ($ branches )) {
774774 // No branch matched but branches are defined - this might be okay if trigger has no value yet
775775 // Only throw if trigger has a value that doesn't match any branch
776776 if (!empty ($ triggerAnswer )) {
@@ -783,8 +783,10 @@ private function validateConditionalQuestion(array $question, array $answerData,
783783 }
784784
785785 // Validate the active branch's subquestions with the full per-question rules
786- if ($ activeBranch !== null && isset ($ activeBranch ['subQuestions ' ])) {
787- $ this ->validateSubmission ($ activeBranch ['subQuestions ' ], $ subQuestionAnswers , $ formOwnerId );
786+ if (\count ($ activeBranches ) > 0 ) {
787+ // Merge subquestion of all active branches
788+ $ subQuestions = array_merge (...array_column ($ activeBranches , 'subQuestions ' ));
789+ $ this ->validateSubmission ($ subQuestions , $ subQuestionAnswers , $ formOwnerId );
788790 }
789791 }
790792
@@ -793,11 +795,11 @@ private function validateConditionalQuestion(array $question, array $answerData,
793795 *
794796 * @param array $question The conditional question
795797 * @param array $triggerAnswer The trigger answer values
796- * @return array|null The active branch or null if none matches
798+ * @return array The active branches or empty array if none matches
797799 */
798- public function getActiveBranch (array $ question , array $ triggerAnswer ): ?array {
800+ public function getActiveBranches (array $ question , array $ triggerAnswer ): ?array {
799801 $ extraSettings = $ question ['extraSettings ' ] ?? [];
800- return $ this ->findActiveBranch (
802+ return $ this ->findActiveBranches (
801803 $ extraSettings ['triggerType ' ] ?? '' ,
802804 $ triggerAnswer ,
803805 $ extraSettings ['branches ' ] ?? [],
@@ -806,30 +808,22 @@ public function getActiveBranch(array $question, array $triggerAnswer): ?array {
806808 }
807809
808810 /**
809- * Find the active branch based on trigger answer
811+ * Find the active branches based on trigger answer
810812 *
811813 * @param string $triggerType The type of the trigger question
812814 * @param array $triggerAnswer The trigger answer values
813815 * @param array $branches The available branches
814816 * @param array $options The options for the trigger question
815- * @return array|null The active branch or null if none matches
817+ * @return array The active branches or empty array if none matches
816818 */
817- private function findActiveBranch (string $ triggerType , array $ triggerAnswer , array $ branches , array $ options ): ?array {
818- foreach ($ branches as $ branch ) {
819+ private function findActiveBranches (string $ triggerType , array $ triggerAnswer , array $ branches , array $ options ): ?array {
820+ return array_filter ($ branches, function ( $ branch) use ( $ triggerType , $ triggerAnswer ) {
819821 $ conditions = $ branch ['conditions ' ] ?? [];
820-
821822 if (empty ($ conditions )) {
822- continue ;
823- }
824-
825- $ matches = $ this ->evaluateBranchConditions ($ triggerType , $ triggerAnswer , $ conditions );
826-
827- if ($ matches ) {
828- return $ branch ;
823+ return false ;
829824 }
830- }
831-
832- return null ;
825+ return $ this ->evaluateBranchConditions ($ triggerType , $ triggerAnswer , $ conditions );
826+ });
833827 }
834828
835829 /**
0 commit comments