Create SQLFORM having one submit button for 2 distinct tables

48 views
Skip to first unread message

Mohit Jain

unread,
May 22, 2016, 2:01:27 PM5/22/16
to web2py-users
Hello,
    I have 2 tables (ApplicationDeadline & NominationDeadline). Both of them have only 2 fields (start-time and end-time). I would like to create a form where the user can see 4 fields and only one submit button that can be used to insert new values of these fields. Only the fields that have been filled by the user must be inserted (blank fields must not have new insertions). This is somewhat similar to what is given in the manual (http://web2py.com/books/default/chapter/29/07/forms-and-validators#One-form-for-multiple-tables) however, I don't have a common foreign-key (like the client-id of the given example) and hence would like to know how to do such a thing, if at all possible.

models/db.py
#######################################
# Set the Application Period Deadline.#
#######################################
db
.define_table(
   
'AppDeadline',
   
Field('start','datetime',required=True, label='Start Time'),
   
Field('end','datetime',required=True, label='End Time')
   
)


#######################################
# Set the Nomination Period Deadline.#
#######################################
db
.define_table(
   
'NomDeadline',
   
Field('start','datetime',required=True, label='Start Time'),
   
Field('end','datetime',required=True, label='End Time')
   
)

What I want is something like, 

form = SQLFORM.factory(db.AppDeadline, db.NomDeadline)
if form.process().accepted:
   
if form.vars.appTime: #To check if application-fields have been updated
        response
.flash = 'Application Period Set : '+str(form.vars.start)+' to '+str(form.vars.end)
        db
.UserLogs.insert(activity='Application Period Set : '+str(form.vars.start)+' to '+str(form.vars.end))
   
if form.vars.nomTime: #To check if nomination-fields have been updated
        response
.flash = 'Nomination Period Set : '+str(form.vars.start)+' to '+str(form.vars.end)
        db
.UserLogs.insert(activity='Nomination Period Set : '+str(form.vars.start)+' to '+str(form.vars.end))
elif form.errors:
    response
.flash = 'Form has error(s)!'


Regards,
Mohit

Anthony

unread,
May 22, 2016, 10:34:01 PM5/22/16
to web2py-users
It's an extra hassle to have multiple fields with the same name in a given form because you get back a list for each common name. Instead, you're better off just creating a custom form via SQLFORM.factory, naming each field uniquely. Then do the inserts with the form.vars values after validation.

Anthony

Mohit Jain

unread,
May 23, 2016, 5:27:28 AM5/23/16
to web2py-users
Thanks Anthony for your suggestion! Works just fine :)

For anyone looking at a similar problem, here's what I've done now.

    form = SQLFORM.factory(
                Field('appStart','datetime',label='App-Period Start Time'),
                Field('appEnd','datetime',label='App-Period End Time'),
                Field('nomStart','datetime',label='Nom-Period Start Time'),
                Field('nomEnd','datetime',label='Nom-Period End Time')
                                   )   

    if form.process().accepted:
        app_msg_flash = 'Application Period Unchanged'
        nom_msg_flash = 'Nomination Period Unchanged'
        if form.vars.appStart:
            if not form.vars.appEnd:
                session.flash = 'Application Period End-Time not specified!'
                redirect(URL('overall_admin','set_deadline'))
            else:
                db.AppDeadline.insert(start=form.vars.appStart, end=form.vars.appEnd)
                db.UserLogs.insert(activity='Application Period Set : '+str(form.vars.appStart)+' to '+str(form.vars.appEnd))
                app_msg_flash = 'Application Period Set : '+str(form.vars.appStart)+' to '+str(form.vars.appEnd)
        elif form.vars.appEnd:
            session.flash = 'Application Period Start-Time not specified!'
            redirect(URL('overall_admin','set_deadline'))
        if form.vars.nomStart:
            if not form.vars.nomEnd:
                session.flash = 'Nomination Period End-Time not specified!'
                redirect(URL('overall_admin','set_deadline'))
            else:
                db.NomDeadline.insert(start=form.vars.nomStart, end=form.vars.nomEnd)
                db.UserLogs.insert(activity='Nomination Period Set : '+str(form.vars.nomStart)+' to '+str(form.vars.nomEnd))
                nom_msg_flash = 'Nomination Period Set : '+str(form.vars.nomStart)+' to '+str(form.vars.nomEnd)
        elif form.vars.nomEnd:
            session.flash = 'Nomination Period Start-Time not specified!'
            redirect(URL('overall_admin','set_deadline'))
            
        response.flash = DIV(app_msg_flash,BR(),nom_msg_flash)
    elif form.errors:
        response.flash = 'Deadlines not Set : Form has error(s)!'
Reply all
Reply to author
Forward
0 new messages