Search code examples
movilizer

How to validate that at least one check box is selected in a Multi Select Screen


Assuming I want to validate in a Multi Select Screen (type = 4) that at least one check box is selected. How do I have to define the condition of the related validation in the following example?

<question title="Preferrable Colors" type="4" key="#1">
  <answer nextQuestionKey="END" key="#1_1"  position="0">
    <text>Pink</text>
  </answer>
  <answer nextQuestionKey="END" key="#1_2"  position="1">
    <text>Red</text>
  </answer>
  <answer nextQuestionKey="END" key="#1_3"  position="2">
    <text>Violet</text>
  </answer>
  <text>Select the colors you prefer </text>
  <validation type="ERROR">
    <condition>true</condition>
    <text>Sorry, you have to select at least one color</text>
  </validation>
</question>

Solution

  • an easy way to fulfill your requirement in this static scenario, is to have a look at the 'checked state' of each answer by using the isAnswerSelectedByClientKey method. This method will return true or false and in my approach I am writing all 'states' into an array and perform a check for the existence of true afterwards.

                <question title="Preferrable Colors" type="4" key="#1">
                <answer nextQuestionKey="END" key="#1_1" position="0">
                    <text>Pink</text>
                </answer>
                <answer nextQuestionKey="END" key="#1_2" position="1">
                    <text>Red</text>
                </answer>
                <answer nextQuestionKey="END" key="#1_3" position="2">
                    <text>Violet</text>
                </answer>
                <text>Select the colors you prefer </text>
                <validation type="ERROR">
                    <condition>hasValue(selArray, true) == false</condition>
                    <text>Sorry, you have to select at least one color</text>
                </validation>
                <onLeaveOkPrepareAssignment>
                    selArray = null;        
                    selArray['1'] = isAnswerSelectedByClientKey($answer:'#1_1', null);
                    selArray['2'] = isAnswerSelectedByClientKey($answer:'#1_2', null);
                    selArray['3'] = isAnswerSelectedByClientKey($answer:'#1_3', null);          
                </onLeaveOkPrepareAssignment>
            </question>