Problem with zip code integer validation

470 views
Skip to first unread message

voltron

unread,
May 17, 2008, 5:29:52 AM5/17/08
to web2py Web Framework
I have been validating zip code fields for the USA and Germany like
this:

TR(TD(T("Zip"), _class="label"),
INPUT(_name="zip_code", _size="5", requires=[IS_LENGTH(5),
IS_FLOAT_IN_RANGE(999,99999)])),


Now, using the latest web2py, I get this error:

Error: invalid literal for int() with base 10: ''

Changing the zip_code field to a double did not help. This is starnge
as it used to work before.


Thanks

mdipierro

unread,
May 17, 2008, 8:49:52 AM5/17/08
to web2py Web Framework
I suggest using regular expressions since a zip code may start with 0,

IS_MATCH('\d{5}',error_message=T('Invalid zip code'))

Massimo

voltron

unread,
May 17, 2008, 9:18:34 AM5/17/08
to web2py Web Framework
Using your method, I still get this error:

Error: invalid literal for int() with base 10: ''

The database field declaration looks like this:

SQLField('zip_code', 'integer', length=10),

Massimo Di Pierro

unread,
May 17, 2008, 9:34:03 AM5/17/08
to web...@googlegroups.com
I see. A zip code cannot be integer because can start with 0. It has
to be a string. Moreover the length is ignored for integers and it is
only used for strings.

Massimo

voltron

unread,
May 17, 2008, 9:49:38 AM5/17/08
to web2py Web Framework
Thats what gets to me, it used to work before. Hmm this would be a
problem. The German post offers a dump of their zip code database, the
fields are declared as integers. One can also do some integer
calculation to do proximity guessing and such. With strings ,one would
have to cast the types before insertion or retrieval from the
database. Why does FLOAT not work? Floast can start with zero and the
precision be would be dsplayed

voltron

unread,
May 17, 2008, 9:57:29 AM5/17/08
to web2py Web Framework
I suspect something else because even validation an integer does not
work:

requires=[IS_NULL_OR(IS_INT_IN_RANGE(10000,99999,
error_message=T("Please check your value")))])),

Error: invalid literal for int() with base 10: ''




Massimo Di Pierro

unread,
May 17, 2008, 10:11:27 AM5/17/08
to web...@googlegroups.com
What value are you inserting? can you send me the ticket?
Did you set a default value in the form?

This is not your problem (yet) but int('01') will cause problems
because python thinks 01 is octal.

I do not see the problem in converting zip codes from int (as you
have them) into strings. zip codes must be treated as strings. You
can still do proximity searches because the fixed length ensures that
the strings are sorted in the same way as the integers.

Massimo

Massimo Di Pierro

unread,
May 17, 2008, 10:15:01 AM5/17/08
to web...@googlegroups.com
This:

db=SQLDB("sqlite://db.db")
db.define_table('test',SQLField('name','integer'))
db.test.name.requires=[IS_NULL_OR(IS_INT_IN_RANGE(10000,99999,error_message=T("Please check your value")))]

works great for me. Does it work for you?

voltron

unread,
May 17, 2008, 10:22:47 AM5/17/08
to web2py Web Framework
I do not get a ticket because the query is wrapped in a try block, I
get an exception instead:

try:
present_user = db(db.user_profiles.user_id ==
session.user_id).select()
print present_user[0].user_profiles.title
except Exception, detail:
print 'Error:', detail

I still think that there must be some way to validate integers. The
choice is not mine, the databases are created and updated by the
German post and the fields are integers. This is a commercial
databases that is constantly updated by them.


Thanks

Massimo Di Pierro

unread,
May 17, 2008, 1:19:52 PM5/17/08
to web...@googlegroups.com
from you other email it seem to me that IS_NULL_OR(IS_INT_RANGE())
works for you.
The code below does not call validators.

I suspect the problem is somewhere else. Are you using sqlite? have
you changed the type of the user_id field? It is possible there are
corrupt values in there.
Try getting a traceback.

Massimo

voltron

unread,
May 18, 2008, 4:22:09 PM5/18/08
to web2py Web Framework
The error was coming from another field, and this was not reflected in
the exception. I did not alter the table and I created the database
from scratch I have decided not to validate zip codes for the mean
time. I will check if the other frameworks have a reg-ex that can take
care of it. Thanks

Massimo Di Pierro

unread,
May 18, 2008, 4:29:40 PM5/18/08
to web...@googlegroups.com
I found that this works...


from cPickle import *
import copy_reg

class S(dict):
def __getattr__(self,key): return self[key]
def __setattr__(self, key, value): self[key]=value

class A(S):
def __init__(self): self.i=lambda u: u
#def __getstate__(self): return {}

def unserialize_A(d):
return A()

def serialize_A(o):
return unserialize_A, ({},)

copy_reg.pickle(A, serialize_A)

a=A()
print a.i(3)
x=dumps(a)
b=loads(x)
print b.i(4)


I guess the rule is do not trust getstate, use copy_reg instead.

Reply all
Reply to author
Forward
0 new messages