Skip to content

Add support for dynamic defaults #338

@lognaturel

Description

@lognaturel

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions