This question almost covers what I am after here, but not quite.
It seems like IntegerField skips coercion when the string value is '0'. Any other positive integer seems to work okay.
Here is an example of '0':
from wtforms import validators, Form, IntegerField
from webob.multidict import MultiDict
class TestForm(Form):
num = IntegerField('How Many?', [validators.DataRequired('num required.'), validators.NumberRange(min=0, max=100)])
data_in = {'num': '0'} # Note '0' is a string as would be POSTed
test_form = TestForm(formdata=MultiDict(data_in))
print("HTML Render: %s" % test_form.num())
print(" Validate: %s" % test_form.validate())
print(" Errors: %s" % test_form.errors)
Output of this is:
HTML Render: <input id="num" name="num" type="text" value="0">
Validate: False
Errors: {'num': ['num required.']}
And alternatively, using an example of '66':
from wtforms import validators, Form, IntegerField
from webob.multidict import MultiDict
class TestForm(Form):
num = IntegerField('How Many?', [validators.DataRequired('num required.'), validators.NumberRange(min=0, max=100)])
data_in = {'num': '66'} # Note '66' is a string as would be POSTed
test_form = TestForm(formdata=MultiDict(data_in))
print("HTML Render: %s" % test_form.num())
print(" Validate: %s" % test_form.validate())
print(" Errors: %s" % test_form.errors)
Out of this is:
HTML Render: <input id="num" name="num" type="text" value="66">
Validate: True
Errors: {}
What gives? I could use InputRequired instead to keep it type agnostic, but that completely defeats the purpose of this.