WTForms 不验证 - 没有错误 [英] WTForms doesn't validate - no errors

查看:43
本文介绍了WTForms 不验证 - 没有错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 WTForms 库时遇到了一个奇怪的问题.对于测试,我创建了一个带有单个字段的表单:

I got a strange problem with the WTForms library. For tests I created a form with a single field:

class ArticleForm(Form):
    content = TextField('Content')

它接收一个简单的字符串作为内容,现在我使用 form.validate() 并且它出于任何原因返回 False.

It receives a simple string as content and now I use form.validate() and it returns False for any reason.

我查看了 'FormField 对象的 validate() 方法.我发现如果错误列表的长度为零,则该字段返回 true.这对于我的测试是正确的,因为我没有收到任何错误.在 shell 中,我的字段验证按预期返回 True.

I looked into the validate() methods of the 'Form and Field object. I found out that the field returns true if the length of the errorlist is zero. This is true for my test as i don't get any errors. In the shell the validation of my field returns True as expected.

Form 对象中的 validate() 方法只是在字段上运行并调用它们的 validate() 方法,并且只有在字段之一被验证为时才返回 false假的.

The validate() methode in the Form object just runs over the fields and calls their validate() method and only returns false if one of the fields is validated as false.

因此,由于我的字段经过验证且没有任何错误,我在代码中看不到任何原因 form.validate() 返回 False.

So as my Field is validated without any error i can't see any reason in the code why form.validate() returns False.

有什么想法吗?

推荐答案

在我看来,您只是向表单传递了错误的值.这是你需要使用这样的形式:

It seems to me, you just pass wrong values to your form. This is what you need to use such form:

from wtforms import Form, TextField # This is wtforms 0.6

class DummyPostData(dict):
    """
    The form wants the getlist method - no problem.
    """
    def getlist(self, key):
        v = self[key]
        if not isinstance(v, (list, tuple)):
            v = [v]
        return v

class ArticleForm(Form):
    content = TextField('Content')

form = ArticleForm(DummyPostData({'content' : 'my content' }))
print form.validate()
#$ python ./wtf.py 
#True

ps:如果您提供更明确的信息会更好:代码示例和 WTForms 版本.

ps: It would be much better if you gave more explicit information: code examples and version of WTForms.

这篇关于WTForms 不验证 - 没有错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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