On Fri, Nov 30, 2001 at 05:17:20PM -0500, Geoffrey Talvola wrote:
> At 12:22 PM 11/30/01 -0500, Hsueh-ching Yen wrote:
> >Hi, all
> >I just started using webware, and have a question on HTML form submission.
> >I have a
> ><SELECT MULTIPLE name= myselect >
> >If user selects one item, AAA, then submit, in HTTPRequest object
> >req.fields()["myselect"] is AAA (a string).
> >If user selects two item, AAA and BBB, then submit, in HTTPRequest object
> >req.fields()["myselect"] is ['AAA', 'BBB'] (a list).
> >Isn t easier if it is always in a list? Then we don t need to test the
> >type of the objects?
> I think it's impossible to tell the difference. The way form fields are
> encoded, a single item selected in a multiple select list is the same as an
> item selected in a single select list.
> What I did is to write a method in my SitePage.py:
> def listField(self, name):
> values = self.request().field(name, )
> if not isinstance(values, types.ListType):
> values = [values]
> return values
> Then instead of using reg.fields()["myselect"], I use
> self.listField("myselect"). It'll always come back as a list.
The problem is in Python's cgi module that Webware uses. It returns a
string if there's one value, or a list if there's more than one. In my
applications I use a .fieldMulti method like Geoff's, and a
.fieldSingle method that arbitrarily picks the first value if there are
more than one. That way, the application doesn't have to do an if-test
for every single variable, nor run the risk of crashing if the user
submits multiple values to a single-value-only field.
-Mike (Iron) Orr, iron@... (if mail problems: mso@...)
http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol