Constraints dependent on another field

Hi! I am having trouble setting constraints dependent on another field.

The first question is 'age' and second question is 'age unit' (months or years). If 'months' is selected then I want the constraints for the age field to be 0-24, if 'years' is selected then I want the constraints for the age field to be 0-120.

Any help?

Thanks!

Welcome to the ODK forum, @faran_d! We're glad you're here. When you get a chance, please introduce yourself on this forum thread. I'd also encourage you to add a picture as your avatar because it helps build community!

The constraint is checked when the question value changes, or when the entire survey is validated before submission. This can make it confusing if the constraint for the first question cannot be fully evaluated until the second question is answered (and therefore, not until the survey is validated for submission at the end).

One option would be to move the question about age unit to before the age integer question.

In both of the above cases, you have a single column of data in your results that mixes numbers with different units. A "2" could be 2 months or 2 years. Which I think can lead to confusion during analysis.

Another option would be to ask the age category (0-23 months old or 2+ years old) and then use relevance to ask (in separate questions) either months or years. If you need a single age column, you could use a calculate type question that copies in either the years value or the months value divided by 12. Then all values in your data results for that column would be in unit of years.

I have examples of all three scenarios in this file:
constraints_example.xlsx (10.2 KB)

Totally agree! The antecedent, upon which the inputted value's validation depends, should naturally come prior. You can certainly construct an (X)form that performs validation checks which employ any other value(s) located anywhere else in the form, but this will lead to unexpected and awkward behavior from a user perspective. Try to guide your user sequentially as much a possible when filling in your form, both in terms of validation and showing/hiding relevant questions.

1 Like

Thanks for your suggestions! I went with the suggestion of asking the age category (0-23 months or 2+ years) first, and then the age in months/years depending on the answer to that field.

2 Likes