qgis – QGIS Attribute Form Constraint

qgis

I have two fields in a geopackage layer:

  1. field1 (text) which has a value map with the values 'yes', or 'no' to choose from. It cant be NULL
  2. field2 (int)

I'm trying to add a constraint to field 2 to make it mandatory to enter a number when field 1 is 'yes'

I've tried many different expressions in combination with a "Not null" constraint., for example:

nullif( "field1", 'yes')

case when "field1"='yes' then -999 end

The latter force me to enter some value when field1 is yes, but wont allow me to enter nothing when field 1 is no.

It feels like I'm complicating things.. How can I solve this?

enter image description here

Best Answer

Try this expression:

"field1" != 'yes' or "field2"

Either field1 should not be yes OR field2 needs to have a value. enter image description here