Hi all,
Given a simple form for a layer with a field “Name”, I need this field to be set, an in a specific format : non null + enforce expression constraint. That’s OK.
Now on top of that, how to alert the user if his entry already exists in this field “Name”. Just an alert It’s not unique, you’ve been warned.
A small text in orange, complementary to the red text related to the constraints criterion.
Thanks!
I think you just need to not enable the Enforce unique constraint right beside the Unique checkbox on the attribute’s form:
QField then would follow the same behaviour as QGIS, painting the field orange while you populate it to let you know that it is not unique, but would just let you save it anyways.
Thanks @cuprico , I tried that: the behavior is as you described in QGIS, but in QField, the constraint seems to be interpreted as an enforced one.
In QField, I see in red my constraint by expression, not null, and unique.
Can’t add a duplicate.
hmm, have you tried without the expression constraint? I seem to remember the orange warning, maybe there can only be a single warning shown at a time (as I remember the space for the error message to be quite small, in-between the top empty space of the attribute field).
Maybe you can enforce the constraint in a different way. What about, for example, spliting the attribute into two:
- One numeric integer field, with the only constraint being limiting it to 999 or below. I think you can add the 0 padding afterwards with an expression, and this would speed the user’s input of the number.
- Another attribute of type text, maybe called something like “suffix letter”. On it, you can give a dropdown to the user to pick the correct letter, instead of an usual typing field. The interaction itself would limit what the user can input.
Then, if you would really like to have an attribute with the full name, it can be a third one, hidden from the user and autopopulated (using a “default value” expression) with those two fields.
I have been testing this and it seems QField does not differentiate between enforcing or not enforcing the unique constraint, QField treats the Unique constraint always as Enforced
The Not Null constraint by itself has the correct behaviour: when Enforce is not checked, it will show as orange and you can save a NULL value
The Expression constraint by itself has the correct behaviour: when Enforce is not checked, it will show as orange and you can save a value that does not match your expression
The Unique constraint, even by itself does not have the correct behaviour: even when Enforce is not checked, you are not allowed to fill in a duplicate value.
This has to be a bug in QField.
I have just now filed it as a bug: Unique constraint is always treated as Enforced · Issue #8069 · opengisch/QField · GitHub
Hi @Jeroen-GroeneBij, @cuprico ,
By now, to get a simple alert about the unicity, I’ve opted for this :
It shows the orange flag but permits the record.
I’ve retained @cuprico proposition with two different fields, one for the site number, another for the suffix. Both are mandatory and unique.
I’ve created another “full_name” field (virtual one), populated with the expression needed to format the record. This field is hidden. And the screenshot above shows how both the flexible unique constraint and the auto update of the “full_name” field are managed to trigger the alert.
Thank you both for your support.
I close the topic as my specific need is solved, but I confirm unique constraint is always treated as Enforced. Thank you for opening the ticket.