• 17408 views
  • 0 comments
  • loading...

SQLFORM provides a high-level API for building create, update and delete forms from an existing database table

SQLFORM provides a high-level API for building create, update and delete forms from an existing database table.
SQLFORM is 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.

The automatically generated form is more complex than the previous low-level form. First of all, it contains a table of rows, and each row has three columns. The first column contains the field labels (as determined from the db.person), the second column contains the input fields (and eventually error messages), and the third column is optional and therefore empty (it can be populated with the fields in the SQLFORM constructor).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.








#The signature for the SQLFORM constructor is the following:


SQLFORM(table, record=None, deletable=False, linkto=None,
upload=None, fields=None, labels=None, col3={}, submit_button='Submit',
delete_label='Check to delete:', showid=True, readonly=False,
comments=True, keepopts=[], ignore_rw=False, record_id=None,
formstyle='table3cols', buttons=['submit'], separator=': ', **attributes)

Attributes / Arguments

If deletable is set to True, the UPDATE form displays a "Check to delete" checkbox. The value of the label for this field is set via the delete_label argument.

SQLFORM(table, record=None, deletable=False)

submit_button sets the value of the submit button.

id_label sets the label of the record "id"

The "id" of the record is not shown if showid is set to False.

fields is an optional list of field names that you want to display. If a list is provided, only fields in the list are displayed.


fields = ['name']

labels is a dictionary of field labels. The dictionary key is a field name and the corresponding value is what gets displayed as its label. If a label is not provided, web2py derives the label from the field name (it capitalizes the field name and replaces underscores with spaces).

labels = {'name':'Your Full Name:'}

col3 is a dictionary of values for the third column. For example:

col3 = {'name':A('what is this?',
      _href='http://www.google.com/search?q=define:name')}

If set to True, displays the form as readonly

If set to True, displays the form as readonly


If set to False, does not display the col3 comments

Normally, for a create/update form, only fields marked as writable=True are shown, and for readonly forms, only fields marked as readable=True are shown. Setting ignore_rw=True causes those constraints to be ignored, and all fields are displayed. This is mostly used in the appadmin interface to display all fields for each table, overriding what the model indicates.

formstyle determines the style to be used when serializing the form in html. It can be:

  • "table3cols" (default),
  • "table2cols" (one row for label and comment, and one row for input),
  • "ul" (makes an unordered list of input fields),
  • "divs" (represents the form using css friendly divs, for arbitrary customization),
  • "bootstrap" which uses the bootstrap form class "form-horizontal".
formstyle can also be a function which generates everything inside the FORM tag. You pass to your form constructor function two arguments, form and fields. Hints can be found in the source code file sqlhtml.py (look for functions named formstyle_)

buttons is a list of INPUTs or TAG.buttons (though technically could be any combination of helpers) that will be added to a DIV where the submit button would go.

#For example, adding a URL-based back-button (for a multi-page form) and a renamed submit button:

buttons = [TAG.button('Back',_type="button",_onClick = "parent.location='%s' " % URL(...),
             TAG.button('Next',_type="submit")]

#or a button which links to another page:

buttons = [..., A("Go to another page",_class='btn',_href=URL("default","anotherpage"))]

separator sets the string that separates form labels from form input fields.

When a dictionary is passed as hidden, its items are translated into "hidden" INPUT fields

form = SQLFORM(...,hidden=...)

Optional attributes are arguments starting with underscore that you want to pass to the FORM tag that renders the SQLFORM object.

_action = '.'

Optional attributes are arguments starting with underscore that you want to pass to the FORM tag that renders the SQLFORM object

_method = 'POST'

linkto is optional URLs to user-defined controllers that allow the form to deal with reference fields

upload are optional URLs to user-defined controllers that allow the form to deal with reference fields


Related References


Examples

loading...

Sources / Reference

" Web2py Book - Chapter 7 Forms and Validators" is licensed under Creative Common License BY-NC-ND 3.0


Comments

loading...

Powered by
Web2py

Hosted on
www.pythonanywhere.com
(affiliated link)