At 3.24.2, I have a Geopackage table containing two fields:
- "interior_observed" is a Boolean, with a Checkbox widget type, true or false.
- "egg_number" is an integer, with a Range widget type.
The widget types will be used with the Mergin Input app, as follows:
- If field staff view the interior of a bird nesting box, they record the number of eggs (greater than or equal to zero) therein.
- If the field staff do not view the interior, the number of eggs is unknown, and its resulting value must therefore be NULL.
Unfortunately, every constraint expression that I've tried has no effect on "egg_number". That is, any value (including NULL) can be applied to "egg_number", regardless of whether "interior_observed" is true or false - as if no constraint exists. The situation is complicated with additional widget options such as NULL enforcement and default values.
The seemingly most straightforward expression I've tried is
nullif("interior_observed" , 'false')
I've also experimented with variations on the answers at Attribute form constraint and and QGIS constraint not null depending on another field value
In summary, how to constrain "egg_number" to NULL when "interior_viewed" = 'false'?
Here's a screenshot of the constraints for "egg_number":
Here's a screenshot of the "interior_observed" widget settings:
-
Please note: 'false' is a string literal, not a boolean.peter– peter2022年05月19日 20:34:56 +00:00Commented May 19, 2022 at 20:34
-
I've edited the OP to included the "interior_observed" widget settings.Stu Smith– Stu Smith2022年05月19日 20:49:59 +00:00Commented May 19, 2022 at 20:49
-
My gut tells me this method will not work. But if it could work you should begin by changing nullif("interior_observed", 'false') to nullif("interior_observed", false). (Remove the quotes around false.)peter– peter2022年05月19日 21:14:12 +00:00Commented May 19, 2022 at 21:14
-
Unfortunately, changing 'false' to false made no difference.Stu Smith– Stu Smith2022年05月19日 21:37:42 +00:00Commented May 19, 2022 at 21:37
1 Answer 1
It does not work the way we imagined. We falsly assumed that the return-value from the expression nullif("interior_observed", false)
would be assigned to the field content. My guess now is that the return-value is only evaluated as a boolean, merely to decide if the field is valid.
To assign a field value, you need to use the Default value of the widget.
It's a bit messy but the following, finally, works for me (that is, in QGIS):
A. In the egg_number widget:
- Add this Constraint expression:
if("interior_observed"=false and "egg_number" is null, true, false)
OR
if("interior_observed"=true and "egg_number" is not null, true, false)
Tick the Enforce constraint checkbox
Add this Default value expression:
if("interior_observed"=false,NULL,NULL)
*This looks weird, as it always returns NULL, but I think this is needed so the value is re-evaluated and re-applied on each update. (Just guessing)
- Tick the Set Apply default value on update checkbox
B. On the interior_observed widget:
- Untick the not null constraint.
-
Peter, your solution isn't working for me. Would you please add a screenshot to your answer? Also, In your second paragraph, what do you mean by "... Default value of the widget"? Finally, I'm using Widget type = Range. What type are you using?Stu Smith– Stu Smith2022年05月21日 04:00:31 +00:00Commented May 21, 2022 at 4:00
-
@Stu Smith: I have changed my answer. My fields are named different from yours, and I forgot to adapt them. So I changed "inspected" to "interior_observed", and "eggs" to "egg_number". I assume that will fix it.peter– peter2022年05月21日 20:21:14 +00:00Commented May 21, 2022 at 20:21
-
@Stu Smith: The Default value is in the last section of the widget properties. See your own screenshots... This value is assigned to the field when you open an insert form for a point.peter– peter2022年05月21日 20:57:50 +00:00Commented May 21, 2022 at 20:57