WTForms: IntegerField skips coercion when string value is '0'

风格不统一 提交于 2019-12-20 02:43:20

问题


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.


回答1:


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:

if not field.data


来源:https://stackoverflow.com/questions/25335398/wtforms-integerfield-skips-coercion-when-string-value-is-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!