| | 1 | = IS_ONE_OF Validator = |
| | 2 | |
| | 3 | The IS_ONE_OF() validator is a custom version of web2py's IS_IN_DB() for foreign key lookups in a key table. Other than IS_IN_DB, the IS_ONE_OF validator is deletion status sensitive, i.e. records in the key table that are flagged as 'deleted' will be ignored. |
| | 4 | |
| | 5 | Furthermore, it is possible to filter the key records by a type field (which should be int) for certain options |
| | 6 | |
| | 7 | Usage: |
| | 8 | db.table.field.requires = IS_NULL_OR(IS_ONE_OF(db, ''"keytable.keyfield"'', ''represent'', filterby=''"fieldname"'', filter_opts=''options'')) |
| | 9 | |
| | 10 | - ''keytable'' = Name of the key table |
| | 11 | - ''keyfield'' = Name of the key field |
| | 12 | - ''represent'' = how to build the option label (in drop-downs in forms) from the key entry, one of the following: |
| | 13 | - string template, e.g. "%(first_name)s %(last_name)s" |
| | 14 | - list of names of fields, e.g. ('first_name', 'last_name') |
| | 15 | - function or lambda, e.g. lambda r: "%(first_name)s %(last_name)s" % dict(r) |
| | 16 | - filterby=''"fieldname"'' = the type field (and int-field) to filter for |
| | 17 | - filter_opts=''"options"'' = a list or tuple of allowed values for the filterby-field |
| | 18 | |
| | 19 | If filterby is specified, the drop-down fields get sorted by this field. |
| | 20 | |
| | 21 | Example: |
| | 22 | {{{ |
| | 23 | ... |
| | 24 | db.Field('pr_pe_id', db.pr_pentity), |
| | 25 | ... |
| | 26 | |
| | 27 | db[table].pr_pe_id.requires = IS_NULL_OR(IS_ONE_OF(db, |
| | 28 | 'pr_pentity.id', |
| | 29 | shn_pentity_represent, |
| | 30 | filterby='opt_pr_pentity_class', |
| | 31 | filter_opts=(1,) |
| | 32 | )) |
| | 33 | }}} |