烧瓶WTForms:DataRequired和InputRequired之间的区别 [英] Flask WTForms: Difference between DataRequired and InputRequired
问题描述
DataRequired
和 InputRequired
在 wtforms.valiadators
我在我的申请表单一些字段:
用户名
密码
password_repeat
提交
DataRequired
或 InputRequired
验证器
InputRequired
为什么?
让我们看一下docs / code中的一些注释:
请注意,在InputRequired外观和DataRequired之间有区别,表单输入数据是提供的,而DataRequired则是查看后期强制数据。
和
这个验证器曾经被调用过
必需
,但它的行为方式(要求强制数据,而不是输入数据)意味着它的功能与可选
验证器,并进一步引起混淆某些领域,强制数据为假的值,如0
,十进制(0)
,时间(0)
等。除非一个非常具体的原因存在,我们建议使用:类:InputRequired
这意味着什么?
在表格
类,你会注意到两个关键字参数 FORMDATA
和数据
。这些通常对应于两个方法 process
和 process_formdata
。当表单数据进入线路时,并不总是以 Field
类型对应的格式。的一个很好的例子是 U'1’
被供应到一个 IntegerField
的值。如果你有一个 NumberRange
验证器,这将是个坏消息,因为 u'1'
不是一个数字。 p>
process_formdata
方法的主要目的是通过在运行验证之前将值强制为正确的类型来防止这种情况规则。这就是他们指的是,当他们说什么的 着眼于胁迫后的数据的
的问题!
$ b $ InputRequired
和 DataRequired
以同样的方式工作特别是 __ call __
的实现:
$ p $ def $ _call __(self,form,field ):
如果不是field.data或isinstance(field.data,string_types)而不是field.data.strip():
如果self.message是None:
message = field.gettext ( '此字段是必需的。')
,否则:
消息= self.message
某些字段类型将数据强制转换为Falsey 值(0,Decimal(0)等)。当你有一个 IntegerField
并且表单提交一个值如'0'
时,就会出现这个问题。如果您应用 DataRequired
,则验证失败。这是因为 DataRequired
会在字段之后强制执行后评估
是 Falsey 数值 if field.data ...
。数据 0
What is difference between DataRequired
and InputRequired
in wtforms.valiadators
I have some fields in my signup form :
username
password
password_repeat
submit
Should these fields use the DataRequired
or InputRequired
validator?
Short Answer
Unless you have a good reason you should use InputRequired
Why?
Lets look at some notes from the docs/code:
Note there is a distinction between this and DataRequired in that InputRequired looks that form-input data was provided, and DataRequired looks at the post-coercion data.
and
NOTE 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 theOptional
validator and furthermore caused confusion with certain fields which coerced data to 'falsey' values like0
,Decimal(0)
,time(0)
etc. Unless a very specific reason exists, we recommend using the :class:InputRequired
instead.
what does this mean?
In the Form
class you will notice two keyword arguments formdata
and data
. These generally correspond with two methods process
and process_formdata
. When form data comes in off the wire its not always in the format corresponding to the Field
type. A good example of this is the value u'1'
being supplied to an IntegerField
. This would be bad news if you had a NumberRange
validator because u'1'
isn't a number.
The primary purpose of the process_formdata
method is to prevent this situation by coercing the value into its correct type prior to running validation rules. That is what they are referring to when they say "looks at the post-coercion data"
the problem!
Both InputRequired
and DataRequired
work the same way specifically the __call__
implementations:
def __call__(self, form, field):
if not field.data or isinstance(field.data, string_types) and not field.data.strip():
if self.message is None:
message = field.gettext('This field is required.')
else:
message = self.message
Certain field types coerce data into Falsey values(0, Decimal(0), etc.). The problem occurs when you have an IntegerField
and the form submits a value like '0'
. If you apply DataRequired
to this it will fail validation. This is because DataRequired
will evaluate if not field.data...
after coercion where field.data
is the Falsey numeric value 0
.
这篇关于烧瓶WTForms:DataRequired和InputRequired之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!