Overview

Next to freeform topic contents, each topic can store additional data in name/value pairs.

Topic data is normally not visible when you view a topic (except for a small table at the bottom of the topic - dependent on the used skin). Topic data works "behind the scenes" and facilitates searches, reports and custom displays.

Topic data, or better: structured data, can be used in many ways. The Foswiki Support questions serves as a demonstration how topic data can be used:

To create a complete list of "Support Questions" topics

To show a subset of all questions that have not been answered yet

To display the title and subject of each question

Another uses for structured data could be:

Create a list of all contributions of one particular author

Create a quick report of all employee names and phone numbers

Create a software documentation repository

Create and track tasks

Create a bug tracker

To work with topic data, you will need 3 things:

The data definition, specified in a table in a "data form" topic. The table lists all fields and their types - see The data definition.

The web's WebPreferences needs to list the form in the WEBFORMS setting - see Enabling forms.

Sometimes new users with a web programming background are confused how "data forms" and "HTML forms" are related. They are not related. But you'll see later on that you can use web forms to pass data to a topic data form.

The data definition

Topics can store data as name/value pairs, or form fields. The attributes of each form field are specified in the data definition, which is an ordinary topic.

The name of the data form topic usually ends with "Form". For example, the form topic for the Support Questions is named "QuestionForm". The form topic can be placed in any web, but usually this is in the same web as the topics that will be using it.

Topic types
You could consider the data form topic as the data type. For instance, all topics that use the form QuestionForm are topics of type "Question".

A useful core feature of Foswiki 1.1 is the automatic selection of view and edit templates based on the name of the form attached to a topic. With this we are really starting build up a topic as something resembling a typed object: its form name being the type identifier, and its form+templates as the details of its implementation. See AutoViewTemplatePlugin for details of this feature.

General Notes:

The topic definition is not read when a topic is viewed.

Form definition topics can be protected in the usual manner, using AccessControl, to limit who can change the form definition and/or individual value lists. Note that view access is required to be able to edit topics that use the form definition, though view access to the form definition is not required to view a topic where the form has been used.

The form table

A form is to a web as a table is to a database. -- Andrew Steele

The data form table is a kind of spreadsheet:

Each row of the table specifies one form field

The table header defines what attributes of the form fields need to be specified

Header row

Each column name in the table header row is one element of an entry field:

Form field rows

The data type: text, date, single or multi-value, labels (read-only). The type also defines how form field data can be entered in the edit screen, such as text field or radio buttons.

Size

The input size of the form field inputs on the edit screen.

Values

For checkboxes, radio buttons and dropdown lists: predefined input to select from. More advanced: this can be a dynamically generated list of values.

Tooltip message

(hardly used or useful anymore) A message that will be displayed when the cursor is hovered over the field in edit view.

Attributes

Whether the field is mandatory or hidden in view mode.

As said, only Name, Type and Size are required.

Form field attributes

Name

The name of the form field.

Names have to be unique for each data definition.

A very few field names are reserved. If you try to use one of these names, Foswiki will automatically append an underscore to the name when the form is used. But do not use the field name undefined (or any variant of that name, such as UnDefined), as that name is reserved for use in search queries.

You can space out the title of the field, and it will still find the topic e.g. Aeroplane Manufacturers is equivalent to AeroplaneManufacturers.

If a label field has no name, it will not be shown when the form is viewed, only when it is edited.

Field names can in theory include any text, but you should stick to alphanumeric characters. If you want to use a non-wikiname for a select, checkbox or radio field, and want to get the values from another topic, you can use [[...]] double bracket links. This notation can also be used when referencing another topic to obtain field values, but a name other than the topic name is required as the name of the field.

If you want the Field name to include embedded spaces, use the format [<nop>[FieldName][Descriptive human-friendly Field Name]].

Leading and trailing spaces do not matter.

Type

The data type defines the kind of input: text, date, single or multi-value, or labels (read-only). This is done by setting the type of interface control on the edit screen: checkbox, radio button, text field, and so on.

The control appearance is also specified by size and (initial) value. More on those attributes below.

Type

Description

