# How is Constraint being used exactly?

**URL:** <https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024>\
**Category:** Support\
**Created:** [November 4, 2020, 5:52am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024 "2020-11-04T05:52:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Neko\_Acedia](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/neko_acedia/32/13565_2.png) [@Neko\_Acedia](https://forum.getodk.org/u/Neko_Acedia)\
**Post date:** [November 4, 2020, 5:52am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/1 "2020-11-04T05:52:55Z")

</div>

I'm sorry for posting so many questions here on the forum.

Regarding select\_one questions, Constraint:

> not(selected(.,'B02'))

This 'constraint' will display an error when optionName B02 **_is_ selected**.

This always happens when I am trying out validation on my forms. I am always confused on how Constraint really works.

Are conditions placed on contraint need to be what should not happen, or what should happen?

**1. What is the problem? Be very detailed.**

I have two select\_one questions.  
Q1 has 9 options  
Q2 has 10 options

What I want to happen, is While Q1=opt 1-6 selected, and Q2=opt 2 selected, there would be an error.

**2. What app or server are you using and on what device and operating system? Include version numbers.**  
I am using XLSForm Viewer to test the form: [https://getodk.org/xlsform/](https://getodk.org/xlsform/)

**3. What you have you tried to fix the problem?**  
I have searched "Constraint on Select\_one" on the net and this forum multiple times, but I have yet to find a solution to my problem.

- I first tried isolating the conditions, from which When Q1 opt 1-6 is selected it would display an error

> not(.='A00' or .='A01' or .='A02' or .='A03' or .='A04' or .='A05' or .='A06')

- Then when Q2 opt 2 is selected, it would also display an error.

> not(selected(.,'B02'))

- I tried combining the two:

> not(selected(.,'B02') and .='A00' or .='A01' or .='A02' or .='A03' or .='A04' or .='A05' or .='A06' )  
> But then Q2 doesnt display errors

- Tried just separating them

> not(selected(.,'B02')) and not(.='A00' or .='A01' or .='A02' or .='A03' or .='A04' or .='A05' or .='A06')  
> But then Q2 will always display error when opt 2 is selected.

**5. Anything else we should know or have? If you have a test form or screenshots or logs, attach below.**

[ODK Tester (43).xlsx](https://forum.getodk.org/uploads/short-url/vYKl34rovaMiiRImpRvfoVYCaUL.xlsx) (39.0 KB)

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [November 4, 2020, 8:16am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/2 "2020-11-04T08:16:18Z")

</div>

Hi @Neko_Acedia

> [@Neko\_Acedia](#):
>
> I'm sorry for posting so many questions here on the forum.

that's ok, it's always good to spend a few minutes googling and browsing the forum because I guess about 70% of new questions here are repeated but generally if you are not able to find anything helpful don't hesitate to ask.

> [@Neko\_Acedia](#):
>
> Are conditions placed on contraint need to be what should not happen, or what should happen?

according to the doc [https://docs.getodk.org/form-logic/#validating-and-restricting-responses](https://docs.getodk.org/form-logic/#validating-and-restricting-responses)

> To validate or restrict response values, use the constraint column. The constraint expression will be evaluated when the user advances to the next screen. If the expression evaluates to True, the form advances as usual. If False, the form does not advance and the constraint\_message is displayed.

> [@Neko\_Acedia](#):
>
> not(selected(.,'B02'))  
> This 'constraint' will display an error when optionName B02 **_is_ selected**.

Yes and that's aligned with what's said in the doc `If the expression evaluates to True, the form advances as usual`` not(selected(.,'B02'))` will return false in such a case because `selected(.,'B02')` returns true and `not()` changes it into false.

regarding your problem... if you use constraints that calculate their result based on multiple questions like in your case it might be better to put those questions into [field-list group](https://docs.getodk.org/form-question-types/#grouping-multiple-widgets-on-the-same-screen) to display on one screen. It's not a must I'm just mentioning it because I think in most cases it might make the form more readable.

To solve your problem you can use constraint like:  
`not((${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’) and .=’B02’)`

Here is a corrected form:  
[ODK Tester (43).xlsx](https://forum.getodk.org/uploads/short-url/dgbv931NFNW7Pe2jmFOsntEEEmW.xlsx) (7.4 KB)

The easiest way of thinking about constraints like that when you want to display it when a combination of different conditions take place is to build it in the way you naturally think about it and add `not()`, in your case such scenario you want to block is:

`(${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’) and .=’B02’`

and then just wrap it using `not()`.

BTW. make sure that using constraints your questions require answer, otherwise if an answer might be empty constraints are not evaluated what might be confusing.

---

<div class="post-metadata">

**Author:** ![Neko\_Acedia](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/neko_acedia/32/13565_2.png) [@Neko\_Acedia](https://forum.getodk.org/u/Neko_Acedia)\
**Post date:** [November 4, 2020, 8:35am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/3 "2020-11-04T08:35:01Z")

</div>

Thank you for the clear explanation for constraints, I was really confused as they work one way or the other at times mostly leading to an unexpected result that I never did understood.

> [@Grzesiek2010](#):
>
> (${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’) and .=’B02’

Just to clarify, as I still did not understood how this code worked.

Was my mistake, where I placed _.=’B02_ first before _(${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’)_ ?

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [November 4, 2020, 8:44am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/4 "2020-11-04T08:44:06Z")

</div>

> [@Neko\_Acedia](#):
>
> Was my mistake, where I placed _.=’B02_ first before _(${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’)_ ?

No but I'm not sure which of options you provider you are referring now.

> [@Neko\_Acedia](#):
>
> not(.='A00' or .='A01' or .='A02' or .='A03' or .='A04' or .='A05' or .='A06')

This one is generally ok but since you are using . (current question) that means you are in your first question and then you are just checking that particular part of your form. Your question was about checking answers from two questions at the same time (as a combination)

> [@Neko\_Acedia](#):
>
> not(selected(.,'B02'))

as above generally ok but you wanted to check both answers

so my constraint is a combination of those two:

`not((${op1}=’A01’ or ${op1}=’A02’ or ${op1}=’A03’ or ${op1}=’A04’ or ${op1}=’A05’ or ${op1}=’A06’) and .=’B02’)`

> [@Neko\_Acedia](#):
>
> not(selected(.,'B02') and .='A00' or .='A01' or .='A02' or .='A03' or .='A04' or .='A05' or .='A06' )

This one probably would work but you can't use dots everywhere. If you put that constraint in your second question you can use `selected(.,'B02')` but then instead of `.='A00'` you need `${op1}='A00'`.

---

<div class="post-metadata">

**Author:** ![Neko\_Acedia](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/neko_acedia/32/13565_2.png) [@Neko\_Acedia](https://forum.getodk.org/u/Neko_Acedia)\
**Post date:** [November 4, 2020, 8:50am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/5 "2020-11-04T08:50:40Z")

</div>

I see, I think I understand it now. Thank you, once again for the explanation.

---

<div class="post-metadata">

**Author:** ![Grzesiek2010](https://getodk.b-cdn.net/user_avatar/forum.getodk.org/grzesiek2010/32/2881_2.png) [@Grzesiek2010](https://forum.getodk.org/u/Grzesiek2010)\
**Post date:** [November 4, 2020, 8:54am UTC](https://forum.getodk.org/t/how-is-constraint-being-used-exactly/31024/6 "2020-11-04T08:54:42Z")

</div>

I'll just add again because it was your mistake that dots in expressions like `.='A00'` you can use only if you refer an answer from the same question (the same row in xls form), but if you refer previous questions you need to add their full name like `${op1}` for example. It applies to all calculations, constraints, relevant.
