I'm trying to add a checkbox that must be ticked to an update SQLFORM,
but I'm getting an internal error.
Here's the create form that works fine with an added checkbox:
form = SQLFORM(db.job_post, submit_button='Post Job',
formstyle='table2cols',
fields=['poster_name', 'poster_email', 'poster_phone',
'zipcode', 'job_type', 'job_title', 'job_description'],
_id='postjob',
_class='form2col'
)
form[0].insert(-2, TR(SPAN(LABEL('I agree to the ', A('terms of
service', _href=URL('terms'))), INPUT(_name='terms', _value='agree',
_type='checkbox', _style="margin: 0px 0px 4px 14px")))) # add
checkbox
def validateTerms(form):
if form.vars.terms != 'agree':
form.errors.terms = 'You must agree to the terms.'
if form.accepts(request.vars, session, onvalidation={'onsuccess':
validateTerms, 'onfailure': validateTerms}):
# bla bla
Now on another page here's virtually the same thing but an update
form:
form = SQLFORM(db.job_post, job.id, submit_button='Update Job',
formstyle='table2cols',
fields=['poster_name', 'poster_email', 'poster_phone',
'zipcode', 'job_type', 'job_title', 'job_description'],
_id='postjob',
_class='form2col'
)
form[0].insert(-2, TR(SPAN(LABEL('I agree to the ', A('terms
of service', _href=URL('terms'))), INPUT(_name='terms',
_value='agree', _type='checkbox', _style="margin: 0px 0px 4px
14px")))) # add checkbox
def validateTerms(form):
if form.vars.terms != 'agree':
form.errors.terms = 'You must agree to the terms.'
if form.accepts(request.vars, session,
onvalidation={'onsuccess': validateTerms, 'onfailure':
validateTerms}):
# bla bla
And here's the ticket I get with this second form:
Traceback (most recent call last):
File "/home/www-data/web2py/gluon/restricted.py", line 188, in
restricted
exec ccode in environment
File "/home/www-data/web2py/applications/staging/controllers/
default.py", line 329, in <module>
File "/home/www-data/web2py/gluon/globals.py", line 124, in <lambda>
self._caller = lambda f: f()
File "/home/www-data/web2py/applications/staging/controllers/
default.py", line 119, in post_edit
if form.accepts(request.vars, session, onvalidation={'onsuccess':
validateTerms, 'onfailure': validateTerms}):
File "/home/www-data/web2py/gluon/sqlhtml.py", line 1042, in accepts
if self.table[key].type == 'upload' \
File "/home/www-data/web2py/gluon/dal.py", line 4320, in __getitem__
return dict.__getitem__(self, str(key))
KeyError: 'terms'
Looking at sqlhtml.py line 1042, I see there's a check done for update
forms that involves looking for every entry in self.errors in
self.table, which of course won't be found here because my checkbox is
not part of the table.
If I comment out the assignment to form.errors.terms, I don't get this
error, but of course then I don't get the validation I want. Also, I
strangely then get a 'no data' error message for the first field when
I visit the page but no such error when I submit the form. If I remove
the onvalidation arg from accepts, this error goes away entirely.
So any ideas? Am I doing things wrong or is there some bug here?