简体   繁体   中英

Something wrong with wtforms FieldList && validation

Something wrong with wtforms FieldList && validation... It should say that field must have Int value, not This field is required Why f.data has [None, 2, None] value, not ['def', 2, 'abc'] ?

from webob.multidict import MultiDict

from wtforms import Form
from wtforms import FieldList, IntegerField
from wtforms import validators

class SearchForm(Form):
    locality_id = FieldList(IntegerField(u'Locality', [validators.Required()]))


d = MultiDict([('locality_id-0', 'def'), ('locality_id-1', 2), ('locality_id-2', 'abc')])

f = SearchForm(d)

print f.validate()
print f.errors
print f.data
print f.locality_id.data

% python form_test.py

False

{'locality_id': [[u'This field is required.'], [u'This field is required.']]}

{'locality_id': [None, 2, None]}

[None, 2, None]

It looks like there is a try... except block in the IntegerField ancestry which will place all non ints into the process_errors property and that the class is specifically prevented from allowing you to have data populated with anything but valid data. I believe you can still get the values you seek in the raw_data property, however.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM