wtforms Issue #14 might interest you. The user was hoping to try to create a "NullableIntegerField". One idea that came was to use the optional validator. You can see how this works in the below script:
from werkzeug import MultiDict
from wtforms import Form, IntegerField, validators
class MyDefaultForm(Form):
my_field = IntegerField('My Field')
class MyRequiredForm(Form):
my_field = IntegerField('My Field', [validators.required()])
class MyOptionalForm(Form):
my_field = IntegerField('My Field', [validators.optional()])
input_data = MultiDict({
'my_field' : ''
})
default_form = MyDefaultForm(input_data)
required_form = MyRequiredForm(input_data)
optional_form = MyOptionalForm(input_data)
print 'Default Form Validation: {0}'.format(default_form.validate())
print 'Default Form Errors: {0}'.format(default_form.errors)
print 'Default Data: {0}'.format(default_form.data)
print
print 'Required Form Validation: {0}'.format(required_form.validate())
print 'Required Form Errors: {0}'.format(required_form.errors)
print 'Required Data: {0}'.format(required_form.data)
print
print 'Optional Form Validation: {0}'.format(optional_form.validate())
print 'Optional Form Errors: {0}'.format(optional_form.errors)
print 'Optional Data: {0}'.format(optional_form.data)
print
The results:
Default Form Validation: False
Default Form Errors: {'my_field': [u'Not a valid integer value']}
Default Data: {'my_field': None}
Required Form Validation: False
Required Form Errors: {'my_field': [u'This field is required.']}
Required Data: {'my_field': None}
Optional Form Validation: True
Optional Form Errors: {}
Optional Data: {'my_field': None}
I create three forms with an integer field. One with no validators, one with the required validator, and one with optional validator. You probably will want to use the optional validator so that the result is None.