Define answer choices based on previous answers

Hello,

I need help from the community to define answer choices based on previous answers for the attached form [test_form.xlsx|attachment]
test_form.xlsx (8.2 KB)
:

  • create a condition which forces you to choose only one answer to the question "date_inonde_hab" when you have answered "yes_one" to "inonde_hab".

  • that the answers to the question "date_inonde_ref" correspond only to those chosen by answering the previous question "date_inonde_hab".

Thank you in advance
Pierre

Hi @Pierre_Floods

To solve this I would add something like this if(${inonde_hab}=’oui_une’,count-selected(.)=1,true()) in the constraint column.

If two sets of choices are equal (that's the case as I can see date_inonde_hab and date_inonde_ref) then you can simply remove one of them and use the same one in both questions. To filter out not selected options you can add selected(${date_inonde_hab},name) to the choice_filter column.

Here is your updated form. I've highlighted the edited parts in yellow:
test_form.xlsx (8.7 KB)

thank you very much @Grzesiek2010, it works very well !!

Despite this, I try to reuse this type of constraint (if(${inonde_hab}=’oui_une’,count-selected(.)=1,true())) in other questions, but I can't manage it when it's in the same question (depending on the responses in a selected_multiple question type).

In the attached example
test_flood.xlsx (6.4 KB)
, several responses can be chosen but I want to limit to only one response only if I select the response "I don't know".

Can you help me please in that case? Thank you in advance

I found that response you gave @Grzesiek2010 in this forum query : Multiple select, constraint, one answer then nothing else

It works for me, using it with : not(selected(.,'nsp')) or count-selected(.) = 1, in the attached form
test_flood.xlsx (6.4 KB)

However, I should admit that I don't really understand the logic of this type of code, such as if(${inonde_hab}=’oui_une’,count-selected(.)=1,true()))

Which language is it? Is there a documentation about this kind of code? Thank you in advance

inonde_hab is the name of your first question (select one).
oui_une is the name of the choice that represents oui, par une seule inondation.
count-selected is a function which checks how many answers were selected in multi_select_question see the doc https://docs.getodk.org/form-operators-functions/#count-selected. The dot passed to the function as an argument indicates that we are counting answers passed to the current question (you can do the same for another question by passing its name instead of the dot).

so we can translate that logic to human language in this way:
If the answer to Votre logement/garage a-t-il déjà été touché par une inondation ? (inonde_hab) is oui, par une seule inondation (oui_une) the number of answer selected in the current question A quelle(s) date(s) avez-vous été inondé ? (date_inonde_hab) should be 1 otherwise it can by any.

Constraints work in the way that if the logic returns true then everything is fine and if it's false then an error is shown. That why in the first part you have count-selected(.)=1 because you want it to be true and in the second part it's simply true() which means it returns true always because we don't care about the number of selected choices if the answer to inonde_hab was not oui_une.

You ca read more about how to build logic for constraints, calculations etc here: https://xlsform.org/en/

2 Likes