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.
I'm afraid you'd have to keep that type agnostic and use InputRequired instead :-)
The docs from here says:
"[...]this validator used to be called Required but the way it behaved (requiring coerced data, not input data) meant it functioned in a way which was not symmetric to the Optional validator and furthermore caused confusion with certain fields which coerced data to 'falsey' values like 0, Decimal(0), time(0) etc. Unless a very specific reason exists, we recommend using the :class:InputRequired instead."
The actual code cuplrit is a little down below at line 201: