On Monday, September 16, 2013 at 2:49:08 AM UTC-4, Hadi Sunyoto wrote:
>
> 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?
>
You could use request.post_vars in the validator. For example, assuming
these values are integers:
Field('config_value', ...,
requires=IS_EXPR(lambda value: value > int(request.post_vars.
default_value)))
Assuming these fields store numbers, you should use the appropriate field
type (integer, double, decimal) rather than string.
Anthony
--
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.