Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Ticket #1032 (closed defect: invalid)

Opened 13 years ago

Last modified 12 years ago

TypeError on Form Validation

Reported by: L33tminion Owned by: anonymous
Priority: normal Milestone:
Component: TG Widgets Version: 0.9a6
Severity: normal Keywords:


I encounter this bug anytime I try to validate a form (using @validate(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
  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.
  2. The function checks to see if value is 0, in which case it validates. Then, it checks to make sure it's not false.
  3. 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().
  4. 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.
  2. If that function is supposed to use a FieldStorage, make sure that value.list is set to something sensible.

Versions of stuff:

(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.)

Change History

comment:1 Changed 13 years ago by L33tminion

  • Status changed from new to closed
  • Resolution set to invalid

As it turns out, only a small part of this is a bug and the rest of it is me not understanding how validators work. As a result, I'm marking this as invalid and submitting a new, less confusing ticket for the actual problem.

comment:2 Changed 13 years ago by L33tminion

More sensical version of ticket created as #1039.

Note: See TracTickets for help on using tickets.