0

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>
André Schäfer
  • 608
  • 5
  • 15

2 Answers2

1

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> 
Andreas
  • 106
  • 3
0

you can define the condition to:

<condition>getQuestionValueNew() == ""</condition>

So when nothing is selected this returns true and if something is selected it returns false.

JCimbal
  • 46
  • 3
  • Hmmm I tried this but on my Android client the condition is always true, even if I select all visible checkboxes. The documentation of getQuestionValue does not indicate that it is intended to be used in screens where multiple selections are possible. – André Schäfer May 19 '15 at 16:14
  • I tried it on iOS and Swing and it worked as expected... But if it should not be used in screens, where multiple selections are possible, Andreas answer is better. – JCimbal May 22 '15 at 06:54