Discussion:
how to validate field that depend on each other in the model rather than form
Hadi Sunyoto
2013-09-16 06:49:08 UTC
Permalink
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 web2py+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/***@public.gmane.org
For more options, visit https://groups.google.com/groups/opt_out.
mcamel
2015-07-31 18:34:51 UTC
Permalink
I'm wondering just the same. How can this be done in model?.

Particularly I want to use IS_IN_DB on a referenced field based on a query
filtering by the value of another field of the table.

I've tried this but didn't work:
db.table1.field1.requires = lambda x,row: IS_IN_DB(db(db.table2.field2==row.
field2)), 'table2.id')

Nor did this one:
db.table1.field1.requires = IS_IN_DB(db(db.table2.field2==db.table1.field2
)), 'table2.id')

where field1 is field of table1:
Field('field1', 'reference table2')


Regards.
Post by Hadi Sunyoto
http://www.web2py.com/books/default/chapter/29/07/forms-and-validators#Database-validators
<http://www.google.com/url?q=http%3A%2F%2Fwww.web2py.com%2Fbooks%2Fdefault%2Fchapter%2F29%2F07%2Fforms-and-validators%23Database-validators&sa=D&sntz=1&usg=AFQjCNE4_4fAPTYEpCZJVvLy1ejv7Tcocw>
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
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 web2py+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Anthony
2015-07-31 19:11:22 UTC
Permalink
Post by Hadi Sunyoto
There is an example validation done in controller but there is no example
validation done in model
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 web2py+***@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Continue reading on narkive:
Loading...