SQLForm not processing everytime values in form are changed

48 views
Skip to first unread message

Mujeeb Farees

unread,
Oct 11, 2017, 3:47:25 AM10/11/17
to web2py-users
I have an sqlform that is not working as expected. I think I have the wrong sequence or something and would like some help with it.

Controller
# Days Form
if session.days:
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')
else:
    days_form
= SQLFORM(db.days, submit_button = 'Save',_id='days_form')
   
if session.offer:
        days_form
.vars.offer_id = session.offer['id']

if days_form.process().accepted:
    response
.flash = 'Days form submitted'
    session.days = days_form.vars.id
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')
elif days_form.errors:
    response
.flash = 'Days form has errors'


Note: On the view, I am just displaying the form and adding a readonly attribute on some of the fields. Also, four sqlforms are loaded on this page, all coming from and going to the same controller. 

Current form behavior is,
1) When I submit this form the first time it saves all values (Great!)
2) Then it redirects to the same form again (Great again!)
3) But now when I change a value and submit the form, it does not update the changes, nor does it display any of the response.flash (Not Cool...)
4) It then redirects to the same form again (Great!)
5) Now when I change any value, the form submits and updates the values in the database. It also shows the Days form submitted response

I need to know why #3 is not working and how to fix it.

Anthony

unread,
Oct 11, 2017, 2:39:51 PM10/11/17
to web2py-users
# Days Form
if session.days:
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')
else:
    days_form
= SQLFORM(db.days, submit_button = 'Save',_id='days_form')
   
if session.offer:
        days_form
.vars.offer_id = session.offer['id']

if days_form.process().accepted:
    response
.flash = 'Days form submitted'
    session.days = days_form.vars.id
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')

The problem is that you are overwriting days_form with a new SQLFORM object after calling .process() on the first instance of the form. You never call .process() on the second instance, so it doesn't get the hidden _formkey input (used for CSRF protection), which means the subsequent form submission fails due to lack of _formkey.

Anthony

Mujeeb Farees

unread,
Oct 16, 2017, 3:18:45 AM10/16/17
to web...@googlegroups.com
Yeah I figured that out. Made the following change and it worked.

Controller
# Days Form
if session.days:
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')
else:
    days_form
= SQLFORM(db.days, submit_button = 'Save',_id='days_form')
   
if session.offer:
        days_form
.vars.offer_id = session.offer['id']

if days_form.process().accepted:
    response
.flash = 'Days form submitted'
    session.days = days_form.vars.id
    days_form
= SQLFORM(db.days,record=session.days,submit_button='Save',showid=False,
                               
_id='days_form')
    days_form.process()
Reply all
Reply to author
Forward
0 new messages