1

I want to populate a text field of a layer with a set of predefined attributes in QGIS 3.4.

For example, The field name is Type. The attributes set are AAA, BBB , CCC. I want to limit the entry of this field to the attribute sets so only one of these attribute can be entered in this field.

How can I limit the value of this field to the predefined attribute sets so that the user can enter them without typing?

3

You can do using the following steps:

  • Go to Layer Properties -> Attribute Form
  • Select the target field Type in your case
  • Under Widget Type, select Value Map
  • Fill up the each row with the value you want AAA, BBB, and CCC.
  • Select Unique and Enforce Unique Constraint

enter image description here

Check the attribute table, you can only choose from the values you defined:

enter image description here

Update

Based on discussions with J.R, when you add a new feature, enabling Enforce Unique Constraint will not let you add any of AAA, BBB, and CCC if they are already existing in the attribute table. You need to disable Enforce Unique Constraint to get multiple same records.

  • the "Select Unique and Enforce Unique Constraint" will prevent to fill several identical value if you use the form to fill in attribute (if you fill tha attribute directly in the attribute table the constraint are not enforced and you can fill several time the same value...). In this case I don't think this setting is what we want as it seem that the value don't need to be unique, the setting to consider is the "Not null" constraint if you want to force user to fill a value – J.R Oct 25 at 8:42
  • @J.R Thanks for sharing your thoughts. In my understanding Select Unique and Enforce Unique Constraint is used to avoid duplicate values in the list itself but still can input many same values in the attribute table. Using Not Null will prevent null values but I am not if the OP wants to fill up all the rows inside Type column or want to keep some null rows. I leave this to the OP to decide. – ahmadhanb Oct 25 at 8:52
  • The unique constraint(or any other constraint) is for the attribute form only, try to create a few new point with the "show form on add feature" setting and you will see that you cant validate the form if you try to set several point with the same attribute. (As as said before if you open the attribute table to set the value the contraint is not enforced and you will be able to set the same value for all your point if you need) – J.R Oct 25 at 9:04
  • @J.R You are right, but if I uncheck Enforce Unique Constraint only, I found it accepts a same value to what is existing in the attribute table when I add a new feature (polygon in my case). – ahmadhanb Oct 25 at 9:12
  • 1
    Yes, if you uncheck 'Enforce Unique Constraint' you just get warning that the uniqueness is not respected (the field turn orange and you get an orange cross right to it) but you can validate the form (so the constraint is here but QGIS is not enforcing it, it's up to you to respect the constraint or not). My point is to not setting constraint if you dont really need them regardless if you can bypass it or not. – J.R Oct 25 at 9:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.