That is better, but still not good enough because form.accepts(...,
keepvalues=True) has no effect.
Anyway, Massimo gave enough hint for the correct direction. Here comes
my patch to html.py
__all__.append('OPTGROUOP') # Finally we have native OPTGROUP support!
class OPTGROUP(DIV):
tag = 'optgroup'
class SELECT(INPUT):
tag = 'select'
def _fixup(self):
components = []
for c in self.components:
if isinstance(c, str): # my patch
components.append(OPTION(c, _value=str(c)))
else: # my patch
components.append(c)
self.components = components
def _postprocessing(self):
import itertools
options = itertools.chain( *[ # my patch
( c.components if isinstance(c, OPTGROUP) else [c] )
for c in self.components ] )
if self['value'] != None:
if not self['_multiple']:
for c in options: # my patch
if self['value'] and str(c['_value'])\
== str(self['value']):
c['_selected'] = 'selected'
else:
c['_selected'] = None
else:
values = re.compile('[\w\-:]
+').findall(str(self['value']))
for c in options: # my patch
if self['value'] and str(c['_value']) in values:
c['_selected'] = 'selected'
else:
c['_selected'] = None
Regards,
iceberg
On Apr21, 11:59am, mdipierro <[email protected]> wrote:
> Now I see the problem:
>
> do this
>
> class MYSELECT(SELECT):
> def _fixup(self): pass
>
> and use MYSELECT
>
> > > On Apr 17, 1:08 pm, Iceberg <[email protected]> wrote:
>
> > > > Hi Denes and/or Massimo,
>
> > > > I just pick up this old post
> > > > fromhttp://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