roam-qgis / Roam

Simple data collection built using QGIS.
http://roam-docs.readthedocs.org/en/latest/
GNU General Public License v2.0
171 stars 61 forks source link

Forcing integer widget to require non-zero value before saving form #455

Open she-weeds opened 4 years ago

she-weeds commented 4 years ago

Is it possible to force certain numeric widgets to still be 'Required' if the value is 0 (or the minimum value)? I have a fair number of required numeric fields for non-zero measurements, but because they default to 0 (or the minimum value) it gets interpreted as filled-in, which results in assessors skipping over that field by mistake and a heap of erroneous data.

I've got a clumsy workaround by adding the following code to accept() in form.py:

numfieldlist = ['basal_d_cm','height_m','width_m']
numwidgetlist = [self.boundwidgets[field] for field in numfieldlist]
zerovallist = [w.unformatted_label for w in numwidgetlist if w.value() == 0]
if zerovallist:
    raise utils.MissingValuesException.missing_values(zerovallist)
else:
    return True

This blocks assessors from saving the form with 0 as a value for the fields that shouldn't have that (hardcoded as some numeric fields can have 0s) On attempting to save the form a popup mentions which fields need to be fixed (while staying on the form, which I can't get to happen with the standard FeatureSaveException() as it weirdly flashes back to the main window and then back to the form)

But if I could indicate that those 0-value fields are Required with red text on the form itself while filling it out, rather than finding out only on attempting to save the form, that would be ideal. Related issues raised on github date back to 2015 and haven't helped me figure it out.

Am still testing this on ROAM 2.7.2 to work with current projects in production while waiting for more stable releases of ROAM 3 (thanks to everyone who has been working on that!)