Hey,
There are quite a few bugs in SQLFORM when having a field of type 'list' (so
'list:string', 'list:integer'). First one being when you submit and form and
there are error's with other fields.
def error_form():
form = SQLFORM(
Field('string', 'string'),
Field('list', 'list:string'))
if form.accepts(request.vars, session,
onvalidation=_force_string_error):
pass
return dict(form=form)
def _force_string_error(form):
form.errors.string = "blah blah"
Submitting this form with list being one entry equal to "abc" (so one box, no
empty box after) will cause on return "abc" being iterated and each value going
into its own box (so three boxes for list on the form).
I do have a solution for this, it involves modifying gluon/sqlhtml.py (fixes
involve latest web2py download). The following code can be added after line
1061:
for fieldname in self.vars:
field = self.table[fieldname]
value = self.vars[fieldname]
if field.type.startswith('list:') and not isinstance(field, (tuple, list)):
self.vars[fieldname] = value and [value] or []
Or in the controller we could have the following:
def error_form():
form = SQLFORM(
Field('string', 'string'),
Field('list', 'list:string'))
if form.accepts(request.vars, session,
onvalidation=_force_string_error):
pass
elif form.errors:
if not isinstance(form.vars['list'], (tuple,
list)):
form.vars['list'] =
form.vars['list'] and [form.vars['list']] or []
return dict(form=form)
The second bug is you cannot set form.errors for a field with type list. I do
not have a web2py fix for this but I do the following:
def error_form():
form = SQLFORM(
Field('string', 'string'),
Field('list', 'list:string'))
if form.accepts(request.vars, session,
onvalidation=[_force_string_error, _force_list_error]):
pass
elif form.errors:
if not isinstance(form.vars['list'], (tuple,
list)):
form.vars['list'] =
form.vars['list'] and [form.vars['list']] or []
if form.errors.haskey('list'):
form.element(_id='no_table_list__row')[1].append(DIV(form.errors.list,
_class='error'))
return dict(form=form)
def _force_list_error(form):
form.errors.list = "blah blah"
Looking to see what Massimo thinks, and also what the rest of the community
think and/or if you have encountered this issue yourself.
-Eric
This communication, including any attachments, does not necessarily represent
official policy of Seccuris Inc.
Please see http://www.seccuris.com/Contact-PrivacyPolicy.htm for further
details about Seccuris Inc.'s Privacy Policy.
If you have received this communication in error, please notify Seccuris Inc.
at [email protected] or at 1-866-644-8442.