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)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py Web Framework" group.
To post to this group, send email to web2py@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to