烧瓶WTForms:DataRequired和InputRequired之间的区别 [英] Flask WTForms: Difference between DataRequired and InputRequired

查看:2193
本文介绍了烧瓶WTForms:DataRequired和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 会在字段之后强制执行后评估 if field.data ... 。数据 Falsey 数值 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 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.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