getodk / getodk/docs

Add a note about using `if()` with `index-repeat()`

Open
#679 6 comments 0 reactions 0 assignees View on GitHub
Form design ODK
Dominant language
Python
Stars
58
Forks
160
Avg merge
5d 11h
Merged PRs (30d)
12

Description

From old gotchas doc:

----

indexed-repeat failure before adding 2nd repeat group
Using XLSForm, I have a repeat and use indexed-repeat to call variables from there into the non-repeat section of the form. It works fine when the index is a static number. When the index is based on another (currently undefined, but defined before the indexed-repeat is reached) variable it crashes when transitioning from the first repetition to the second. Note that this crash is before the prompt using the indexed-repeat function has actually been reached.

My solution was to put an if on the index so it always has a value. That fixed it but I wanted to put a heads up here. I hope that is clear, if not let me know. The bottom line is, when calling from a repeat into the non-repeating part of the form:

This doesn't work - indexed-repeat(${name}, ${repeat}, ${id})
This does - indexed-repeat(${name}, ${repeat}, if(${id} != null, ${id}, 1))
Clarification: This odd behavior occurs because whenever you alter a value, all formulas using the value are re-evaluated. Since the ${repeat} changed (you saved and/or created a new repeat group), the formula containing the indexed-repeat is re-evaluated, but ${id} is not yet initialized.

-----

The examples given in `form-repeats` do not follow this pattern.
They should, and it should be explained.

Contributor guide

Open the contributing guide

Research direction

Start by reading the `form-repeats` examples referenced in the issue and compare them with the `indexed-repeat()` and `if()` pattern described above. Update the examples to follow that pattern and explain why an undefined index can cause the formula to fail during repeat changes; the documentation should clearly show the safe form.

Written by the indexing model from the issue text.

Assessment

Domain
documentation
Issue type
Documentation
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.