form wizard all steps in one page

551 views
Skip to first unread message

aetag...@gmail.com

unread,
Dec 29, 2015, 1:13:04 PM12/29/15
to web2py-users
I am forever having trouble with making the form wizard show all the steps in one page, all it shows now is the last batch of form questions instead of the previous batches and although the database gets updated with the entry, a file that is supposed to be uploaded into the database isn't.

When I view the page, while in the terminal I am able to see all steps 0-8 with fields showing.. but it in the actual page view it just shows only the last batch of form fields in the last step. Why?



    step = 0
    for i in STEPS:
        if step == 0:
            session.myformt = {}
        fields=STEPS[i]
        print "Fields:" + str(fields) + "Trip Day" + str(step)
        if isinstance(fields,tuple):
            form = SQLFORM.factory(*[f for f in db.myform if f.name in fields])
        step = step+1
    if form.accepts(request,session):
            session.myform.update(form.vars)
            db.myform.insert(**session.myform)
            session.flash='Thanks! Form submitted.'
            redirect(URL('myformlist'))
    elif form.errors:
        response.flash='Please correct errors!'
    return dict(form=form)

Anthony

unread,
Dec 29, 2015, 1:48:16 PM12/29/15
to web2py-users

    step = 0
    for i in STEPS:
        if step == 0:
            session.myformt = {}
        fields=STEPS[i]
        print "Fields:" + str(fields) + "Trip Day" + str(step)
        if isinstance(fields,tuple):
            form = SQLFORM.factory(*[f for f in db.myform if f.name in fields])

You loop over STEPS but keep overwriting "form" on each iteration, so at the end of the loop, "form" is just the form associated with the last step.

Anyway, if you want all the steps on a single HTML page, don't do it this way (SQLFORM.factory will generate a separate form for each step, which you do not want). Just create a single form with multiple fieldsets, and show/hide the fieldsets as you go from step to step (this is what jQuery Steps does). You will have to create custom form markup in the template (or create a custom formstyle function), as the built-in formstyles for SQLFORM will not generate the HTML markup you need.

In short, in the web2py controller, just create a single standard SQLFORM using all of the fields. Then in the view, generate markup like you see here: http://www.jquery-steps.com/Examples#advanced-form. See http://web2py.com/books/default/chapter/29/07/forms-and-validators#Custom-forms for details on generating the custom form markup.

Anthony

aetag...@gmail.com

unread,
Dec 30, 2015, 10:39:58 AM12/30/15
to web2py-users
Thank you for your responses, I finally figured it out. You are very helpful!
Reply all
Reply to author
Forward
0 new messages