Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WTForms doesn't validate - no errors

Tags:

python

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')

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

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.

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.

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

Any ideas?

like image 915
Ovid Avatar asked Dec 26 '10 14:12

Ovid


Video Answer


1 Answers

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: It would be much better if you gave more explicit information: code examples and version of WTForms.

like image 164
Nikita Hismatov Avatar answered Sep 23 '22 15:09

Nikita Hismatov