[Qgis-user] Built-in attribute form validation

Nathan Woodrow madmanwoo at gmail.com
Tue Dec 20 03:41:16 PST 2011


Hi all,

I have been working on this idea over the last couple days which I think
would be cool/handy to have in QGIS and I would be interested to get some
feedback with what people think.

The general idea is to add a form of field validation to the built-in
attribute forms.  The use case for this is pretty clear I think, being able
to let the user/admin of any layer define validation rules to check against
the data entered on the attribute form when the user presses OK providing
instant feedback.  I normally do this on my PostGIS tables but you don't
get errors until you try and commit the data and even then it's a simple
"there is a error", if you have added more then 1 item you have to check
each one to work out what is wrong, and this method only applies to a
PostGIS layer.  The idea I have been drafting would work on any layer, as
it's at the QGIS level vs at the datasource level.

My idea is to use simple a simple Python file with basic functions that
will be called for each field in the attribute form that is editable.  I'm
a big fan of convention over configuration so my idea uses conventions to
define what functions are used to validate what fields.  The function will
return False and an error message that can be shown to the user if
something is wrong.

The functions use the following syntax: validate{FieldName} or
validate{FieldIndex} for naming (plus some other styles) e.g

def validateName():
   if value == "":
      return (False,"Name can't be NULL")
   return True

You would place the function in a python file e.g PeopleRules.py and then
tell QGIS to use this file to look for validation rules, pretty much the
same as we do now for having custom Python forms.

Overview of logic:

   1. Define Python file with rules
   2. Tell QGIS to use the rules file for the layer.
   3. Open attribute form
   4. Edit some values
   5. Press OK
   6. QGIS calls each function in the rules file to validate each field
   that is defined with a rule
   7. If there are errors: cancel the accept() action and show the errors.
    If there are no errors go though like normal.

My thoughts for using Python are:

   - Easy to read and write
   - Not limited to what we have in QgsExpression and easier to expand.
   - Can add documentation to validation rules.
   - Can add more complex validation rules e.g. regex, parsing, database
   checks etc

My overall goal with this is to reduce the need to create custom Python Qt
forms when doing data entry, and doing validation is one of the reasons I
tend to make custom Python Qt forms but then you loose the ease of QGIS
doing most of the form work for you.

I have attached two files which have a example of what the idea is.
 fieldChecks.py is an example rules file with comments to explain each
section, runMe.py has the logic for reading and executing the rules for
each field.  Run: python runMe.py to see the running logic and output.

What love to get peoples thoughts about this, users and devs a like.

- Nathan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20111220/edfdc267/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: fieldChecks.py
Type: text/x-python
Size: 1532 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20111220/edfdc267/attachment.py>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: runMe.py
Type: text/x-python
Size: 2659 bytes
Desc: not available
URL: <http://lists.osgeo.org/pipermail/qgis-user/attachments/20111220/edfdc267/attachment-0001.py>


More information about the Qgis-user mailing list