Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Zope: Users

Form Variable

 

 

Zope users RSS feed   Index | Next | Previous | View Threaded


ahabib at engin

Sep 22, 2004, 11:20 AM

Post #1 of 5 (1906 views)
Permalink
Form Variable

I have a form variable whose value is being passed to a DTML method upon
form submission. In this DTML method, I use the form variable as input to
a <dtml-in>. When this form variable is passed as a list(i.e. it contains
multiple values), everything works fine. However, when this form variable
is passed as a string(i.e. it contains a single value), <dtml-in>
complains that it is being passed an input of the wrong type. Is there a
way to detect whether this form variable is a list or string? Using the
len function doesn't work because len works on both strings and lists.

One solution I have figured out uses a HTML hidden variable to set the
form variable to a default value. This guarantees that the form variable
is always a list since a minimum of 2 values are passed upon form
submission. However, this is a hack and I'd rather use something more
elegant.

Any help would be greatly appreciated. Thanks.

- Asad
_______________________________________________
Zope maillist - Zope [at] zope
http://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


pw_lists at slinkp

Sep 22, 2004, 11:51 AM

Post #2 of 5 (1952 views)
Permalink
Re: Form Variable [In reply to]

On Wed, Sep 22, 2004 at 02:20:40PM -0400, Asad Habib wrote:
> I have a form variable whose value is being passed to a DTML method upon
> form submission. In this DTML method, I use the form variable as input to
> a <dtml-in>. When this form variable is passed as a list(i.e. it contains
> multiple values), everything works fine. However, when this form variable
> is passed as a string(i.e. it contains a single value), <dtml-in>
> complains that it is being passed an input of the wrong type. Is there a
> way to detect whether this form variable is a list or string? Using the
> len function doesn't work because len works on both strings and lists.
>
> One solution I have figured out uses a HTML hidden variable to set the
> form variable to a default value. This guarantees that the form variable
> is always a list since a minimum of 2 values are passed upon form
> submission. However, this is a hack and I'd rather use something more
> elegant.

In your form, append ":list" to the variable's name.
e.g.

<input type="text" name="my_variable:list"> ....

This is documented in the "Advanced Scripting" chapter of the
online zope book.

--

Paul Winkler
http://www.slinkp.com
_______________________________________________
Zope maillist - Zope [at] zope
http://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


toolkit at magma

Sep 22, 2004, 12:20 PM

Post #3 of 5 (1848 views)
Permalink
Re: Form Variable [In reply to]

----- Original Message -----
From: "Asad Habib" <ahabib [at] engin>
> I have a form variable whose value is being passed to a DTML method upon
> form submission. In this DTML method, I use the form variable as input to
> a <dtml-in>. When this form variable is passed as a list(i.e. it contains
> multiple values), everything works fine. However, when this form variable
> is passed as a string(i.e. it contains a single value), <dtml-in>
> complains that it is being passed an input of the wrong type. Is there a
> way to detect whether this form variable is a list or string? Using the
> len function doesn't work because len works on both strings and lists.

You need the python 'type' statement, which unfortunately isn't available to
python scripts (its use is restricted). Therefore, you need a small external
method:


def typeof(self,a,b):
if type(a) == type(b):
return 1
else:
return 0


Then to use this method you can:

<dtml-call "REQUEST.set('a', [])">
<dtml-call "REQUEST.set('b', 'abc')">

<dtml-if "typeof(a,[])">
a is a list<br>
</dtml-if>

<dtml-if "typeof(b,'')">
b is a string<br>
</dtml-if>

Note: in the second dtml-if above, you are checking 'b' against an empty
string (two single quotes)

If you run the above dtml you will get:

a is a list
b is a string


HTH

Jonathan





_______________________________________________
Zope maillist - Zope [at] zope
http://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


pw_lists at slinkp

Sep 22, 2004, 12:53 PM

Post #4 of 5 (1852 views)
Permalink
Re: Form Variable [In reply to]

On Wed, Sep 22, 2004 at 03:21:21PM -0400, Jonathan Hobbs wrote:
>
> ----- Original Message -----
> From: "Asad Habib" <ahabib [at] engin>
> > I have a form variable whose value is being passed to a DTML method upon
> > form submission. In this DTML method, I use the form variable as input to
> > a <dtml-in>. When this form variable is passed as a list(i.e. it contains
> > multiple values), everything works fine. However, when this form variable
> > is passed as a string(i.e. it contains a single value), <dtml-in>
> > complains that it is being passed an input of the wrong type. Is there a
> > way to detect whether this form variable is a list or string? Using the
> > len function doesn't work because len works on both strings and lists.
>
> You need the python 'type' statement, which unfortunately isn't available to
> python scripts (its use is restricted). Therefore, you need a small external
> method:

What's wrong with same_type which zope provides for this purpose?

--

Paul Winkler
http://www.slinkp.com
_______________________________________________
Zope maillist - Zope [at] zope
http://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )


toolkit at magma

Sep 22, 2004, 1:25 PM

Post #5 of 5 (1857 views)
Permalink
Re: Form Variable [In reply to]

----- Original Message -----
From: "Paul Winkler" <pw_lists [at] slinkp>
> >
> > ----- Original Message -----
> > From: "Asad Habib" <ahabib [at] engin>
> > > I have a form variable whose value is being passed to a DTML method
upon
> > > form submission. In this DTML method, I use the form variable as input
to
> > > a <dtml-in>. When this form variable is passed as a list(i.e. it
contains
> > > multiple values), everything works fine. However, when this form
variable
> > > is passed as a string(i.e. it contains a single value), <dtml-in>
> > > complains that it is being passed an input of the wrong type. Is there
a
> > > way to detect whether this form variable is a list or string? Using
the
> > > len function doesn't work because len works on both strings and lists.
> >
> > You need the python 'type' statement, which unfortunately isn't
available to
> > python scripts (its use is restricted). Therefore, you need a small
external
> > method:
>
> What's wrong with same_type which zope provides for this purpose?

I've been using my 'typeof' external method for about 100 years - it feels
that long :),

but I like the new (new-to-me!) same_type function!




_______________________________________________
Zope maillist - Zope [at] zope
http://mail.zope.org/mailman/listinfo/zope
** No cross posts or HTML encoding! **
(Related lists -
http://mail.zope.org/mailman/listinfo/zope-announce
http://mail.zope.org/mailman/listinfo/zope-dev )

Zope users RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.