[web2py] Problem with multiple forms

391 views
Skip to first unread message

Sverre

unread,
May 6, 2010, 8:19:51 AM5/6/10
to web2py-users

I'm using this design in a controller to have buttons on the page:
----
form_membership = FORM('', INPUT(_type='submit',_value='New'))
form_back = FORM('', INPUT(_type='submit',_value='Back to user'))
if form_back.accepts(request.vars, session):

redirect(URL(r=request,c='sysuser',f='edit_user',args=userid))
if form_membership.accepts(request.vars, session):

redirect(URL(r=request,c='sysuser',f='new_membership',args=userid))
return
dict(msg=rows,form_membership=form_membership,form_back=form_back)
----
But this isn't working. I'm don't get the right redirections. The
"Back to user' button gives me the redirection of the other button.
Has someone a clue?

mdipierro

unread,
May 6, 2010, 9:17:46 AM5/6/10
to web2py-users
If you have two forms you have to name them:

if form_back.accepts(request.vars, session, formname='back'):
...
if form_membership.accepts(request.vars,
session,formname='membership'):

Karthik

unread,
Aug 13, 2011, 9:24:27 PM8/13/11
to mdipierro, web...@googlegroups.com
Is there a way to dynamically name the forms?
I don't know how many forms I'll need to display, it depends on an
external factor.
(I have a textarea and depending on the lines filled in the textarea
by the user, I need to create that many forms).

Is this possible withing web2py's FORM() helper or would using the
default HTML be a better idea?

Thanks!

weheh

unread,
Aug 14, 2011, 12:12:53 AM8/14/11
to web2py-users
Each form needs to have a unique name. I suppose you could use uuid to
make a unique name. Then something like this pseudocode might work:

allforms=dict(form_id=new_form_id(),form=SQLFORM(...))
for f in allforms:
if f['form'].accepts(request.vars,formname=f['form_id']):
# do something if form accepts
else:
# do something else if form doesn't accept
return dict(allforms=allforms)

Then in views you will need to loop on allforms to make your form. You
will probably want a custom form and make sure you have a hidden input
with id=f['form_id']

Obviously, if the order of forms is important, you will want to modify
this code to make the forms into a list instead of a dict.

Hope this helps. Good luck.

Karthik

unread,
Aug 15, 2011, 11:04:42 AM8/15/11
to web2py-users
Thanks weheh, that helped a lot!
Reply all
Reply to author
Forward
0 new messages