Hi Denes and/or Massimo,
I just pick up this old post from
http://groups.google.com/group/web2py/browse_frm/thread/971433920541935a
I used the code suggested by Denes, e.g. using TAG.SELECT and
TAG.OPTGROUP.
The code shows proper drop-down box but, after form.accepts(...) the
form.vars.my_field seems always None.
def test_code():
OG=TAG.OPTGROUP
g1=[ OPTION( r, _value=r ) for r in [23, 24, 25] ]
g2=[ OPTION( r, _value=r ) for r in [32, 33, 34] ]
ogs=[OG(_label='30 and under',*g1),OG(_label='over 30',*g2)]
sel = TAG.SELECT(_name='person', *ogs) # DO NOT USE
sel=SELECT(...)
form=FORM( sel, INPUT(_type='submit') )
if form.accepts(request.vars,keepvalues=True):
response.flash = 'got %s' % form.vars.person # Why always got
none?
return dict(form=form)
Question: What need I change to make form.vars.my_field work when
using TAG.SELECT()?
By the way, in case some people might ask why bother, why not just use
request.vars.my_field instead, well, it is just my personal best-
practice because form.vars.my_field usually contains data qualified by
validators, therefore more precisely than request.vars.my_field. Think
about this:
define_table('my_table',Field('my_field', requires=IS_UPPER()))
form = SQLFORM(db.mytable)
and input "hello world", then
request.vars.my_field=="hello world"
but
form.vars.my_field=="HELLO WORLD"
usually form.vars.my_field contains what I really need.
Regards,
Iceberg
On Feb18, 12:48am, DenesL <[email protected]> wrote:
> So for future reference:
>
> you can create OPTGROUPs with TAG.OPTGROUP
> but do NOT use it with the SELECT helper,
> you must use TAG.SELECT as in the following example:
>
> def index():
> OG=TAG.OPTGROUP
> g1=[ OPTION( r.name, _value=r.id ) for r in
> db(db.person.age<=30).select() ]
> g2=[ OPTION( r.name, _value=r.id ) for r in
> db(db.person.age>30).select() ]
> ogs=[OG(_label='30 and under',*g1),OG(_label='over 30',*g2)]
> sel = TAG.SELECT(_name='person', *ogs)
> # DO NOT USE sel=SELECT(_name='person',*ogs )
> f=FORM( sel )
> return dict(f=f)
>
> Denes.