Dynamic Constraint Message (Logic expression)

Dear KoBo experts,
we want to specialise error messages. Example:
multiple_select … question is:
label: “What are priority needs not met for your household, if any?”
hint:: Top 3 main needs.
required_message: If no needs defined, select the 'None" option.
constraint_message “You can only select up to 3 needs or ‘None’ alone.”.

We would like to be able to divide in 2 different constraint_messages (for "… more than 3 - and for “… None alone”). Even to be able to define 2 different constraints would be great.
At the moment we use as constraint:
if ( selected(${UnmetNeeds05}, 99), count-selected(${UnmetNeeds05}) = 1, count-selected(${UnmetNeeds05}) <= 3 ). - Could this be done more simply? (99 = “None”)

So far, we only see the option to create/calculate an extra text-variable and reference it in the constraint-Message. It doesn’t seem possible to use logic expressions in constraint_message. Is this true?

Kind regards

Hi @wroos

Try this :

constraint
((count-selected( . ) = 3 or count-selected( . ) = 2) and not(selected(.,‘NONE’))) or count-selected( . ) = 1

Regards
Tulus

per Redirecting to https://getodk.github.io/xforms-spec/

jr:constraintMsg Specifies the custom message to be displayed when the constraint is violated. Value can be string literal ( jr:requiredMsg="message" ) or a translation function call ( jr:requiredMsg="jr:itext('id')" ).

[BTW typo noted…]

Which is to say, the contents of the constraint_message column in an XLSForm are interpreted as either a literal string, or at most a jr:itext() translation. Whereas relevant, required, read_only, constraint, calculation may contain an XPath (aka logic) expression and will be evaluated as such, both requiredMsg and constraintMsg are not evaluated as expressions.

The only way to present different constraint messages is to have them associated with different constraints on different questions, and control the display of said questions using relevance.

1 Like

Thanks!

It would be great, if there is combined documentation (e.g. just a table) which/where references and expressions can be used in which XLS columns for KoBo Collect. (If possible with further special hints.) Trial and error, scanning throug KoBo & ODK Community chats or diving into XForms spec is not making me too happy.

Not only related to your answer, another QUESTION comes up:: Is there is any possibilty to SET (some) KoBo “internal” variables, like jr:constraintMsg, via XLSForms (or something else, without having to recode Forms externally.).? I will extract it in a new topic. Setting values for KoBoCollect internal variables

Separating in two questions, just to get more specific msg, is not an good option for our requirements and will also have disadvantages for later data export/analysis.

Another possibility would be to add a following check calculation and note, but this creates the disadvantage that the user has to move (Android screen) back for correction.

It might be an idea for KoBo optimisation to allow definition of several separate contraints (& messages) or allow logic for message text?

Kind regards and Happy Easter for the KoBo team!

If you havent already, I’d recommend bookmarking these:

1 Like

Thanks!
We knew them and scanned often (getting lost sometimes, instead of finding answers for overall design questions - sorry.). It’s something like an advanced “Best practice” KoBoCollect manual that would be great.
Have a nice day.