Can webforms evaluate constraints when a field is set to a default via trigger?

1. What is the issue? Please be detailed.
Is there a way to get Enketo to trigger validation when a field is set to a default by a trigger?

I'm running ODK Central self-hosted on AWS.

versions:
e4221ebeb41cd6ccb0cedad0461e5b603c207339 (v2024.3.2-1-ge4221eb)
 8b1de6512faa7a60c05764312caec01f5c138c42 client (v2024.3.2)
 7574030f7ea8750f3837950001a5efcdeba45b92 server (v2024.3.1)

I'm hosting an ODK Form via a public submission link and I expect nearly all submissions to come from a browser via the Enketo webform. So far, it works absolutely beautifully.

The XLSForm has one field for a default date, and several groups of other fields which prefill that default date when a third field is clicked.

There are several of these prefilled fields, and most have their own date range limits, so I'd like to nudge our users to adjust the date ranges to the available limits (which I also show in the form as note).

What works well: when submitting / validating, the fields display their validation error messages.
What I would love to see is that constraints are evaluated when fields are set to defaults via a trigger.
This would mean that users see any errors as they go through the form and don't have to scroll back after trying to submit (and feel bad about having filled in the form incorrectly).

The salient fields are:

# Field 1: The default date
date	ds_default_start_date	Default start date	The default start date for any requested dataset unless specified differently for a requested dataset.

# Field 2: The checkbox which decides whether field 3 is shown & populated or not
select_one yes_no	ds_doc_notifications_requested	Include this dataset?	This dataset is Event based. Data is available from 1977.	yes			no

# Field 3: A date that is set to the date of field 1 if field 2 selects "yes"
date	ds_doc_notifications_start_date	Start date	The earliest date of event to include

required:     ${ds_doc_notifications_requested} = 'yes'	
relevant:     ${ds_doc_notifications_requested} = 'yes'		
constraint:  . >= date('1977-01-01')	
constraint_message: Please select a start date from 1977 onwards.	
calculation:  ${ds_default_start_date}	
trigger:      ${ds_doc_notifications_requested}

2. What steps can we take to reproduce this issue?
If helpful, I will create a MVE. For now, I'm curious whether others have experienced the same issue.

When filling in the form on ODK Collect, the validation is triggered on swiping to the next screen, and the validation message shows up (and the layout is of course absolutely stunning).

Because the form is loaded in its entirety in Enketo, the "moving to next screen" event is of course missing.

3. What have you tried to fix the issue?

Via form design: I should improve the default a calculation so that e.g. my field 3 (populated with date from field 1) is the latest date (max()?) out of field 1 default date and my "earliest_available_date".

So this formula works by avoiding any validation errors happening through the trigger default:

if(${ds_default_start_date}<date('1971-01-01'),date('1971-01-01'),${ds_default_start_date})

if(default_date < earliest_available_date, earliest_available_date, default_date)

(The user could still update this value by hand and enter an invalid value, which then will raise the constraint message on submission. Fine by me.)

But this does not address the issue, which could be a limitation of Enketo, so I would like to suggest this to be considered for the upcoming ODK webforms.
Edit: I missed that Enketo has a "pages" mode https://xlsform.org/en/#multiple-webpage-forms which likely evaluates constraints on navigating to the next form page.

4. Upload any forms or screenshots you can share publicly below.
I can send the XLSForm as is via DM or upload a simplified MVE.

An interesting alternative option might be to use a once(...) calculation instead of a "default"..

1 Like

I don't remember when validation happens when using Enketo's pages mode. Maybe you could give that a quick try if you haven't already? Our intent with ODK Web Forms once we get to a one-question-or-field-list-per-page view is to validate on page change like Collect.

Showing constraint violations caused by default values at form load time could lead to a lot of visual noise so that's not currently something we're considering for ODK Web Forms but we'll see whether we get other ideas to satisfy your need.

You could also consider an alternative form structure where you show the default date in the label for a yes/no question. That would be something like "Is the date 1977-01-01". Then if the user says no, you can show a date field. You can then use a calculate with coalesce to have a single field for analysis.

I think that wouldn't make a difference in terms of when constraints are evaluated but it might be good to double check! The difference between using once and a calculate with a trigger is that with once, the default value never updates after it's set. Which is more appropriate depends on the context.

1 Like

Thanks for the insight @wroos and @LN!

Thanks for pointing out the Enketo pages mode - https://xlsform.org/en/#multiple-webpage-forms - I suspect validation might work in this mode just as I've expected.

In my particular use case, my solution was to improve the calculation to provide the correct value straight away as outlined in my step 3 above.

In the long term, ODK Web Forms validating on page change will be the exact solution I was after. In combination with calculating the value on trigger straight away to fit any constraints this covers all use cases I can think of.

Thanks!