web2py/web2py — pull request #45 by lucadealfaro (2012-12-08 00:45:52+00:00)
Title: Added required=True argument to IS_IN_DB

Added an optional argument

required=

(default: True)

to the IS_IN_DB validator. If set to False, then the validator return value None (and no error) in case no value has been selected.

Rationale:

Some fields in databases are not required. Thus, it makes sense to have an option so that the IS_IN_DB validator checks that either the value is in the specified DB set (as now), _or_, the selection has been left empty.

For instance, assume that I have a field as follows:

Field('teaching_assistant', db.person),

The database accepts a None value (because db....teaching_assistant.required has not been set to True). So if I have a set of possible TAs q (as defined by a query), and I want the teaching assistant to be either selected from q, or left blank, I can now use:

IS_IN_DB(q, required=False)

to allow leaving the TA choice empty.