Forms and validators

There are four distinct ways to build forms in web2py:

  • FORM provides a low-level implementation in terms of HTML helpers. A FORM object can be serialized into HTML and is aware of the fields it contains. A FORM object knows how to validate submitted form values.
  • SQLFORM provides a high-level API for building create, update and delete forms from an existing database table.
  • SQLFORM.factory is an abstraction layer on top of SQLFORM in order to take advantage of the form generation features even if there is no database present. It generates a form very similar to SQLFORM from the description of a table but without the need to create the database table.
  • CRUD methods. These are functionally equivalent to SQLFORM and are based on SQLFORM, but provide a more compact notation; CRUD is now deprecated in favor of SQLFORM.grid() and SQLFORM.smartgrid().

All these forms are self-aware and, if the input does not pass validation, they can modify themselves and add error messages. The forms can be queried for the validated variables and for error messages that have been generated by validation.

Arbitrary HTML code can be inserted into or extracted from the form using helpers.

FORM and SQLFORM are helpers and they can be manipulated in a similar way as the DIV. For example you can set a form style:

  1. form = SQLFORM(..)
  2. form['_style']='border:1px solid black'