Reputation: 725
I am using the web2py framework and I am trying to create a SQLFORM without a submit button. I have multiple forms, with fields in common so I cannot use SQLFORM.factory(), and I wish to have only one of the forms with a submit button which will handle the data from all of the other forms on the page. Any suggestions on the best way to do this?
Upvotes: 1
Views: 1296
Reputation:
form1 = SQLFORM(db.table1, buttons=[])
this will set form one without any button, so no submit too.
Upvotes: 3
Reputation: 2451
I guess one way to do it could be to set the _formkey and _formname fields of all the forms to the same value. Something like this:
my_form2.formname = my_form1.formname
my_form2.formkey = my_form1.formkey
If you want to hide submit buttons manually, try something like this:
my_form1.element('input', _type = 'submit')['_style'] = 'display:none'
Leave one of the submit buttons for which formname and formkey has been set equal to the values of the other forms intact - this button will be used to submit the forms.
Inserting vars into db tables can be done something like:
db.table1.insert(**db.table1._filter_fields(request.vars))
db.table2.insert(**db.table2._filter_fields(request.vars))
If fields have the same names, there may be some potential for trouble, so be careful.
Upvotes: 1
Reputation: 161
I think that you can combine different SQLFORMs appending one form to other. So you will get only one "multiple form" with just one submit button. Something like that in your controller:
form1 = SQLFORM(db.table1)
form2 = SQLFORM(db.table2)
form1.append(form2)
if form1.process().accepted:
#filter the fields from each form do whatever you want
response.flash = 'OK'
elif form1.errors:
response.flash = 'NOT OK'
return dict(form=form1)
Did you see that I just asked to the form1? It is because he is the main form and, sure, you did the right filter among your forms. Hope this helps.
Upvotes: 0