Validators: IS_LAT & IS_LON

5 views
Skip to first unread message

Fran

unread,
Nov 30, 2008, 5:44:10 PM11/30/08
to web2py Web Framework
I have a need to validate Geographic Coordinates.
I've therefore added 2 new validators based on the existing
IS_FLOAT_IN_RANGE.
Makes it just a little easier when coding & provides a nicer error
message to users.
Of any interest to others? (I'd love to see this added by default to
make my apps portable & save me re-hacking it in to my copy)

F

Procedure:
* add code below to gluon/validators.py
* add these functions to __all__ in the same file
* delete validators.pyc
* close web2py & start it up again

class IS_LAT(object):
"""
example:

INPUT(_type='text',_name='name',requires=IS_LAT())

latitude has to be in degrees between -90 & 90
"""
def __init__(self, error_message='Latitude/Northing should be
between -90 & 90!'):
self.minimum=-90
self.maximum=90
self.error_message = error_message
def __call__(self, value):
try:
value = float(value)
if self.minimum <= value <= self.maximum: return (value,
None)
except ValueError: pass
return (value, self.error_message)

class IS_LON(object):
"""
example:

INPUT(_type='text',_name='name',requires=IS_LON())

longitude has to be in degrees between -180 & 180
"""
def __init__(self, error_message='Longitude/Easting should be
between -180 & 180!'):
self.minimum=-180
self.maximum=180
self.error_message = error_message
def __call__(self, value):
try:
value = float(value)
if self.minimum <= value <= self.maximum: return (value,
None)
except ValueError: pass
return (value, self.error_message)

mdipierro

unread,
Nov 30, 2008, 6:11:09 PM11/30/08
to web2py Web Framework
Thank you. I will add an alterEgo entry.
Reply all
Reply to author
Forward
0 new messages