• 7950 views
  • 0 comments
  • loading...

Checks that the field values are in a set

In SQLFORM (and the grids) this validator will automatically set the form field to an option field (ie, with a drop-down menu).

IS_IN_SET checks that the field values are in a set:

The elements of the set can be combined with an numerical validator, as long as IS_IN_SET is first in the list. Doing so wil force conversion by the validtor to the numerical type. So, IS_IN_SET can be followed by IS_INT_IN_RANGE (which converts the value to int) or IS_FLOAT_IN_RANGE (which converts the value to float). For below example 2.

Checkbox validation


To force a filled-in form checkbox (such as an acceptance of terms and conditions), see example 3.

Dictionaries and tuples with IS_IN_SET


You may also use a dictionary or a list of tuples to make the drop down list more descriptive.















# Example 1

requires = IS_IN_SET(['a', 'b', 'c'],zero=T('choose one'),
error_message='must be a or b or c')

# Example 2
requires = [ IS_IN_SET([2, 3, 5, 7],IS_INT_IN_RANGE(0, 8),
error_message='must be prime and less than 10')]

# Example 3
requires=IS_IN_SET(['on'])

# Example 4
requires = IS_IN_SET({'A':'Apple','B':'Banana','C':'Cherry'},zero=None)

requires = IS_IN_SET([('A','Apple'),('B','Banana'),('C','Cherry')])

Attributes / Arguments

Displays an error message.

requires = [ ...   error_message='Some message')]

 If set to True, multiple values can be stored in one field. The field should be of type list:integer or list:string. multiple references are handled automatically in create and update forms, but they are transparent to the DAL. We strongly suggest using the jQuery multiselect plugin to render multiple fields.

Note that when multiple=True, IS_IN_SET will accept zero or more values, i.e. it will accept the field when nothing has been selected. multiple can also be a tuple of the form (a,b) where a and b are the minimum and (exclusive) maximum number of items that can be selected respectively.

Empty option

db.table_name.field_name.requires = IS_EMPTY_OR(IS_IN_SET(['en', 'es','eu'], zero=T('None')))


loading...

Sources / Reference

" Web2py Book"


Comments

loading...

Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)