#1032: TypeError on Form Validation
------------------------+---------------------------------------------------
Reporter: L33tminion | Owner: anonymous
Type: defect | Status: new
Priority: normal | Milestone:
Component: Widgets | Version: 0.9a6
Severity: normal | Keywords:
------------------------+---------------------------------------------------
I encounter this bug anytime I try to validate a form (using
[EMAIL PROTECTED](form=form_var)`).
My code for the form:
{{{
class MyForm(Form):
template = '''<Kid template omitted>'''
params = []
params_doc = {}
class UploadFields(WidgetsList):
upload_file = FileField(validator=NotEmpty)
upload_file.label = ''
form_var=MyForm(fields=UploadFields(), submit_text="Upload")
}}}
Stack trace:
{{{
Page handler: <bound method Root.upload of <cattail.controllers.Root
object at 0xb7d1b16c>>
Traceback (most recent call last):
File "/usr/lib/python2.4/site-
packages/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 105, in
_run
self.main()
File "/usr/lib/python2.4/site-
packages/CherryPy-2.2.1-py2.4.egg/cherrypy/_cphttptools.py", line 254, in
main
body = page_handler(*virtual_path, **self.params)
File "<string>", line 3, in upload
File "/opt/turbogears/turbogears/identity/conditions.py", line 235, in
require
return fn(self, *args, **kwargs)
File "<string>", line 3, in upload
File "/opt/turbogears/turbogears/controllers.py", line 281, in expose
output = database.run_with_transaction(
File "<string>", line 5, in run_with_transaction
File "/opt/turbogears/turbogears/database.py", line 245, in so_rwt
retval = func(*args, **kw)
File "<string>", line 5, in _expose
File "/opt/turbogears/turbogears/controllers.py", line 298, in <lambda>
mapping, fragment, *args, **kw)))
File "/opt/turbogears/turbogears/controllers.py", line 322, in
_execute_func
output = errorhandling.try_call(func, *args, **kw)
File "/opt/turbogears/turbogears/errorhandling.py", line 68, in try_call
return func(self, *args, **kw)
File "<string>", line 3, in upload
File "/opt/turbogears/turbogears/controllers.py", line 127, in validate
kw.update(form.validate(value, state))
File "/opt/turbogears/turbogears/widgets/forms.py", line 1046, in
validate
return self.validator.to_python(value, state)
File "/usr/lib/python2.4/site-
packages/FormEncode-0.5.1-py2.4.egg/formencode/api.py", line 316, in
to_python
value = tp(value, state)
File "/usr/lib/python2.4/site-
packages/FormEncode-0.5.1-py2.4.egg/formencode/schema.py", line 132, in
_to_python
new[name] = validator.to_python(value, state)
File "/usr/lib/python2.4/site-
packages/FormEncode-0.5.1-py2.4.egg/formencode/api.py", line 319, in
to_python
vp(value, state)
File "/usr/lib/python2.4/site-
packages/FormEncode-0.5.1-py2.4.egg/formencode/validators.py", line 420,
in validate_python
if not value:
File "/usr/lib/python2.4/cgi.py", line 625, in __len__
return len(self.keys())
File "/usr/lib/python2.4/cgi.py", line 601, in keys
raise TypeError, "not indexable"
TypeError: not indexable
}}}
As far as I can tell, this is what happens:
1. `NotEmpty.validate_python` is getting passed a `cgi.FieldStorage`
object called `value`.
1. The function checks to see if `value` is 0, in which case it
validates. Then, it checks to make sure it's not false.
1. `if not value:` tries to evaluate `value` as a boolean. This checks
the length of `value`, which in turn checks the length of `value.keys()`.
1. `value.keys()` barfs because `value.list` is not set (it's still
`None`).
Possible fixes:
1. Is that function supposed to be passed a `FieldStorage` object? From
how it acts, it seems that it's supposed to take a string. If so, fix
that.
1. If that function is supposed to use a `FieldStorage`, make sure that
`value.list` is set to something sensible.
Versions of stuff:
* Turbogears- from SVN, r1629
* FormEncode- 0.5.1
* CherryPy- 2.2.1
(Incidentally, I've also encountered a similar bug when checking to make
sure that uploaded_file has been passed to the function that handles my
form. `if not uploaded_file:` will return the same `ValueError` if it's
assigned; it's also a `FieldStorage` with `list` set to `None`.)
(Also, I apologize if this is just me making some stupid mistake. Many
thanks for the help.)
--
Ticket URL: <http://trac.turbogears.org/turbogears/ticket/1032>
TurboGears <http://www.turbogears.org/>
TurboGears front-to-back web development
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"TurboGears Tickets" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/turbogears-tickets
-~----------~----~----~----~------~----~------~--~---