Add support for dynamic defaults by expanding what the exiting default column does. See the forum thread for discussion.
If the contents of the default column are not a literal value, the expression is passed through to the value attribute of a setvalue action triggered by the odk-instance-first-load event. In the case of nodes that are not in repeats, this action is added as a child of the model and ideally right after the bind for the node of the value it sets (this is not a hard requirement -- it can be anywhere in the model).
In the case of repeats, the action is added as a child of the repeat form control (in the body) and the triggering event is odk-instance-first-load odk-new-repeat.
Given the following row from a survey sheet:
| type |
name |
label |
default |
| date |
event_date |
Event date |
today() |
The following XForm content should be generated in the model:
<bind nodeset="/data/event_date" type="date"/>
<setvalue ref="/data/event_date" value="today()" event="odk-instance-first-load"/>
Given the following rows from a survey sheet:
| type |
name |
label |
default |
| begin repeat |
event |
Event |
|
| date |
event_date |
Event date |
today() |
| end repeat |
event |
|
|
The following XForm content should be generated in the body:
<repeat nodeset="/data/event">
<setvalue event="odk-instance-first-load odk-new-repeat" ref="/data/event/event_date" value="today()"/>
<input ref="/data/event/event_date">
<label>Event date</label>
</input>
</repeat>
I believe the best approach for identifying static vs. dynamic defaults will be to use a regular expression. It should be fine for static values to end up as the value for the value attribute so not a big deal if the regex catches some things that are not actually intended to be evaluated. However, there is the possibility that a user will want to literally use text that could be evaluated. For example, a user could want the literal string "today()" as a default and that would no longer be possible. We may want to add some kind of escape strategy for that case but I propose we wait until a user requests it because it seems unlikely to me.
Add support for dynamic defaults by expanding what the exiting
defaultcolumn does. See the forum thread for discussion.If the contents of the default column are not a literal value, the expression is passed through to the
valueattribute of asetvalueaction triggered by theodk-instance-first-loadevent. In the case of nodes that are not in repeats, this action is added as a child of themodeland ideally right after thebindfor the node of the value it sets (this is not a hard requirement -- it can be anywhere in themodel).In the case of repeats, the action is added as a child of the repeat form control (in the
body) and the triggering event isodk-instance-first-load odk-new-repeat.Given the following row from a survey sheet:
The following XForm content should be generated in the
model:Given the following rows from a survey sheet:
The following XForm content should be generated in the
body:I believe the best approach for identifying static vs. dynamic defaults will be to use a regular expression. It should be fine for static values to end up as the value for the
valueattribute so not a big deal if the regex catches some things that are not actually intended to be evaluated. However, there is the possibility that a user will want to literally use text that could be evaluated. For example, a user could want the literal string "today()" as a default and that would no longer be possible. We may want to add some kind of escape strategy for that case but I propose we wait until a user requests it because it seems unlikely to me.