Here is a list of available web2py widgets:

1 SQLFORM.widgets.string.widget

2 SQLFORM.widgets.text.widget

3 SQLFORM.widgets.password.widget

4 SQLFORM.widgets.integer.widget

5 SQLFORM.widgets.double.widget

6 SQLFORM.widgets.time.widget

7 SQLFORM.widgets.date.widget

8 SQLFORM.widgets.datetime.widget

9 SQLFORM.widgets.upload.widget

10 SQLFORM.widgets.boolean.widget

11 SQLFORM.widgets.options.widget

12 SQLFORM. widgets .multiple. widget

13 SQLFORM .widgets.radio.widget

14 SQLFORM.widgets.checkboxes.widget

The first ten of them are the defaults for the corresponding field types.

The "options" widget is used when a field's requires is is_in_set or is_in_db with multiple=False (default behavior). The "multiple" widget is used when a field's requires is is_in_set or is_in_db with muitipie=True. The "radio" and "checkboxes" widgets are never used by default, but can be set manually. For example, to have a "string" field represented by a textarea:

1 Field('comment', 'string', widget=SQLFORM.widgets.text.widget)

You can create new widgets or extend existing widgets; in fact,

SQLFORM . widgets [type] is a class and SQLFORM.widgets [type] .widget is a static member function of the corresponding class. Each widget function takes two arguments: the field object, and the current value of that field. It returns a representation of the widget. As an example, the string widget could be recoded as follows:

1 def my_string_widget(field, value):

2 return INPUT(_name=field.name,

3 _id="%s_%s\" % (field._tablename, field.name),

4 _class=field.type,

5 _value=value,

6 requires=field.requires)

8 Field('comment', 'string', widget=my_string_widget)

The id and class values must follow the convention described later in this chapter. A widget may contain its own validators, but it is good practice to associate the validators to the "requires" attribute of the field and have the widget get them from there.

Was this article helpful?

+1 -2

Post a comment