.. include:: hyperlinks.txt .. _customize form: Customize Forms =============== Widgets associate to fields of a given table are grouped in a form. It is the main interface to create, duplicate, update or delete record in the database. By default, fields are displayed on the top of each others. It is possible to modified theirs look and feel and theirs layout in many different ways. The recommend practice is to do it in the ``ui`` file associated to each table, *e.g.* ``ui_people.py``. Form configuration ------------------- A form is an instance of the ``Dbui.form.Panel`` class which derives from the ``Ext.form.Panel``. The former mainly embedded the logic to exchange date with the server. Its configuration parameters are those of the base class. The form can be customized in the python model using the ``Ext.form.Panel`` configuration parameters described in the `Ext JS API`_ documentation. To configure the form, instantiate the ``FormModifier`` and use the method ``configure``. Its keyword arguments corresponds to the configuration parameters of the ``Ext.form.Panel`` class. There are no limitation on the number of keyword arguments which can be used:: formModifier = dbui.FormModifier('mytable') formModifier.configure(labelAlign='right', width=400, height=380) Tooltips -------- Tooltips can be displayed when the mouse hover field widgets. Two steps are required to activate this functionality: * Set the attribute ``comment`` when modeling the database table:: tp_lastname = "Family name, i.e Doe" db.define_table("people", Field("first_name", "string", notnull=True), Field("last_name", "string", notnull=True, comment=tp_lastname), Field("note", "text"), migrate="people.table") * Activate the tool tips for all forms in the file ``common_settings.py``:: tables = ['people', 'countries'] dbui.configure_forms(tables, plugins=['pFormToolTip']) Line up Fields in Form ---------------------- The table ``history`` contains two fields ``period_start`` and ``period_end``. In the form, each field appears on top of each others, but it would be better to group them together on the same line:: fieldsModifier = dbui.FieldsModifier('history') fieldsModifier.merge_fields('period_start', 'period_end', fieldLabel=T('Period')) In this example, the line start with the label ``Period``. The labels of each sub field are hidden. Grouping Fields in Form using TabPanel -------------------------------------- Field widgets are displayed in a row on top of each others in the default layout. This is well suited for table with few fields by it is not appropriated for table with many fields. In the latter case, it is better to group fields within a ``Ext.tab.Panel``. The tab panel is a widget displaying several tabs. Each of them has a title and can contains few fields. Take the model of a table describing person and their addresses:: db.define_table("people", Field("first_name", "string"), Field("last_name", "string"), Field("address", "string"), Field("city", "string"), Field("postal_code", "string"), migrate="people.table") You would like to group ``first_name`` and ``last_name`` in a tab name *Contact* and the other ones in a tab name *Private*:: formModifier = dbui.FormModifier('people') formModifier.merge_fields('first_name', 'last_name', title=T('Contact')) formModifier.merge_fields('address', 'city', 'postal_code', title=T('Private')) formModifier.set_mapper(dbui.map_tabpanel) formModifier.configure(width=300) Add plugins to form ------------------- Javascript plugins have been designed to enhance the functionalities of the form: * ``pFormToolTip`` to active the field tooltips. Plugins can be applied to list of *tables* in one go:: dbui.configure_forms(tables, plugins=['pFormToolTip']) or to a single form:: formModifier = dbui.FormModifier('mytable') formModifier.configure(plugins=['pFomPlugin1', 'pFormPlugin2'])