Ignoring constraint if variable is empty (no value give)

1. What is the problem? Be very detailed.

  • I need to validate parent’s age not to be greater than the child’s age. But I need to give room for no parent age given… I want to always make sure that if the parent’s age is given it enforces the constraint with the child’s age, which is always there.

2. What app or server are you using and on what device and operating system? Include version numbers.
ODK Survey on Aggregate 1.4.15

3. What you have you tried to fix the problem?
I tried the following ||.
data(‘parent_age’)=="" || data(‘parent_age’)>data(‘child_age’)
data(‘parent_age’)==’’ || data(‘parent_age’)>data(‘child_age’)
data(‘parent_age’).length==0 || data(‘parent_age’)>data(‘child_age’)

4. What steps can we take to reproduce the problem?

5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.

Hello @cmuchuchuti,

I believe that’s achievable in various ways…

The quickest could be:

  • not allowing null values in parent’s age by adding a constraint like (.>=18 and .<=110) or .=999 meaning age of a parent is between 18 and 110 years, and if not known of provided then use 999
  • next, in the child’s age question constraint use if(${parentage}!=999,.<${parentage}-10,.>=0 and .<=110) this will limit the age of a child to the age of a parent minus 10 years … assuming maybe the lowest age a woman can give birth to is 10… you can of course increase it. In case the parent age was not provided then the child age ranges from 0 year (if less than 12months) and 110 years. You can also extend this by adding instructions to what to do when child’s age is not known… you can do this by adding an or .=999 behind the rest of arguments in the child’s constraint

Hoping this helps,
Jules R

Hello @jules,

Thank you for the response.
Please note my question:

  1. Applies to ODK2
  2. I wanted to avoid putting another question of whether parent_age is known or not. If a data collector does not get to the question about the parent_age for some reasons, either due to skip pattern based on earlier conditions or just stops interview and finalize.

Thanks.

Hello @cmuchuchuti,

Sorry about that version, the logic is all is achieved through constraints… but instead of leaving null values or empty variables you specify what user uses in those cases… that’s where you’ve seen I used 999… but all in all the variables remain two the parent’s and child’s age

Best,
Jules R

The major challenge with your strategy is that still a user must get to that variable, which is not always the case. So even if we give them 999…they have instances where they are not going to reach that question. Yet if they get there the constraint should be enforced.
If you check the examples I gave above on what I have tried may give a hint on what I am looking for.
data(‘parent_age’)=="" || data(‘parent_age’)>data(‘child_age’) – so the constraint is met by two options–either the parent_age is “” or parent_age>child_age.
Something along this.

There are a few ways to do this, for example you can add:

|| data(‘parent_age’) == undefined
|| data(‘parent_age’) == null
I think one of those should work
However, it might be better to have the constraint allow for not having a parent or child, like this:
|| selected(data(‘has_parent’), ‘2’)
so that the constraint can’t be avoided just by not having data earlier, since that might itself be an error.

Hi @elmps2018 

Thanks a lot. data(‘parent_age’)==undefined has worked.
There are study specific reasons why I do not want to introduce the select_one option.

Many thanks once again Carol.