Massimo,
This patch allows other validators to be used with IS_IN_SET and
still have SQLFORM render a dropdown list:
requires=[IS_IN_SET(['Please
choose...','English','Chinese','Italian']),IS_EXPR("str(value) !
='Please choose...' ")]
...would produce a dropdown list with a the text 'Please Choose...'
selected, but not allow the user to submit it.
Or to simple have a blank:
requires=[IS_IN_SET(['','English','Chinese','Italian']),IS_NOT_EMPTY
()]
If IS_IN_SET is NOT requires[0] then it will render with StringWidget.
I think something similar could be done for IS_IN_DB.
On Sep 5, 7:33 am, mdipierro <[email protected]> wrote:
> Can you explain it?
>
> On Sep 5, 3:34 am, "mr.freeze" <[email protected]> wrote:
>
> > I think this would do it:
>
> > Index: gluon/sqlhtml.py
> > ===================================================================
> > --- gluon/sqlhtml.py (revision 1196)
> > +++ gluon/sqlhtml.py (working copy)
> > @@ -167,11 +167,14 @@
> > :param field: the field needing checking
> > :returns: True if the field has options
> > """
> > + requires = field.requires
> > + if not isinstance(requires, (list, tuple)):
> > + requires = [requires]
> > + requires0 = requires[0]
> > + return hasattr(requires0, 'options')\
> > + or isinstance(requires0, IS_NULL_OR)\
> > + and hasattr(requires0.other, 'options')
>
> > - return hasattr(field.requires, 'options')\
> > - or isinstance(field.requires, IS_NULL_OR)\
> > - and hasattr(field.requires.other, 'options')
> > -
> > �...@staticmethod
> > def widget(field, value, **attributes):
> > """
> > @@ -602,7 +605,11 @@
> > else:
> > inpval = ''
> > elif OptionsWidget.has_options(field):
> > - if not field.requires.multiple:
> > + requires = field.requires
> > + if not isinstance(requires, (list, tuple)):
> > + requires = [requires]
> > + requires0 = requires[0]
> > + if not requires0.multiple:
> > inp = self.widgets.options.widget(field, default)
> > else:
> > inp = self.widgets.multiple.widget(field,
> > default)
>
> > On Sep 5, 3:00 am, "mr.freeze" <[email protected]> wrote:
>
> > > I think SQLFORM should be changed to allow for IS_IN_SET with other
> > > validators then you could just do:
> > > requires=[IS_IN_SET(['','English','Chinese','Italian']),IS_NOT_EMPTY
> > > ()]
>
> > > Or you could get fancy:
> > > requires=[IS_IN_SET(['Please
> > > choose...','English','Chinese','Italian']),IS_EXPR("str(value) !=
> > > 'Please choose...' ")]
>
> > > It could just be a convention that requires[0] is IS_IN_SET (similar
> > > to how IS_NULL_OR works currently).
>
> > > On Sep 4, 11:58 pm, Iceberg <[email protected]> wrote:
>
> > > > Hi Massimo,
>
> > > > My first web2py app is an order management system for my company, it
> > > > worked well in enterprise production for more than 15 months now. By
> > > > the way, all my other "first work of something" ended up as just a
> > > > prototype but not this one, thank god and web2py. I am refactoring the
> > > > app and going to share some ideas. Here comes the first one, about UI/
> > > > UE aspect.
>
> > > > Currently, if we write code like this:
> > > > db.Field('language', requires=IS_IN_SET
> > > > (['English','Chinese','Italian']))
> > > > it generates a drop-down list for three choices and the "English"
> > > > appears as the first option.
>
> > > > It is ok but, according to my long time observation, users who are
> > > > facing a long form with dozens of fields, tend to finish each blank
> > > > field one by one, but won't give enough attention to fields showing a
> > > > default value. So it would be much better if the above example can be
> > > > rendered as a drop-down list with 4 choices: an initial blank, then
> > > > English, Chinese, Italian. Currently this can be done by:
> > > > db.Field('language', requires=IS_NULL_OR(IS_IN_SET
> > > > (['English','Chinese','Italian'])))
> > > > but with a side effect that the empty value is also acceptable,
> > > > sometimes not what we want.
>
> > > > So my proposal is to adjust OptionsWidget to always show up with an
> > > > initial blank (when creating record), but not necessarily accepting
> > > > it. Just change line 198 of sqlhtml.py from:
> > > > opts = []
> > > > to:
> > > > if value:
> > > > opts = []
> > > > else: # To enforce an explicit choice when creating new record
> > > > opts = [OPTION(_value='')]
>
> > > > Please give it a try and hope you will like it.
>
> > > > Regards,
> > > > Iceberg
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---