Linked sqlform inputs

8 views
Skip to first unread message

Delaney

unread,
Dec 31, 2009, 6:52:29 PM12/31/09
to web2py-users
So I'm finishing up my first web2py program (has been interesting,
once you 'get' the web2py way of doing things. One last thing left.
I need to link the actions of a few inputs from a SQLForm together.

Specifically I need a double email inputs that have to matched but
don't really need the second one in the model, or do I?

Also need to link a drop down input and string input, so if the drop
down option is 'Other' the string input has to be filled out,
otherwise it need to be empty.

Where in the manual or online docs can I find the best way to
implement this?

Thanks!

mdipierro

unread,
Jan 1, 2010, 12:21:02 PM1/1/10
to web2py-users
form=SQLFORM(...)
form[0].insert(position, TR(
LABEL(self.messages.verify_email + ':'),
INPUT(_name="email_two",
_type="email",
requires=IS_EXPR('value==%s' % \
repr(request.vars.get(email, None)),
error_message=T('Emails do not math')),'')

Delaney

unread,
Jan 6, 2010, 5:37:15 PM1/6/10
to web2py-users
Thanks for that, but it doesn't seem to work when using
sqlform.custom.label & wigdets. Which is how I happen to be doing the
whole form for this application. Is there an alternative?

On Jan 1, 9:21 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> form=SQLFORM(...)
> form[0].insert(position, TR(
>                         LABEL(self.messages.verify_email + ':'),
>                         INPUT(_name="email_two",
>                               _type="email",
>                               requires=IS_EXPR('value==%s' % \
>                                repr(request.vars.get(email, None)),
>                         error_message=T('Emails do not math')),'')
>
> On Dec 31 2009, 5:52 pm, Delaney <delaneygilli...@gmail.com> wrote:
>
> > So I'm finishing up my first web2py program (has been interesting,
> > once you 'get' the web2py way of doing things.  One last thing left.

> > I need to link the actions of a fewinputsfrom a SQLForm together.
>
> > Specifically I need a double emailinputsthat have to matched but

Delaney

unread,
Jan 6, 2010, 8:22:53 PM1/6/10
to web2py-users
On page 228 of the web2py manual I see the docs pertaining to almost
exactly what I'm trying to do with the title 'Validators with
Dependencies' and see that 'The same mechanism can be applied to FORM
and SQLFORM objects.'

However I don't want 'password_again' in the database. If I try
inserting/append the Field into the sqlform it complains about not
being in the table. Am I missing something?

On Jan 6, 2:37 pm, Delaney <delaneygilli...@gmail.com> wrote:
> Thanks for that, but it doesn't seem to work when using
> sqlform.custom.label & wigdets.  Which is how I happen to be doing the
> whole form for this application.  Is there an alternative?
>
> On Jan 1, 9:21 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > form=SQLFORM(...)
> > form[0].insert(position, TR(
> >                         LABEL(self.messages.verify_email + ':'),
> >                        INPUT(_name="email_two",
> >                               _type="email",
> >                               requires=IS_EXPR('value==%s' % \
> >                                repr(request.vars.get(email, None)),
> >                         error_message=T('Emails do not math')),'')
>
> > On Dec 31 2009, 5:52 pm, Delaney <delaneygilli...@gmail.com> wrote:
>
> > > So I'm finishing up my first web2py program (has been interesting,
> > > once you 'get' the web2py way of doing things.  One last thing left.

> > > I need tolinkthe actions of a fewinputsfrom a SQLForm together.


>
> > > Specifically I need a double emailinputsthat have to matched but
> > > don't really need the second one in the model, or do I?
>

> > > Also need tolinka drop downinputand stringinput, so if the drop
> > > down option is 'Other' the stringinputhas to be filled out,

mdipierro

unread,
Jan 6, 2010, 9:19:36 PM1/6/10
to web2py-users
Can you post an example of your code?

Delaney

unread,
Jan 7, 2010, 1:22:37 PM1/7/10
to web2py-users
The controller is...
def reg_info():
form = SQLFORM(db.attendees)
form.append(
Field('verify_email','string',
requires = IS_EXPR('value==%s' % repr(request.vars.get
(form.vars.email, None)),
error_message=T('Emails do not
math'))))

if form.accepts(request.vars):
response.flash = 'form accepted'
session.attendee_name = "%s %s" %
(form.vars.first_name,form.vars.last_name)
redirect(URL(r=request,f='thank_you'))
elif form.errors:
response.flash = 'form has errors: %s' % form.errors
else:
response.flash = 'Please fill out the registration
form'

return dict(form=form)

When I try to use in the view
<div class="left"> {{=form.custom.label.verify_email}}
{{=form.custom.widget.verify_email}}</div>

I get '<no table>.verify_email'

I guess what I'm looking for is the ability to add a Field to a
SQLForm that doesn't get stored in the DB. For now I'm just add it to
the db.py with a custom validator but seems like there must be some
way to make this a contoller/view addition without touching the
model. Thanks for your time mdipierro!


On Jan 6, 6:19 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> Can you post an example of your code?
>

> On Jan 6, 7:22 pm,Delaney<delaneygilli...@gmail.com> wrote:
>
> > On page  228 of the web2py manual I see the docs pertaining to almost
> > exactly what I'm trying to do with the title 'Validators with
> > Dependencies' and see that 'The same mechanism can be applied to FORM
> > and SQLFORM objects.'
>
> > However I don't want 'password_again' in the database.  If I try
> > inserting/append the Field into the sqlform it complains about not
> > being in the table.  Am I missing something?
>

> > On Jan 6, 2:37 pm,Delaney<delaneygilli...@gmail.com> wrote:
>
> > > Thanks for that, but it doesn't seem to work when using
> > > sqlform.custom.label & wigdets.  Which is how I happen to be doing the
> > > whole form for this application.  Is there an alternative?
>
> > > On Jan 1, 9:21 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
>
> > > > form=SQLFORM(...)
> > > > form[0].insert(position, TR(
> > > >                         LABEL(self.messages.verify_email + ':'),
> > > >                        INPUT(_name="email_two",
> > > >                               _type="email",
> > > >                               requires=IS_EXPR('value==%s' % \
> > > >                                repr(request.vars.get(email, None)),
> > > >                         error_message=T('Emails do not math')),'')
>

mdipierro

unread,
Jan 7, 2010, 1:28:29 PM1/7/10
to web2py-users
where do you get '<no table>.verify_email'? can you show the complete
html output?

Delaney

unread,
Jan 7, 2010, 1:49:29 PM1/7/10
to web2py-users
Sure, http://www.pasteall.org/10201/html4strict

This is the view and the output.

On Jan 7, 10:28 am, mdipierro <mdipie...@cs.depaul.edu> wrote:
> where do you get '<no table>.verify_email'? can you show the complete
> html output?
>

Delaney

unread,
Jan 7, 2010, 1:52:22 PM1/7/10
to web2py-users
Honestly doing keeping 'verify_email' in the db is not a big deal to
me. If there is a way to reference other Field values in
db.define_table I'll do it there instead. Just a little lost with the
docs on how to do it.

On Jan 7, 10:49 am, Delaney <delaneygilli...@gmail.com> wrote:
> Sure,http://www.pasteall.org/10201/html4strict

Delaney

unread,
Jan 7, 2010, 2:24:47 PM1/7/10
to web2py-users
Sorry for the spamming of messages. But on page 213 of the manual it
says... "The same mechanism can be applied to FORM and SQLFORM
objects."

Can I get an example that in the controller that will work with custom
sqlforms? Maybe that's all I really need.

mdipierro

unread,
Jan 7, 2010, 2:39:47 PM1/7/10
to web2py-users
I get it now. I would do this:

def reg_info():
form = SQLFORM(db.attendees)

form[0].insert(-1,TR('Verify email',INPUT
(_name='verify_email'),''))
def validate_email(form):
if form.vars.email!=form.vars.verify_email:
form.errors.verify_email='email does not match'
if form.accepts(request.vars,onvalidation=validate_email):


response.flash = 'form accepted'
session.attendee_name = "%s %s" %
(form.vars.first_name,form.vars.last_name)
redirect(URL(r=request,f='thank_you'))
elif form.errors:
response.flash = 'form has errors: %s' % form.errors
else:
response.flash = 'Please fill out the registration form'
return dict(form=form)

Delaney

unread,
Jan 7, 2010, 4:39:53 PM1/7/10
to web2py-users
That makes a lot of sense, however in the view.

<div class="left"> {{=form.custom.label.verify_email}}
{{=form.custom.widget.verify_email}}</div>

The verify_email is not available from form.custom.label or
form.custom.widget... still shows 'None,None'

But I do think this is much closer!

mdipierro

unread,
Jan 7, 2010, 5:32:48 PM1/7/10
to web2py-users
You are right. It will never be in form.custom but this could work


{{=form[0][-2][1][0]}}

Delaney

unread,
Jan 7, 2010, 6:36:42 PM1/7/10
to web2py-users
Thank Works!!! I 'got my fish' and would also like to learn 'how to
fish' now. :P

But sure seems like a real cryptic non-documented way to do things.
Is there any way to get more details. Just like using form[0] instead
of form. The api docs online don't seem to fill the gaps for me with
what's left out of the manual. Thanks agian!

mdipierro

unread,
Jan 7, 2010, 6:42:11 PM1/7/10
to web2py-users
form is the form
form[0] is the table
form[0][-2] is the row (TR) before the last, the one you inserted
form[0][-2][1] is the second TD of the TR
form[0][-2][1][0] is the first thing in the TD, the INPUT helper

error messages are appended after INPUT/TEXTAREA helpers
automatically, if any.

mdipierro

unread,
Jan 7, 2010, 6:43:15 PM1/7/10
to web2py-users
On a second thought, you could have also done

{{=form.element(_name='verify_email')}}

which is more readable.

Delaney

unread,
Jan 7, 2010, 7:33:21 PM1/7/10
to web2py-users
Yeah, that's MUCH more legible. By the way I'm DONE!!! once I
understood that part doing the rest was just a few minutes work.
Thank you so much for your time and patience. Soon I will have some
time to help with that previous project we had discussed earlier.
Thanks again Massimo!
Reply all
Reply to author
Forward
0 new messages