Actually the insert index depends on the formstyle.
It should be -2 for formstyle='table2cols' as above.
That has to be changed to -1 for formstyle='table3cols'.

I think this is explained somewhere else in the manual but
form[0] is the TABLE and form[0][0] would be the first row (TR), etc.

When 'table2cols' is used then there are two TRs per field,
hence the -2 index to get over the submit which is last.
While using the default 'table3cols' there is one TR per field,
so -1 does the job.
A different approach has to be used for other styles like 'divs' or
'ul'.

All this just to say that it is not easy to generalize,
you always have to know how things are built.
Playing with the objects in the shell, e.g. using dir on them,
and looking at the source code will help a lot.


On Mar 27, 12:50 pm, villas <[email protected]> wrote:
> Thanks Denes. It is very frustrating when this kind of information is
> not readily available, so I have added this pearl of wisdom to the
> book.
>
> I appreciate that what I have written is not fully explained, but
> sometimes a simple example is worth a thousand words. If you feel this
> could or should be improved, please let me know.
>
> http://web2py.com/book/default/chapter/07#Adding-extra-form-elements-...
>
> On Mar 27, 3:16 pm, DenesL <[email protected]> wrote:
>
>
>
>
>
>
>
> > Just add
>
> > form[0].insert(-2,TR(LABEL('I agree to
> > terms'),INPUT(_name='agree',value=True,_type='checkbox')))
>
> > after the form has been defined.
> > On submission form.vars.agree will have the status of the checkbox,
> > which you probably would check using onvalidation.
>
> > Where do I collect my bonus points? ;)
>
> > On Mar 27, 6:43 am, Brian Will <[email protected]> wrote:
>
> > > I have an insert SQLFORM of 8 fields, to which I'd like to add a
> > > single "I agree to terms" checkbox at the bottom. There are probably
> > > tons of ways to do this, but I'm hoping someone has a really simple
> > > method that doesn't require resorting to a manual FORM? Can I simply
> > > tack on another INPUT to the SQLFORM? (Bonus points for placing it
> > > after my last field but before the submit button). Here's the current
> > > code.
>
> > >     form = SQLFORM(db.job_post, submit_button='Post Job',
> > > formstyle='table2cols',
> > >         fields=['poster_name', 'poster_email', 'poster_phone',
> > > 'zipcode', 'location_description', 'job_type', 'job_title',
> > > 'job_description'],
> > >         _id='postjob'
> > >     )
>
> > >     if form.accepts(request.vars, session):
> > >         redirect(URL('post_email', vars={'id': form.vars.id,
> > > 'poster_name': form.vars.poster_name, 'poster_email':
> > > form.vars.poster_email}))
>
> > > What's solution allows me to make the minimal change? The simplest
> > > solution that occurs to me is to use a SQLFORM.factory:
>
> > >     form = SQLFORM(db.job_post.poster_name, db.job_post.poster_email,
> > > db.job_post.poster_phone, db.job_post.zipcode,
> > > db.job_post.location_description, db.job_post.job_type,
> > > db.job_post.job_title, db.job_post.job_description, FIELD('terms',
> > > 'boolean', IS_EQUAL_TO(True)) submit_button='Post Job',
> > > formstyle='table2cols',
> > >         _id='postjob'
> > >     )
>
> > >     if form.accepts(request.vars, session):
> > >         form.vars.id =
> > > db.job_post.insert(**db.job_post._filter_fields(request.vars))
> > >         redirect(URL('post_email', vars={'id': form.vars.id,
> > > 'poster_name': form.vars.poster_name, 'poster_email':
> > > form.vars.poster_email}))
>
> > > I guess this isn't too onerous, but I don't really like having to
> > > manually do the insert, so I'm wondering about alternatives. BTW, why
> > > does _filter_fields begin with _ if it's used publicly? Shouldn't it
> > > just be filter_fields?

Reply via email to