web2py creates a hidden _formname field for each form. If there are 
multiple forms on the page, they must each have a unique _formname. By 
default, SQLFORM creates a _formname based on the table name and type of 
form (and possibly record number), but you can generate your own name via 
the 'formname' argument to form.accepts (or form.validate, or 
form.process). If you want to create update forms, though, you don't have 
to manually fill in the field values -- just pass the record as the second 
argument to SQLFORM. Also, in that case, you'll automatically get a unique 
_formname for each form because the record id becomes part of the 
_formname. 
See http://web2py.com/book/default/chapter/07#SQLFORM-and-insert/update/delete.

Also, rather than having a new variable for each form (i.e., form0, form1, 
etc.), which makes the code rather messy, why not just make a list of forms?

forms = []
forms.append(SQLFORM(...))  # add a new form to the list
form[i]  # to reference the i-th form

You might also consider creating each form as an Ajax component, or at 
least submitting the forms via Ajax, so the whole page of forms doesn't 
have to get re-processed when just a single form is submitted.

Anthony

On Monday, December 12, 2011 1:43:05 PM UTC-5, Richard wrote:
>
> Hello,
>
> Is there a better way of doing this :
>
> def bunch_update_with_multi_form():
>     form0=None
>     form1=None
>     form3=None
>     form4=None
>     form5=None
>     form6=None
>     form7=None
>     form8=None
>     form9=None
>     form10=None
>     form11=None
>     form12=None
>     form13=None
>     form14=None
>     form15=None
>     form16=None
>     form17=None
>     form18=None
>     form19=None
>     form20=None
>     form21=None
>     form22=None
>     form23=None
>     form24=None
>     form25=None
>     form26=None
>     form27=None
>     form28=None
>     form29=None
>     form30=None
>
>     form_num = 0
>     for i in rows:
>         globals()['form%s'%form_num] = SQLFORM(db[request.args(0)])
>         for f in db[request.args(0)].fields:
>             if globals()['form%s'%form_num] != None:
>                 globals()['form%s'%form_num].vars[f] =
> i[request.args(0)][f]
>         if globals()['form%s'%form_num] != None:
>             if globals()['form%s'%form_num].process().accepted:
>                 response.flash = '...'
>             elif globals()['form%s'%form_num].errors:
>                 response.flash = 'form has errors'
>         form_num+=1
>     return
>
> dict(table=table,form0=form0,form1=form1,form2=form2,form3=form3,form4=form4,form5=form5,form6=form6,form7=form7,form8=form8,form9=form9,form10=form10,form11=form11,form12=form12,form13=form13,form14=form14)
>
> When using web2py shell I don't have any problem to execute my loops,
> but in app I am getting only one correct form on 14...
>
> I read in the book that SQLFORM is creating it on unique form
> identifier but there is no example about that... Should I user SQLFORM
> in view (put my loop there) to make it works :
>
> Thanks
>
> Richard
>
>

Reply via email to