Thanks Bruno, works like a charm. Only caveat is that the
error_message is always grabbed from the first validator…

On Sat, Feb 16, 2013 at 6:21 AM, Bruno Rocha <[email protected]> wrote:
> Cool! but it does not allow the transformation.
>
> class CUSTOM(object):
>     """
>     you can use a function or a lambda
>     to validate or/and transform the field in the way you want
>     it is the same as "onvalidation" and "onsuccess" form callbacks
>     but it can be used per field in models or controller level
>     Usage:
>
>     # the validate function should return the error_message or None
>     def begins_with_a(value):
>         if not value.startswith('a'):
>             return "Should start with a"
>         else:
>             return None
>
>      # the transform function should return a value
>      def to_upper(value):
>          return value.upper()
>
>     db.define_table("foo", Field("bar"))
>     db.foo.bar.requires = CUSTOM(begins_with_a, to_upper)
>
>     >>> CUSTOM(begins_with_a, to_upper)('apple')
>     ('APPLE', None)
>     >>> CUSTOM(begins_with_a, to_upper)('orange')
>     ('ORANGE', 'Should start with a')
>
>     """
>     def __init__(self, validate=lambda value: None, transform=lambda value:
> value):
>         self.validate = validate
>         self.transform = transform
>
>     def __call__(self, value):
>         return(self.transform(value), self.validate(value))
>
>
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "web2py-users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

-- 

--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to