Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do filters work in wtforms?

Tags:

wtforms

I'm reading http://pythonhosted.org/WTForms/ but nowhere does it define the interface to a filter function, as in the filters= keyword argument to the Field() constructor. Elsewhere it says that the .filter attribute is a list of callables. So what is the calling sequence? Does it take one argument and return the filtered value? Should it raise some exception if the argument isn't valid, and if so, what exception(s)?

like image 881
user3830629 Avatar asked Jul 11 '14 19:07

user3830629


1 Answers

Basically,

class MyForm(Form):
    myfield = wtf.TextField('My field', filters=[lambda x: x])

The filter argument is a sequence (tuple or list) of callables accepting the input value of the field and returns the transformed value. You can chain multiple filters sequentially.

Note that the filter shown in the example above does nothing: it returns the raw input value.

like image 131
Damien Avatar answered Dec 31 '22 14:12

Damien