One small problem with this: my checkbox is now validated with
onvalidate which only runs when everything else validates. I want the
'must agree to terms' error to popup any time the form is submitted
without the checkbox checked, not just when it's the only think that
doesn't validate. If I could determine whether this is a postback the
way accepts does, I could just invoke my validation code manually.
Poking into the FORM.accepts code, though, I noticed onvalidate may be
a dict of {'onsuccess': func, 'onfailure': func}, so now I just have:
onvalidation={'onsuccess': validateTerms, 'onfailure': validateTerms}
...and that seems to do the trick.
BTW, I got confused by INPUT's value arg. I kept treating it like
_value until I realized that value is only meant to be set to True to
turn the checkbox on by default. Maybe deprecate 'value' in favor of
'prechecked' for this purpose to avoid the confusion.
On Mar 27, 2:28 pm, Brian Will <[email protected]> wrote:
> Thanks, everyone. That's what I was looking for.
>
> On Mar 27, 9:50 am, 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?