pre-populated forms

21 views
Skip to first unread message

Timmie

unread,
Apr 18, 2009, 5:58:01 AM4/18/09
to web2py Web Framework
Hello,
I am new to web2py and lately bought the manual.
It is really cool how fast you can generate a application with so few
code!

I need more explanation about prepopulating forms.

I would like to have the following:


<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="new_value">Enter New Item</option>
</select>

Where volvo, saab, mercedes shall be popoulated from the database and
new_value shall give the oportunity to enter a new string.

Can you give me an example on how to achieve this?

(adapted from http://www.w3schools.com/tags/tag_select.asp)

As an extended functionality, I will pre-populate other form field
when a existing values (e.g. volvo) is selected.
I will try to use the hint from
Using Ajax with forms
http://groups.google.com/group/web2py/browse_thread/thread/2e1498efdda19a7b#

Thanks in advance & regards,
Timmie

mdipierro

unread,
Apr 18, 2009, 11:00:54 AM4/18/09
to web2py Web Framework
Thank you for your comments.

field validators IS_IN_SET and IS_IN_DB trigger SELECT/OPTION
automatically. You can do

INPUT(_name='fieldname',requires=IS_IN_DB(db,'table.field','%
(otherfieldname)s')

On Apr 18, 4:58 am, Timmie <timmichel...@gmx-topmail.de> wrote:
> Hello,
> I am new to web2py and lately bought the manual.
> It is really cool how fast you can generate a application with so few
> code!
>
> I need more explanation about prepopulating forms.
>
> I would like to have the following:
>
> <select>
>   <option value="volvo">Volvo</option>
>   <option value="saab">Saab</option>
>   <option value="mercedes">Mercedes</option>
>   <option value="new_value">Enter New Item</option>
> </select>
>
> Where volvo, saab, mercedes shall be popoulated from the database and
> new_value shall give the oportunity to enter a new string.
>
> Can you give me an example on how to achieve this?
>
> (adapted fromhttp://www.w3schools.com/tags/tag_select.asp)
>
> As an extended functionality, I will pre-populate other form field
> when a existing values (e.g. volvo) is selected.
> I will try to use the hint from
> Using Ajax with formshttp://groups.google.com/group/web2py/browse_thread/thread/2e1498efdd...

Timmie

unread,
May 18, 2009, 10:16:15 AM5/18/09
to web2py Web Framework
Hello Massimo and others,

> field validators IS_IN_SET and IS_IN_DB trigger SELECT/OPTION
> automatically. You can do
>
> INPUT(_name='fieldname',requires=IS_IN_DB(db,'table.field','%
> (otherfieldname)s')
I checked that out.
It works well to define the items offered for selection.
But how do I deal with the new entry?

If I follow your code I only get:
> > <select>
> > <option value="volvo">Volvo</option>
> > <option value="saab">Saab</option>
> > <option value="mercedes">Mercedes</option>
> > </select>

But what do I do if I want to give the users the option to
1) select from previous entries OR
2) add new data like:

> > <select>
> > <option value="volvo">Volvo</option>
> > <option value="saab">Saab</option>
> > <option value="mercedes">Mercedes</option>
> > <option value="new_value">Enter New Item</option>
> > </select>

Please help me how to get something like:
> > <option value="new_value">Enter New Item</option>
into my SQLFORM?

Thanks in advance,
Timmie

mdipierro

unread,
May 18, 2009, 10:30:54 AM5/18/09
to web2py Web Framework
You must create your own widget do to this. If I have time I will post
an example.

Massimo

Timmie

unread,
May 18, 2009, 10:44:52 AM5/18/09
to web2py Web Framework
> You must create your own widget do to this.
Thanks for this info. I will try to learn widgets.

> If I have time I will post an example.
This would be very neat.
I think this is a typical web form function...

Timmie

unread,
May 25, 2009, 5:37:22 AM5/25/09
to web2py Web Framework
Hello!
> You must create your own widget do to this. If I have time I will post
> an example.
I have just seen that DenesL was faced with the same problem:
http://groups.google.com/group/web2py/msg/089cee9b4fdf11b3

What happened to his patch in:
http://groups.google.com/group/web2py/msg/44cc5a60564d0f93?

Any chance that I can do

db.mytest.person.requires=IS_NULL_OR(IS_ALPHANUMERIC(), IS_IN_DB(db,
'dogs.dog_name',
'%
(dog_name)s'))

where IS_IN_DB could also be IS_IN_SET.

Kind regards,
Timmie

mdipierro

unread,
May 25, 2009, 10:29:14 AM5/25/09
to web2py Web Framework
Let me think about adding this:

db.mytest.person.requires=IS_NULL_OR(IS_ALPHANUMERIC(), IS_IN_DB
(db,'dogs.dog_name',
'%
(dog_name)s'))

In general I am against a proliferation of validators. This is because
everybody has flightly different needs and it is easy enought to
define your own.

I am usually in favor of extending existing validators.

DenesL

unread,
May 25, 2009, 10:41:03 AM5/25/09
to web2py Web Framework
Hi Timmie,

the IS_IN_SET_OR validator code could be used in your situation but it
would limit the range of possible values, i.e. it is not as open ended
as you want:

<QUOTE>
But what do I do if I want to give the users the option to
1) select from previous entries OR
2) add new data like:

> > <select>
> > <option value="volvo">Volvo</option>
> > <option value="saab">Saab</option>
> > <option value="mercedes">Mercedes</option>
> > <option value="new_value">Enter New Item</option>
> > </select>
</QUOTE>

but that limitation is a good thing to have, otherwise you end up with
unwanted data in the DB.

> Any chance that I can do
>
> db.mytest.person.requires=IS_NULL_OR(IS_ALPHANUMERIC(), IS_IN_DB(db,
> 'dogs.dog_name',
> '%
> (dog_name)s'))
>
> where IS_IN_DB could also be IS_IN_SET.
>

Not currently, IS_NULL_OR has two parameters: other (a validator) and
null (the value returned on null imput).

Denes.

mdipierro

unread,
May 25, 2009, 10:51:58 AM5/25/09
to web2py Web Framework
> > Any chance that I can do
>
> > db.mytest.person.requires=IS_NULL_OR(IS_ALPHANUMERIC(), IS_IN_DB(db,
> > 'dogs.dog_name',
> >                                                                 '%
> > (dog_name)s'))
>
> > where IS_IN_DB could also be IS_IN_SET.
>
> Not currently, IS_NULL_OR has two parameters: other (a validator) and
> null (the value returned on null imput).

Not but you can do

db.mytest.person.requires=[IS_ALPHANUMERIC(),IS_NULL_OR(IS_IN_DB
(db,'dogs.dog_name', (dog_name)s'))]

and I believe this is equivalent to what you want, except that it will
not display the dropbox without creating a custom widget to handle it.

Massimo

Timmie

unread,
May 25, 2009, 12:13:24 PM5/25/09
to web2py Web Framework
First, thanks for you both to follow up!
> db.mytest.person.requires=[IS_ALPHANUMERIC(),IS_NULL_OR(IS_IN_DB
> (db,'dogs.dog_name', (dog_name)s'))]
>
> and I believe this is equivalent to what you want, except that it will
> not display the dropbox without creating a custom widget to handle it.
Well, I would use this as validator.
But I definately need the drop-down box as it may show already
existing values in the database.

> not display the dropbox without creating a custom widget to handle it.
Where can I start to create such a widget?
I searched the manual and it only refers to widgets as GUI (TK)
widgets.

Regards,
Timmie

mdipierro

unread,
May 25, 2009, 6:52:32 PM5/25/09
to web2py Web Framework
It is not in the manual.

give this:

db.mytest.person.requires=[IS_ALPHANUMERIC(),IS_NULL_OR(IS_IN_DB
(db,'dogs.dog_name', (dog_name)s'))]

you can do

class OptionsWidget:

@staticmethod
def has_options(field):
return hasattr(field.requires, 'options')\
or isinstance(field.requires, IS_NULL_OR)\
and hasattr(field.requires.other, 'options')

@staticmethod
def widget(field, value):
id = '%s_%s' % (field._tablename, field.name)
if isinstance(field.requires, IS_NULL_OR)\
and hasattr(field.requires.other, 'options'):
opts = [OPTION(_value='')]
options = field.requires[1].other.options()
elif hasattr(field.requires, 'options'):
opts = []
options = field.requires.options()
else:
raise SyntaxError, 'widget cannot determine options of %s'
% field
opts += [OPTION(v, _value=k) for (k, v) in options]
return SELECT(*opts, **dict(_id=id, _class=field.type,
_name=field.name, value=value,
requires=field.requires))

db.mytest.person.widget=OptionWidget.widget

Timmie

unread,
May 26, 2009, 4:33:34 AM5/26/09
to web2py Web Framework
Thanks for this example code.

As it works not out of the box with my table, I would need to play a
bit with it.

I think this is a faily common thing:
For instance these forms:
"where did you hear from us?":
Search engine, friends, newspaper, others => please specify
http://support.easywebstore.net/index.php?_m=knowledgebase&_a=viewarticle&kbarticleid=23

What about a workaroud to place a field in col3 when a user selects
"other" or "Enter New Item" where the input can be typed in?

Pseudocode:
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="mercedes">Mercedes</option>
<option value="new_value">Enter New Item</option> => specify:
<input>mynewcar</input>
</select>

The problem is how to sync these two forms.

mdipierro

unread,
May 26, 2009, 9:02:55 AM5/26/09
to web2py Web Framework
cols3=INPUT(....)
> Search engine, friends, newspaper, others => please specifyhttp://support.easywebstore.net/index.php?_m=knowledgebase&_a=viewart...

Richard

unread,
May 28, 2009, 8:54:57 PM5/28/09
to web2py Web Framework
hi Timmie,

I'm also very interested in a simple way to use that pattern (either
select from list or create new).
Have you made any progress in the last few days?

Richard


On May 26, 6:33 pm, Timmie <timmichel...@gmx-topmail.de> wrote:
> > you can do
>
> > class OptionsWidget:
>
> >     @staticmethod
> >     def has_options(field):
> >         return hasattr(field.requires, 'options')\
> >              or isinstance(field.requires, IS_NULL_OR)\
> >              and hasattr(field.requires.other, 'options')
>
> >     @staticmethod
> >     def widget(field, value):
> >         id = '%s_%s' % (field._tablename, field.name)
> >         if isinstance(field.requires, IS_NULL_OR)\
> >              and hasattr(field.requires.other, 'options'):
> >             opts = [OPTION(_value='')]
> >             options = field.requires[1].other.options()
> >         elif hasattr(field.requires, 'options'):
> >             opts = []
> >             options = field.requires.options()
> >         else:
> >             raise SyntaxError, 'widget cannot determine options of %s'
> > % field
> >         opts += [OPTION(v, _value=k) for (k, v) in options]
> >         returnSELECT(*opts, **dict(_id=id, _class=field.type,
> >                       _name=field.name, value=value,
> >                       requires=field.requires))
>
> > db.mytest.person.widget=OptionWidget.widget
>
> Thanks for this example code.
>
> As it works not out of the box with my table, I would need to play a
> bit with it.
>
> I think this is a faily common thing:
> For instance these forms:
> "where did you hear from us?":
> Search engine, friends, newspaper, others => please specifyhttp://support.easywebstore.net/index.php?_m=knowledgebase&_a=viewart...
>
> What about a workaroud to place a field in col3 when a user selects
> "other" or "EnterNewItem" where the input can be typed in?
>
> Pseudocode:
> <select>
>   <option value="volvo">Volvo</option>
>   <option value="saab">Saab</option>
>   <option value="mercedes">Mercedes</option>
>   <option value="new_value">EnterNewItem</option>   => specify:

JohnMc

unread,
May 28, 2009, 11:18:02 PM5/28/09
to web2py Web Framework
Timmie,

I see a workaround in ColdFusion that is the following:

1/ SELECT statement, INPUT statement and some Javascript. Javascript
function hides the INPUT in an appropriately wrapped DIV.
2/ In the SELECT include 'Other' as part of your OPTION list.
3/ If 'Other' is selected then the DIV/INPUT pair is displayed for the
user to input a new item. Database update occurs.

Not as elegant as a combo box but it works.

On May 26, 3:33 am, Timmie <timmichel...@gmx-topmail.de> wrote:
> Search engine, friends, newspaper, others => please specifyhttp://support.easywebstore.net/index.php?_m=knowledgebase&_a=viewart...

Tim Michelsen

unread,
May 30, 2009, 4:42:52 PM5/30/09
to web...@googlegroups.com
> Have you made any progress in the last few days?
Sorry, no progress on this so far.

My development has stalled a bit as I am now in a decision making
process involving many parties...

But let's not forget about this!

Reply all
Reply to author
Forward
0 new messages