wtforms自定义验证器可以使字段可选? [英] Can wtforms custom validator make a field optional?

查看:873
本文介绍了wtforms自定义验证器可以使字段可选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果选中复选框,则使用自定义验证器来检查字段不为空。它检查正确,但无论它总是仍然验证如果该值是一个数字。

基本上我需要一个字段来停止在某些形式的条件下验证。



有没有办法自定义验证器停止验证的字段?是的,自定义验证器可以像内置的可选和必需验证器一样控制验证流程。要控制验证流程,可以使用 StopValidation 异常,不再进行验证。

它会被添加到错误列表中,否则,如果没有消息,则不会再添加错误。



如果您正在使用,请说,IntegerField,FloatField等。你还必须记住在输入强制时间发生的处理错误。可选验证器处理这种情况的方式是,如果输入为空,则清除以前的所有错误。让我们快速看一下wtforms / fields.py中的可选验证器的代码:

  if field.raw_data or isinstance(field.raw_data [0],basestring)而不是field.raw_data [0] .strip():
field.errors [:] = [] $ b $提高StopValidation()

正如您所看到的,如果没有输入或空白输入,错误。



所以,让我们来看看你如何做你的自定义验证。

  from wtforms.validators import StopValidation 
$ b $ def myvalidator(form,field):
if form.some_checkbox_field.data:
#清除处理错误
field.errors [:] = []
#停止进一步验证器运行
提高StopValidation()

然后你可以像这样使用你的验证器:

  from wtforms import BooleanField,IntegerField,For m,validator v 

类SomeForm(Form):
some_checkbox_field = BooleanField('Enable MyNumber')
mynumber = IntegerField('',[myvalidator,v.NumberRange(那么,如果复选框被选中,它将会被设置为0。验证mynumber是输入的数字。另外,NumberRange验证器将被运行。如果不选中,错误将被清除,StopValidation将阻止NumberRange运行。


I'm using a custom validator to check a field is not empty if a check box is checked. It checks correctly but regardless it always still validating if the value is a number.

Basically I need a field to stop validation under certain conditions of the form.

Is there a way for the custom validator to stop validation on the field?

解决方案

Yes, custom validators can control the validation flow just like the built-in Optional and Required validators. To control the validation flow, you use the StopValidation exception, and no further validation will be done.

If StopValidation is raised with a message, it will be added to the errors list, otherwise if there is no message, no more errors will be added.

If you are using, say, IntegerField, FloatField, etc.. you also have to keep in mind the "processing errors" which occur at input coercion time. The way the Optional validator handles this is it clears all previous errors if the input is empty. Let's just take a quick look at the code for the Optional validator from wtforms/fields.py:

if not field.raw_data or isinstance(field.raw_data[0], basestring) and not field.raw_data[0].strip():
    field.errors[:] = []
    raise StopValidation()

As you can see one of the things it does if there is no input or blank input, is it will clear out any previous errors.

So, let's come up with how you could do your custom validator.

from wtforms.validators import StopValidation

def myvalidator(form, field):
    if not form.some_checkbox_field.data:
        # clear out processing errors
        field.errors[:] = []
        # Stop further validators running
        raise StopValidation()

You could then use your validator like such:

from wtforms import BooleanField, IntegerField, Form, validators as v

class SomeForm(Form):
    some_checkbox_field = BooleanField('Enable MyNumber')
    mynumber = IntegerField('', [myvalidator, v.NumberRange(min=5, max=50)])

So then, if the checkbox is checked, it will validate that mynumber was a number as inputted. In addition, the NumberRange validator will be run. If not checked, errors will be cleared, and the StopValidation will prevent the NumberRange from running.

这篇关于wtforms自定义验证器可以使字段可选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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