Hi all,<div><br></div><div>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.</div><div><br></div>
<div>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.</div>
<div><br></div><div>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.</div>
<div><br></div><div>The functions use the following syntax: validate{FieldName} or validate{FieldIndex} for naming (plus some other styles) e.g</div><div><br></div><div>def validateName():</div><div> if value == "":</div>
<div> return (False,"Name can't be NULL")</div><div> return True</div><div><br></div><div>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.</div>
<div><br></div><div>Overview of logic:</div><div><ol><li>Define Python file with rules</li><li>Tell QGIS to use the rules file for the layer.</li><li>Open attribute form</li><li>Edit some values</li><li>Press OK</li><li>
QGIS calls each function in the rules file to validate each field that is defined with a rule</li>
<li>If there are errors: cancel the accept() action and show the errors. If there are no errors go though like normal.</li></ol><div>My thoughts for using Python are:</div></div><div><ul><li>Easy to read and write</li><li>
Not limited to what we have in QgsExpression and easier to expand.</li><li>Can add documentation to validation rules.</li><li>Can add more complex validation rules e.g. regex, parsing, database checks etc</li></ul><div>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.</div>
<div><br></div><div>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.</div>
</div><div><br></div><div>What love to get peoples thoughts about this, users and devs a like.</div><div><br></div><div>- Nathan </div><div><br></div><div><br></div><div><br></div>