Hello, I had the same problem and I've just found an elegant solution for 
it. First, you neet to notice that before any validation, the values of the 
fields travel to the model, so, in the model you can manipulate them throug 
the *request.vars* . After that everything else is easy: Just create a 
validator of your own, extract the values of the fields and use them as you 
like. An example will do better:


Let's supose you have a table with two fields:

dbOBJECT.define_table("example",
                      Field("high_number",
                            "integer",
                            label= T('High number'),
                            
requires=[IS_NOT_EMPTY(T(stringBuilder.emptyField))]),
                      Field("other_number",
                            "integer",
                            label= T('Lower number'),
                            
requires=IS_NOT_EMPTY(T(stringBuilder.emptyField))))

And you need the second number to be lower than the first. For explanation 
purposes let's create a *custom validator*

class VALIDATE_NUMBERS(object):
    def __init__(self, error_message="Error, this value can't be greather 
than the above one"):
        self.error_message = error_message

    def __call__(self, value):
        error = None
        high_number=request.vars['high_number']
        if high_number < value:
            error = self.error_message
        return (value, error)
Well, the other thing to do is include this validation on the model:

dbOBJECT.define_table("example",
                      Field("high_number",
                            "integer",
                            label= T('High number'),
                            
requires=[IS_NOT_EMPTY(T(stringBuilder.emptyField))]),
                      Field("other_number",
                            "integer",
                            label= T('Lower number'),
                            *requires=IS_NOT_EMPTY 
[(T(stringBuilder.emptyField)), VALIDATE_NUMBERS()]))*
The magic is done!!! Enjoy...



El lunes, 16 de septiembre de 2013, 8:49:08 (UTC+2), Hadi Sunyoto escribió:
>
> from: 
> http://www.web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators
>
> Validators with dependencies
>
> Usually validators are set once for all in models.
>
> Occasionally, you need to validate a field and the validator depends on 
> the value of another field. This can be done in various ways. It can be 
> done in the model or in the controller.
>
>
>
> There is an example validation done in controller but there is no example 
> validation done in model
>
> My table:
>
> db.define_table('config',
>     Field('config_name', 'string', length=255, required=True, unique=True),
>     Field('convert_option', 'string', length=255,
>         requires=IS_IN_SET(CONVERSION, zero=None)),
>     Field('config_value', 'string', length=255, required=True),
>     Field('default_value', 'string', length=255, required=True))
>
> I want to validate if config_value is greater than default value (for 
> example), but i don't want to do it in SQLFORM or FORM.
>
> or is it a bad idea to put validation that depend on other fields in 
> "model" rather than form?
>
> Thank you
>
> Hadi
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
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/d/optout.

Reply via email to