[Zope] dtml-sqlvar type="int" optional

Matthew T. Kromer matt@zope.com
Mon, 26 Aug 2002 17:59:57 -0400


Mike Doanh Tran wrote:

>I have an optional field name "zipcode" in  my form whose type is 
>an integer. I keep getting an Error:  "Invalid integer value for zipcode"
>whenever this field is not fill out by the user.
>
>Here's my form name and ZSQL Method:
>
><input type="text" name="zipcode"  size="5">
>  
>

Absent any user input, this is going to do

    zipcode=""

with the zipcode 22408 entered, it will do

    zipcode="22408"

not

    zipcode=22408

You might want to use the publisher's data conversion to help you for 
this -- suffix the name with ":int" e.g. "zipcode:int" to tell the 
publisher to convert the data to an integer.

THEN, you'll get to discover what the integer value of "" is.   (I think 
its 0, but don't quote me on that).

>ZSQL Method:
>
><dtml-sqlvar zipcode type="int" optional>
>  
>

The "optional" test is doing a test against None, not "" or 0.  This 
isn't what you want, in most cases.  If you use type="nb" you get a 
nonblank test.  However, nb is a nonblank string -- so if you used 
zipcode:int as your field name, it won't be what you want either!

>
>mysql> describe profiles zipcode;
>+---------+--------------+------+-----+---------+-------+
>| Field   | Type         | Null | Key | Default | Extra |
>+---------+--------------+------+-----+---------+-------+
>| zipcode | mediumint(5) | YES  |     | NULL    |       |
>+---------+--------------+------+-----+---------+-------+
>1 row in set (0.00 sec)
>
>The "optional" arguement in the <dtml-sqlvar zipcode> should not
>even rendered if zipcode was blank right?
>Does anyone have any ideas on how I can make this work?
>  
>


What I recommend you do is let the database do the conversion from 
string to integer for you.  So *dont* use the zipcode:int conversion, 
but *do* use the "type='nb'" type in the sqlvar expression.