Size attribute

Value attribute

Modifiers

checkbox

One or more checkboxes.

How many checkboxes will be displayed on each line.

A comma-separated list of item labels.

checkbox+buttons will add Set and Clear buttons to the basic checkbox type. checkbox+values allows the definition of values that are different to the displayed text.

date

A single-line text box and a calendar icon button next to it; clicking on the button will bring up a calendar from which the user can select a date. The date can also be typed into the text box.

The text box width in characters.

The initial text.

label

Read-only label text.

The text of the label.

radio

Like checkbox except that radio buttons are mutually exclusive; only one can be selected.

radio+values allows the definition of values that are different to the displayed text.

select

A select box / dropdown.

A fixed size for the box (e.g. 1, or a range e.g. 3..10. To get a dropdown, use size 1. If you specify a range, the box will never be smaller than 3 items, never larger than 10, and will be 5 high if there are only 5 options. Caution size 1 dropdown is incompatible with select+multi modifier on some browsers.

A comma-separated list of options for the box.

select+multi turns multiselect on for the select, to allow Shift+Click and Ctrl+Click to select (or deselect) multiple items. select+values allows the definition of values that are different to the displayed text. You can combine these modifiers e.g. select+multi+values

text

A one-line text field.

The text box width in number of characters.

The initial (default) content when a new topic is created with this form definition.

Note to extension developers
Such extended data types are single-valued (can only have one value) with the following exceptions:

any type name starting with checkbox

any type name with +multi anywhere in the name

Types with names like this can both take multiple values.

Size

The input size of the form field inputs on the edit screen. The size acts a bit different for each type - see the Type table above.

Values

For checkboxes, radio buttons and dropdown lists: predefined input to select from. More advanced: this can be a dynamically generated list of values.

The field value will be used to initialize a field when a form is created, unless specific values are given by the topic template or query parameters. The first item in the list for a select or radio type is the default item. For label, text, and textarea fields the value may also contain commas. checkbox fields cannot be initialized through the form definition.

Leading and trailing spaces do not matter.

Field values can also be generated through a FormattedSearch, which must yield a suitable table as the result.

Macros in the initial values of a form definition get expanded when the form definition is loaded.

If you want to use a | character in the initial values field, you have to precede it with a backslash, thus: \|.

You can use <nop> to prevent macros from being expanded.

The Format tokens can be used to prevent expansion of other characters.

You are not expected to write these kind of search expressions yourself, but if you like you can find more of these in Search Pattern Cookbook.

Fields and linefeeds
Some browsers may strip linefeeds from text fields when a topic is saved. If you need linefeeds in a field, make sure it is a textarea.

Tooltip message

(hardly used or useful anymore) A message that will be displayed when the cursor is hovered over the field in edit view.

Attributes

Whether the field is mandatory or hidden in view mode.

H

Indicates that this field should not be shown in view mode. However, the field is available for editing and storing information.

M

Indicates that this field is mandatory. The topic cannot be saved unless a value is provided for this field. If the field is found empty during topic save, the user is presented with an error message. Mandatory fields are indicated by an asterisk next to the field name.

Multiple attributes can be entered, separated by spaces:

| TopicTitle | text | 100 | | | H M |

Enabling forms

Before connecting topics to a data definition, the definition must be enabled in the Web's WebPreferences topic.

This is done by adding the form topic name to the WEBFORMS setting. The setting accepts a comma-separated list of form topics:

Changing a form

You can change a form definition, and Foswiki will try to make sure you don't lose any data from the topics that use that form.

If you change the form definition, the changes will not take affect in a topic that uses that form until you edit and save it.

If you add a new field to the form, then it will appear next time you edit a topic that uses the form.

If you delete a field from the form, or change a field name, then the data will not be visible when you edit the topic (the changed form definition will be used). If you save the topic, the old data will be lost (though thanks to revision control, you can always see it in older versions of the topic)

If two people edit the same topic containing a form at exactly the same time, and both change fields in the form, Foswiki will try to merge the changes so that no data is lost.

Searching in form data

The best way to search in form data is using the structured query language in the SEARCH macro.