Setting required for read_only question (with calculation)

Hello,
Also a variable/question with read_only appearance can be set to required = true.

  • When is this recommended?
  • What is the internal difference, i.e. calculation timing?

See example
Calc03_Required.xlsx (15.1 KB)

What we find out so far, with a calculate type and read-only (true)
In Enketo (on validation or submission)

  • even a read_only field will show the required error message (see screenshot)

But in ODK (at least in page mode)

  • the read_only field is not marked with error.

In both systems

  • a required error will be shown, if the calculation creates an invalid/empty result (like NaN)? And with read_only, this will block a user!

Maybe for ODK, @Xiphware might have hints?

image

A related question is, please

  • When does it make sense to set a calculate type to required (true)?
  • Does this change internal behaviour?

I am not sure I fully understand the question, but… required and read_only are two independent settings that may be applied to any form control. And there is nothing to prevent the form writer from misusing them in combination to create a form that cannot be submitted [much like there is nothing to prevent a form writer from adding a constraint that can never be true…].

Also a variable/question with read_only appearance can be set to required = true.

  1. When is this recommended?
  2. What is the internal difference, i.e. calculation timing?
  1. I cant think of a usecase where this is useful. Presenting a question the user cannot ever answer, but making it mandatory, rather seems a bit cruel and inhumane… :cry:

  2. Whether the read-only or required XPath expression is evauated first is not prescribed by the XForms spec AFAIK, so this is an implementation detail. So long as the final outcome is the same: ie questions where read-only (currently) evaluates to true cannot change the associated data (as referenced by the associated XForms binding between the UI element and instance data), and the entire form cannot be submitted if any bindings where required evaluates is true have a null value.

When does it make sense to set a calculate type to required (true)?

Unless the calculation can potentially generate null (!?) there wouldn’t be any situation when the calculated result would be null, hence setting its required=true would be redundant (it will always have a non-null value) but not otherwise harmful.

2 Likes

Hello @Xiphware,
Thanks for the quick and detailed reply!

Use case example:

We used the following in a HH member repeat loop to make sure that the first person is the head, but allowing to select the relationship for all other members

type: select_one Relationship name: RelationHHM
label: What is the RELATIONSHIP of ${NameHHM} to the Head of Household?
required: true (as we need this for all HH members)
read_only: if(${LineHHM} = 1, true(), false())
calculation: once( if(${LineHHM} = 1, ‘1’, ‘’) ).

Choice_List Relationship
image

I is working fine in ODK Collect. (But Enketo still seems not supporting dynamic read_only).

Kind regards and healthy and comfortable 2022

Correct. This issue remains outstanding in Enketo. I’ll raise it in ODK call next week.

2 Likes

Thanks, @Xiphware,
Same missing Enketo support for dynamic default. Would you mind to also adress the documentation issue in this context, please?

What do you mean with “null” here, please? At least a calculation may give an empty string, a NaN or InvalidDate, as far as we have seen…

We posted an ODK problem, partly related with UI and calculation here
https://community.kobotoolbox.org/t/invalid-calculation-value-initially-shown-in-collect/26500.

Kind regards

Did we get it well, please? The calculation does NOT fire differently, e.g. at different events, if the field (or a calculate) is set to required. Same if read_only.