If a field is not required, an empty value is acceptable.

pull/7/head
Ferry Boender 9 years ago
parent ea0cf1613e
commit 8be0dfa2bb
  1. 14
      src/formdefinition.py
  2. 2
      test/test.py
  3. 1
      test/test_formdefinition_validate.json

@ -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))

@ -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')

@ -143,6 +143,7 @@
{
"name": "val_file",
"title": "field",
"required": true,
"type": "file"
}
]

Loading…
Cancel
Save