【问题标题】:IntegerField and required validator unintuitive? Always required?IntegerField 和所需的验证器不直观?总是需要?
【发布时间】:2014-02-22 10:26:44
【问题描述】:

当我使用 wtforms.IntegerField 和 required 验证器时,我只会收到“此字段是必需的”消息。

另一方面,IntegerField 似乎只能是必需的,我不应该使用必需的验证器。

这里的逻辑是什么?这个字段只是添加了一个is整数验证器,它是否仍然可以用作必填字段(但必须是int)或非必填字段?

只是想在这里发现一些模式。

更新:

  • WTForms 版本为__version__ = '1.0.5'
  • Flask_WTF 版本为__version__ = '0.9.3'

表格

class OrderForm(Form):
    order_number = wtforms.IntegerField('Order Number', validators=[validators.Required()])

查看

    def render_response(self):
        ctx = {}
        order_form = OrderForm()
        ctx['order_form'] = order_form

        if order_form.validate_on_submit(): pass
            # stuff.
        return render_template('support/warranty_form.html', **ctx)

模板

{% macro render_field(field) %}
    <div class="form-field-wrapper">
        <div class="form-field">
            {{ field(**kwargs) }}
        </div>
        {{ render_field_errors(field) }}
    </div>
{% endmacro %}

{{ render_field(order_form.order_number, placeholder="ORDER NUMBER") }}

如果我删除了所需的验证器,我的表单仍然需要该值。

我刚刚在文本字段上使用了自定义验证器。

【问题讨论】:

  • 我想我应该开始为每个项目构建自己的验证器库。已经这样做了。
  • 您能否发布您正在使用的表单的代码、您要验证的输入以及您正在使用的 wtforms 的版本?
  • @MarkHildrethd,完成!

标签: python wtforms flask-wtforms


【解决方案1】:

wtforms Issue #14 你可能会感兴趣。用户希望尝试创建一个“NullableIntegerField”。一个想法是使用optional validator。你可以在下面的脚本中看到它是如何工作的:

from werkzeug import MultiDict
from wtforms import Form, IntegerField, validators

class MyDefaultForm(Form):
    my_field = IntegerField('My Field')

class MyRequiredForm(Form):
    my_field = IntegerField('My Field', [validators.required()])

class MyOptionalForm(Form):
    my_field = IntegerField('My Field', [validators.optional()])

input_data = MultiDict({
    'my_field' : ''
})

default_form = MyDefaultForm(input_data)
required_form = MyRequiredForm(input_data)
optional_form = MyOptionalForm(input_data)

print 'Default Form Validation: {0}'.format(default_form.validate())
print 'Default Form Errors: {0}'.format(default_form.errors)
print 'Default Data: {0}'.format(default_form.data)
print

print 'Required Form Validation: {0}'.format(required_form.validate())
print 'Required Form Errors: {0}'.format(required_form.errors)
print 'Required Data: {0}'.format(required_form.data)
print

print 'Optional Form Validation: {0}'.format(optional_form.validate())
print 'Optional Form Errors: {0}'.format(optional_form.errors)
print 'Optional Data: {0}'.format(optional_form.data)
print

结果:

Default Form Validation: False
Default Form Errors: {'my_field': [u'Not a valid integer value']}
Default Data: {'my_field': None}

Required Form Validation: False
Required Form Errors: {'my_field': [u'This field is required.']}
Required Data: {'my_field': None}

Optional Form Validation: True
Optional Form Errors: {}
Optional Data: {'my_field': None}

我创建了三个带有整数字段的表单。一个没有验证器,一个有必需的验证器,一个有可选的验证器。您可能会想要使用可选的验证器,以便结果为 None。

【讨论】:

  • 给我上色愚蠢,有一个可选的验证器!我做了一些假设,因为文本字段默认是可选的。谢谢马克!非常感谢您的宝贵时间!
猜你喜欢
  • 1970-01-01
  • 2021-06-29
  • 2012-01-28
  • 1970-01-01
  • 2019-10-10
  • 2020-10-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多