本文介绍了WTForms:当字符串值为'0'时,IntegerField跳过强制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几乎涵盖了我在这里之后,但不完全。

当字符串值为'0'时,IntegerField好像跳过了强制。

下面是一个'0'的例子:

从webob.multidict导入MultiDict
$ b $ class TestForm(Form):
num = IntegerField('How ?',[validators.DataRequired('num required。'),validators.NumberRange(min = 0,max = 100)])

data_in = {'num':'0'}# '0'是一个字符串,因为会被POST
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)

这个输出是:

  HTML Render:< input id =num name =numtype =textvalue =0> 
Validate:False
错误:{'num':['num required。']}

另外,使用'66'的例子:

 来自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'}#注意'66'是一个字符串,因为会被POST
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)

p>

  HTML Render:< input id =numname =numtype =textvalue =66> 
验证:真
错误:{bb


是什么给出的?我可以使用InputRequired来保持类型不可知,但是这完全违背了这个目的。

解决方案

恐怕你必须保持这种类型的不可知性,并使用InputRequired: - )

来自说:

被称为Required,但其行为
(要求强制数据,而不是输入数据)的方式意味着它以
的方式工作,这与可选验证器不是对称的,并且导致
与某些字段的混淆它将数据强制转换为falsey值,例如
0,Decimal(0),time(0)等。除非存在一个非常具体的原因
,否则我们推荐使用:class:InputRequired。 p>

实际的代码cuplrit在201行稍微低一点:

 如果不是field.data 


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 waywhich was not symmetric to the Optional validator and furthermore causedconfusion with certain fields which coerced data to 'falsey' values like0, Decimal(0), time(0) etc. Unless a very specific reasonexists, we recommend using the :class:InputRequired instead."

The actual code cuplrit is a little down below at line 201:

if not field.data

这篇关于WTForms:当字符串值为'0'时,IntegerField跳过强制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 10:34