How to increase the number of open forms that can be submitted (from 10) ?

88 views
Skip to first unread message

Robert Pates

unread,
Apr 28, 2016, 11:23:14 AM4/28/16
to web2py-users
I have inherited a very nice clinical trials randomization module written in web2py (2.9.5)  and have been asked to modify it to accommodate a new clinical study.
The application features a screen to display all the randomization strata in a list on the screen -- each stratum generates a form using SQLFORM.factory and these are written to a python dictionary and processed in a loop:
if form.process(formname='form1').accepted:

Up to this point the clinical studies have had few strata (< 10).  This latest study, however, has N=16 strata.  Everything looked fine until I checked the functioning of all the 16 forms and found that some worked (i.e. were accepted) and some didn't.  Then, after debugging into html.py (accepts function) I noticed that there were only 10 formkeys represented in the session at any one time.  Then, after much ado I found this post:

http://comments.gmane.org/gmane.comp.python.web2py/129006

in which Anthony explains that, indeed the formkey list in the session holds 10 rotating entries.

I'm wondering what my best options are to address this issue -- I'm somewhat at a loss.  Another post I found indicated that the following snippet from html.py accepts function generates the formkey list in the session:

session[keyname] = list(session.get(keyname,[]))[-9:] + [formkey]


So just for fun I altered the "-9" to "-15" and now everything appears to work fine.

I have trouble believing that tampering with web2py code is a reasonable approach -- is this change likely to break web2py?  Why is the limit set at 10 entries by default?  By now you have probably gathered I am new to web2py.  Any comments would be welcome.

Thanks --

Anthony

unread,
Apr 28, 2016, 11:39:43 AM4/28/16
to web2py-users
Are you saying you have multiple forms on the same page? The allowance of multiple formkeys for the same form was not really intended for the case of multiple forms on the same page -- rather, it was intended more for the case where the user might open up the same page in multiple browser tabs and end up submitting the form on some tab other than the last one loaded. When you have multiple distinct forms (even if they happen to contain the same set of fields), the proper approach is to use different formnames for each form (formkeys are unique to each formname, so you can have as many as you want).

Anthony

Leonel Câmara

unread,
Apr 28, 2016, 11:44:34 AM4/28/16
to web2py-users
I would probably change the way the application works. There is absolutely no need for you to have all the 16 forms in the HTML at the same time and then try to process each one in the controller to see which was submitted. Just load the form you need when you need it using ajax. 

That said web2py shouldn't have magic numbers in the middle of html.py that's just bad.

Anthony

unread,
Apr 28, 2016, 11:56:59 AM4/28/16
to web2py-users
On Thursday, April 28, 2016 at 11:44:34 AM UTC-4, Leonel Câmara wrote:
I would probably change the way the application works. There is absolutely no need for you to have all the 16 forms in the HTML at the same time and then try to process each one in the controller to see which was submitted. Just load the form you need when you need it using ajax. 

Note, if it's multiple copies of the same form, each form will still need a unique name in order to generate unique formkeys in the session.

Anthony

Robert Pates

unread,
Jun 10, 2016, 5:54:51 PM6/10/16
to web2py-users
Well, Thanks Anthony and Leonel for excellent suggestions --
The issue went away when I ensure that each of the 16 forms generated had a unique formname value.
However, I spent some time trying to combine this with Leonel's suggestion to use ajax (with LOAD= ?).  But didn't manage to figure that out in a reasonable amount of time.  If either of you could point me to the best (mdipierro) web2py example appliance(s) to study to be able to examine a complete application I'd be (even more) grateful --

Massimo Di Pierro

unread,
Jun 18, 2016, 8:57:23 AM6/18/16
to web2py-users
As suggested it is critical that multiple forms on the same name have different formname(s) and this is true even if the forms are LOADed because of the CSRF protection mechanism. Anyway, this one one case where you are looking for the wrong solution to the problem. You should use JS to generate those forms and submit them via ajax to different controller.

Robert Pates

unread,
Sep 12, 2016, 12:00:00 PM9/12/16
to web2py-users
Thanks for the comment, Massimo -- Can you point me to any coded examples of this technique?
Reply all
Reply to author
Forward
0 new messages