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

查看:18
本文介绍了Flask WTForms:DataRequired 和 InputRequired 之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

wtforms.valiadators

我的注册表中有一些字段:

I have some fields in my signup form :

username
password 
password_repeat 
submit

这些字段应该使用 DataRequired 还是 InputRequired 验证器?

Should these fields use the DataRequired or InputRequired validator?

推荐答案

简答

除非你有充分的理由,否则你应该使用 InputRequired

让我们看一下 docs/code forDataRequired() :

Lets look at some notes from the docs/code for DataRequired() :

注意 this 和 DataRequired 之间的区别在于 InputRequired 看起来是提供了表单输入数据,而 DataRequired 着眼于后强制数据.

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.

注意 这个验证器曾经被称为 Required 但它的行为方式(需要强制数据,而不是输入数据)意味着它的运行方式与Optional 验证器,此外还会引起与某些字段的混淆,这些字段将数据强制为falsey"值,例如 0Decimal(0)time(0) 等.除非存在非常具体的原因,否则我们建议使用 :class: InputRequired 代替.

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.

这是什么意思?

Form 类中,您会注意到两个关键字参数formdatadata.这些通常对应于 processprocess_formdata 两种方法.当表单数据离线时,其格式并不总是对应于 Field 类型.一个很好的例子是提供给 IntegerField 的值 u'1'.如果您有一个 NumberRange 验证器,这将是个坏消息,因为 u'1' 不是数字.

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.

process_formdata 方法的主要目的是通过在运行验证规则之前将值强制转换为其正确类型来防止这种情况.这就是他们所说的查看强制后数据"

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"

问题所在!

InputRequiredDataRequired 以相同的方式工作,特别是 __call__ 实现:

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

某些字段类型将数据强制转换为 Falsey 值(0、Decimal(0) 等).当您有一个 IntegerField 并且表单提交一个类似于 '0' 的值时,就会出现问题.如果您将 DataRequired 应用于此,它将无法通过验证.这是因为 DataRequired 将在强制转换后评估 if not field.data... 其中 field.dataFalsey 数值 0.

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.

这篇关于Flask WTForms:DataRequired 和 InputRequired 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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