Select to view content in your preferred language

Using IF statements to create a conditional constraint message

1895
2
Jump to solution
05-03-2023 05:23 AM
Bec
by
Regular Contributor

I want to create a conditional constraint message for a Survey123 Connect question.  The user is asked to input an installation year.  If the year doesn't match a 4 digit format or is greater than the current year, the user should get one of the following two messages:

  • Please enter a valid year format, YYYY.  For example, 2023.
  • Year input cannot be greater than the current year with the exception of 9999.

The expression I have tried within the constraint message column is as follows:

if(${cab_year} = 9999, '',
if(regex(${cab_year}, '^[1-9][0-9]{3}$') and ${cab_year} <= number(format-date(today(), '%Y')),
'', 'Year input cannot be greater than current year with the exception of 9999.')) +
if(regex(${cab_year}, '^[1-9][0-9]{3}$'), '', 'Please enter a valid year format, YYYY. For example, 2023.')

When I go to input any value that should kick out a constraint message, I don't get the particular message, I get the entire constraint message field.  See below image.

I'm new to Survey123 Connect and not sure if this is possible to begin with.  Also, there very well could be something wrong with my expression which is also something I'm learning.  Any help would be greatly appreciated!

 

Cab Year Constraint Message.PNG

1 Solution

Accepted Solutions
Katie_Clark
MVP Regular Contributor

I did a bit of testing, and as far as I can tell it seems like the constraint_message column in the XLS form doesn't recognize conditional logic.

However, there are a couple ways you could achieve this. First off, I put this in the constraint column (these conditions must return TRUE for the entry to be valid)

${cab_year} = 9999 or (${cab_year} <= number(format-date(today(), '%Y')) and ${cab_year} > 1000)

Then, in the constraint_message column, you could just have a more general error message.

"Please make sure you have entered the year in the correct format (YYYY) and it is not greater than the current year."

 

Additionally, you could have note fields with null in the bind::esri:fieldType column that conditionally display if a certain condition is met. So something like this:

In the label<font color = 'red'>Year input cannot be greater than current year with the exception of 9999.</font>

In the relevant column: ${cab_year} > number(format-date(today(), '%Y'))

And then you'd add another note field the exact same way, but with a different label and condition in the relevant column.

Hope that helps!

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek

View solution in original post

2 Replies
Katie_Clark
MVP Regular Contributor

I did a bit of testing, and as far as I can tell it seems like the constraint_message column in the XLS form doesn't recognize conditional logic.

However, there are a couple ways you could achieve this. First off, I put this in the constraint column (these conditions must return TRUE for the entry to be valid)

${cab_year} = 9999 or (${cab_year} <= number(format-date(today(), '%Y')) and ${cab_year} > 1000)

Then, in the constraint_message column, you could just have a more general error message.

"Please make sure you have entered the year in the correct format (YYYY) and it is not greater than the current year."

 

Additionally, you could have note fields with null in the bind::esri:fieldType column that conditionally display if a certain condition is met. So something like this:

In the label<font color = 'red'>Year input cannot be greater than current year with the exception of 9999.</font>

In the relevant column: ${cab_year} > number(format-date(today(), '%Y'))

And then you'd add another note field the exact same way, but with a different label and condition in the relevant column.

Hope that helps!

Best,
Katie


“The goal is not simply to ‘work hard, play hard.’ The goal is to make our work and our play indistinguishable.”
- Simon Sinek
Bec
by
Regular Contributor

Below is how I have my constraint currently set up:

(${cab_year} = 9999) or (regex(${cab_year}, '^[1-9][0-9]{3}$') and ${cab_year} <= number(format-date(today(), '%Y')))

This constraint works for what I intended.  I was just hoping in the constraint message I could use IF statements to apply conditional constraint messages based upon the user input.  It looks like from your reply that is not possible.  So my constraint message is set to display as follows: 

Enter a 4-digit year in the format YYYY. Year input cannot be greater than current year with the exception of 9999.

Which it still gets out what I needed to the user if they input an incorrect value. 

I definitely appreciate your input and will play around with the other options you suggested.  It's all a learning experience for me!