From 8be0dfa2bb12b2da69097d8b5a0764b00e584f2c Mon Sep 17 00:00:00 2001 From: Ferry Boender Date: Sun, 26 Jul 2015 08:37:05 +0200 Subject: [PATCH] If a field is not required, an empty value is acceptable. --- src/formdefinition.py | 14 ++++++++++++-- test/test.py | 2 +- test/test_formdefinition_validate.json | 1 + 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/formdefinition.py b/src/formdefinition.py index 03ec5d7..377f95a 100644 --- a/src/formdefinition.py +++ b/src/formdefinition.py @@ -110,6 +110,8 @@ class FormDefinition(object): maxlen = field_def.get('maxlen', None) minlen = field_def.get('minlen', None) + if value == '' and field_def.get('required', False) is False: + return '' if minlen is not None and len(value) < int(minlen): raise ValidationError("Minimum length is {0}".format(minlen)) if maxlen is not None and len(value) > int(maxlen): @@ -125,11 +127,12 @@ class FormDefinition(object): maxval = field_def.get('max', None) minval = field_def.get('min', None) + if value == '' and field_def.get('required', False) is False: + return '' try: value = int(value) except ValueError: raise ValidationError("Must be an integer number") - if minval is not None and value < int(minval): raise ValidationError("Minimum value is {0}".format(minval)) if maxval is not None and value > int(maxval): @@ -145,11 +148,12 @@ class FormDefinition(object): maxval = field_def.get('max', None) minval = field_def.get('min', None) + if value == '' and field_def.get('required', False) is False: + return '' try: value = float(value) except ValueError: raise ValidationError("Must be an real (float) number") - if minval is not None and value < float(minval): raise ValidationError("Minimum value is {0}".format(minval)) if maxval is not None and value > float(maxval): @@ -165,6 +169,8 @@ class FormDefinition(object): maxval = field_def.get('max', None) minval = field_def.get('min', None) + if value == '' and field_def.get('required', False) is False: + return '' try: value = datetime.datetime.strptime(value, '%Y-%m-%d').date() except ValueError: @@ -217,6 +223,8 @@ class FormDefinition(object): minlen = field_def.get('minlen', None) maxlen = field_def.get('maxlen', None) + if value == '' and field_def.get('required', False) is False: + return '' if minlen is not None and len(value) < int(minlen): raise ValidationError("Minimum length is {0}".format(minlen)) @@ -232,6 +240,8 @@ class FormDefinition(object): value = form_values[field_def['name']] minlen = field_def.get('minlen', None) + if value == '' and field_def.get('required', False) is False: + return '' if minlen is not None and len(value) < int(minlen): raise ValidationError("Minimum length is {0}".format(minlen)) diff --git a/test/test.py b/test/test.py index 557690a..874d5c2 100644 --- a/test/test.py +++ b/test/test.py @@ -253,7 +253,7 @@ class FormDefinitionTest(unittest.TestCase): form_values = {} errors, values = fd.validate(form_values) self.assertIn('val_file', errors) - self.assertIn('Invalid', errors['val_file'][0]) + self.assertIn('required', errors['val_file'][0]) def testValidateFileMissingFileName(self): fd = self.fc.get_form_def('test_val_file') diff --git a/test/test_formdefinition_validate.json b/test/test_formdefinition_validate.json index 7acc958..e25e33c 100644 --- a/test/test_formdefinition_validate.json +++ b/test/test_formdefinition_validate.json @@ -143,6 +143,7 @@ { "name": "val_file", "title": "field", + "required": true, "type": "file" } ]