Revert
[openafs-wiki.git] / TWiki / TWikiForms.mdwn
1 # <a name="TWiki Forms"></a> TWiki Forms
2
3 _Add structure to content with forms attached to twiki topics. TWiki forms (with form fields) and formatted search are the base for building database applications._
4
5 <div>
6   <ul>
7     <li><a href="#TWiki Forms"> TWiki Forms</a><ul>
8         <li><a href="#Overview"> Overview</a></li>
9         <li><a href="#Defining a Form"> Defining a Form</a></li>
10         <li><a href="#Enabling Forms by Web"> Enabling Forms by Web</a></li>
11         <li><a href="#Adding a form to a topic"> Adding a form to a topic</a></li>
12         <li><a href="#Changing a form"> Changing a form</a></li>
13         <li><a href="#Structure of a Form Template"> Structure of a Form Template</a><ul>
14             <li><a href="#Values in Other Topics"> Values in Other Topics</a></li>
15           </ul>
16         </li>
17         <li><a href="#Extending the range of form data"> Extending the range of form data types</a></li>
18         <li><a href="#Hints and Tips"> Hints and Tips</a><ul>
19             <li><a href="#Build an HTML form to create new"> Build an HTML form to create new Form-based topics</a></li>
20             <li><a href="#Searching for Form Data"> Searching for Form Data</a></li>
21             <li><a href="#Gotcha!"> Gotcha!</a></li>
22           </ul>
23         </li>
24       </ul>
25     </li>
26   </ul>
27 </div>
28
29 ## <a name="Overview"></a> Overview
30
31 By adding form-based input to freeform content, you can structure topics with unlimited, easily searchable categories. A form is enabled for a web and can be added to a topic. The form data is shown in tabular format when the topic is viewed, and can be changed in edit mode using edit fields, radio buttons, check boxes and list boxes. Many different form types can be defined in a web, though a topic can only have one form attached to it at a time.
32
33 Typical steps to build an application based on TWiki forms:
34
35 1. Define a form template
36 2. Enable the form for a web
37 3. Add the form to a [[template topic|Main/TWikiTemplates#Template_Topics]]
38 4. Build an HTML form to create new topics based on that template topic
39 5. Build a [[FormattedSearch]] to list topics that share the same form
40
41 ## <a name="Defining a Form"></a> Defining a Form
42
43 A Form Template specifies the fields in a form. A Form Template is simply a page containing a TWiki table, where each row of the table specifies one form field.
44
45 1. Create a new topic with your form name: <code>**YourForm**</code>, <code>**ExpenseReportForm**</code>, <code>**InfoCategoryForm**</code>, <code>**RecordReviewForm**</code>, whatever you need.
46 2. Create a TWiki table, with each column representing one element of an entry field: <code>**Name**</code>, <code>**Type**</code>, <code>**Size**</code>, <code>**Values**</code>, <code>**Tooltip message**</code>, and <code>**Attributes**</code> _(see sample below)_.
47 3. For each field, fill in a new line; for the type of field, select from the list.
48 4. Save the topic _(you can later choose to [[enable/disable|Main/WebHome#EnablingForms]] individual forms)_.
49
50 > **Example:**
51 >
52 > %BR%
53 >
54 > `| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* |`
55 >
56 > %BR%
57 >
58 > `| TopicClassification | select | 1 | NoDisclosure, PublicSupported, PublicFAQ | blah blah... |   |`
59 >
60 > %BR%
61 >
62 > `| OperatingSystem | checkbox | 3 | OsHPUX, OsLinux, OsSolaris, OsWin | blah blah... |   |`
63 >
64 > %BR%
65 >
66 > `| OsVersion | text | 16 | | blah blah... |   |`
67 >
68 > <table border="1" cellpadding="0" cellspacing="0">
69 >   <tr>
70 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
71 >     <th bgcolor="#99CCCC"><strong> Type </strong></th>
72 >     <th bgcolor="#99CCCC"><strong> Size </strong></th>
73 >     <th bgcolor="#99CCCC"><strong> Values </strong></th>
74 >     <th bgcolor="#99CCCC"><strong> Tooltip message </strong></th>
75 >     <th bgcolor="#99CCCC"><strong> Attributes </strong></th>
76 >   </tr>
77 >   <tr>
78 >     <td>[[Sandbox/TopicClassification]]</td>
79 >     <td> select </td>
80 >     <td align="center"> 1 </td>
81 >     <td>[[Sandbox/NoDisclosure]], [[Sandbox/PublicSupported]], [[Sandbox/PublicFAQ]]</td>
82 >     <td> blah blah... </td>
83 >     <td>   </td>
84 >   </tr>
85 >   <tr>
86 >     <td>[[Sandbox/OperatingSystem]]</td>
87 >     <td> checkbox </td>
88 >     <td align="center"> 3 </td>
89 >     <td>[[Sandbox/OsHPUX]], [[Sandbox/OsLinux]], [[Sandbox/OsSolaris]], [[Sandbox/OsWin]]</td>
90 >     <td> blah blah... </td>
91 >     <td>   </td>
92 >   </tr>
93 >   <tr>
94 >     <td>[[Sandbox/OsVersion]]</td>
95 >     <td> text </td>
96 >     <td align="center"> 16 </td>
97 >     <td>   </td>
98 >     <td> blah blah... </td>
99 >     <td>   </td>
100 >   </tr>
101 > </table>
102
103 See [[structure of a form|Main/WebHome#FormStructure]] for full details of what types are available and what all the columns mean.
104
105 You can also retrieve possible values for `select`, `checkbox` or `radio` types from other topics:
106
107 > **Example:**
108 >
109 > - In the WebForm topic, define the form: <br /><table border="1" cellpadding="0" cellspacing="0">
110 >   <tr>
111 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
112 >     <th bgcolor="#99CCCC"><strong> Type </strong></th>
113 >     <th bgcolor="#99CCCC"><strong> Size </strong></th>
114 >     <th bgcolor="#99CCCC"><strong> Values </strong></th>
115 >     <th bgcolor="#99CCCC"><strong> Tooltip message </strong></th>
116 >     <th bgcolor="#99CCCC"><strong> Attributes </strong></th>
117 >   </tr>
118 >   <tr>
119 >     <td> TopicClassification </td>
120 >     <td> select </td>
121 >     <td> 1 </td>
122 >     <td>   </td>
123 >     <td> blah blah... </td>
124 >     <td>   </td>
125 >   </tr>
126 >   <tr>
127 >     <td> OperatingSystem </td>
128 >     <td> checkbox </td>
129 >     <td> 3 </td>
130 >     <td>   </td>
131 >     <td> blah blah... </td>
132 >     <td>   </td>
133 >   </tr>
134 >   <tr>
135 >     <td> OsVersion </td>
136 >     <td> text </td>
137 >     <td> 16 </td>
138 >     <td>   </td>
139 >     <td> blah blah... </td>
140 >     <td>   </td>
141 >   </tr>
142 > </table>
143 > <br /> %X% Leave the <code>**Values**</code> field **blank**.
144 >
145 > - Then in the TopicClassification topic, define the possible values: <br />`| *Name*            |` %BR% `| NoDisclosure      |` %BR% `| Public Supported  |` %BR% `| Public FAQ        |` %BR% <table border="1" cellpadding="0" cellspacing="0">
146 >   <tr>
147 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
148 >   </tr>
149 >   <tr>
150 >     <td> NoDisclosure </td>
151 >   </tr>
152 >   <tr>
153 >     <td> Public Supported </td>
154 >   </tr>
155 >   <tr>
156 >     <td> Public FAQ </td>
157 >   </tr>
158 > </table>
159
160 Field values can also be set using the result of expanding other TWiki variables. For example,
161
162 > `%SEARCH{"Office$" scope="topic" web="%USERSWEB%" nonoise="on" type="regex" format="$web.$topic" separator=", " }%`
163
164 When used in the value field of the form definition, this will find all topic names in the Main web which end in "Office" and use them as the legal field values.
165
166 <a name="EnablingForms"></a>
167
168 ## <a name="Enabling Forms by Web"></a> Enabling Forms by Web
169
170 Forms have to be enabled for each individual web. The <code>**WEBFORMS**</code> variable in [[WebPreferences]] is optional and defines a list of possible form templates.
171
172 > **Example:**
173 >
174 > - Set WEBFORMS = BugForm, FeatureForm, Books.BookLoanForm
175
176 - With <code>**WEBFORMS**</code> enabled, an extra button is added to the edit view. If the topic doesn't have a Form, an **Add Form** button appears at the end of the topic. If a Form is present, a **Change** button appears in the top row of the Form. The buttons open a screen that enables selection of a form specified in `WEBFORMS`, or the **No form** option.
177 - You have to list the available form topics explicitly. You cannot use a `SEARCH` to define `WEBFORMS`.
178
179 ## <a name="Adding a form to a topic"></a> Adding a form to a topic
180
181 - Edit the topic and follow the "Add form" button to add a Form. This is typically done to a [[template topic|Main/TWikiTemplates#TemplateTopic]], either to the `WebTopicEditTemplate` topic in a web, or a new topic that serves as an application specific template topic. Initial Form values can be set there.
182
183 - Additionally a new topic can be given a Form using the `formtemplate` parameter in the (edit or save) URL. Initial values can then be provided in the URLs or as form values:
184   - other than checkboxes: <code>**name**</code>, ex: <code>**?BugPriority=1**</code>
185   - checkbox: <code>**namevalue=1**</code>, ex: <code>**?ColorRed=1**</code>. <br /> Boxes with a tick must be specified.
186   - Example: This will add a textfield for the new topic name and a "Create"-Button to your topic. When the button is pressed, the topic editor will open with the form "MyForm" already attached to the new topic.
187         <form name="newtopic" action="%SCRIPTURLPATH{"edit"}%/%WEB%/">
188            <input type="hidden" name="formtemplate" value="MyForm" />
189            New topic name <input type="text" name="topic" size="40" />
190            <input type="submit" class="twikiSubmit" value="Create" />
191         </form>
192 - **_%T% Tip:_** For TWiki applications you can [[automatically generate unique topicnames|Main/TWikiTemplates#AutomaticallyGeneratedTopicname]].
193
194 - **_%X% Note:_** Initial values will **not** be set in the form of a new topic if you _only_ use the formtemplate parameter.
195
196 ## <a name="Changing a form"></a> Changing a form
197
198 - You can change a form definition, and TWiki will try to make sure you don't lose any data from the topics that use that form.
199
200 - 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.
201
202 - If you add a new field to the form, then it will appear next time you edit a topic that uses the form.
203
204 - 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)
205
206 - If two people edit the same topic containing a form at exactly the same time, and both change fields in the form, TWiki will try to merge the changes so that no data is lost.
207
208 <a name="FormStructure"></a>
209
210 ## <a name="Structure of a Form Template"></a> Structure of a Form Template
211
212 A Form Template specifies the fields in a form. A Form Template is simply a page containing a TWiki table, where each row of the table specifies one form field.
213
214 Each **column** of the table is one element of an entry field: <code>**Name**</code>, <code>**Type**</code>, <code>**Size**</code>, <code>**Values**</code>, <code>**Tooltip message**</code>, and <code>**Attributes**</code>.
215
216 The `Name`, `Type` and `Size` columns are required. Other columns are optional. The form **must** have a header row (e.g. `| *Name* | *Type* | *Size* |`).
217
218 <code>**Name**</code> is the name of the form field.
219
220 The <code>**Type**</code>, <code>**Size**</code> and <code>**Value**</code> fields describe the legal values for this field, and how to display them.
221
222 - <code>**Type**</code> `checkbox` specifies one or more checkboxes. The `Size` field specifies how many checkboxes will be displayed on each line. The `Value` field should be a comma-separated list of item labels.
223   - <code>**Type**</code> `checkbox+buttons` will add **Set** and **Clear** buttons to the basic `checkbox` type.
224 - <code>**Type**</code> `radio` is like `checkbox` except that radio buttons are mutually exclusive; only one can be selected.
225 - <code>**Type**</code> `label` specifies read-only label text. The `Value` field should contain the text of the label.
226 - <code>**Type**</code> `select` specifies a select box. The `Value` field should contain a comma-separated list of options for the box. The `Size` field can specify a fixed size for the box (e.g. `1`, or a range e.g. `3..10`. If you specify a range, then the box will never be smaller than 3 items, never larger than 10, and will be 5 high if there are only 5 options.
227   - There are two modifiers that can be applied to the `select` type:
228     - `select+multi` turns multiselect on for the select, to allow Shift+Click and Ctrl+Click to select (or deselect) multiple items.
229     - `select+values` allows the definition of values that are different to the displayed text. For example:     | Field 9 | select+values | 5 | One, Two=2, Three=III, Four | Various values formats | shows <select size="2"><option>One</option>
230   <option>Two</option>
231   <option>Three</option>
232   <option>Four</option></select>
233  but the values or options `Two` and `Three` are `2` and `III` respectively.<br /> You can combine these modifiers e.g. `select+multi+values`
234 - <code>**Type**</code> `text` specifies a one-line text field. `Size` specifies the text box width in number of characters. `Value` is the initial (default) content when a new topic is created with this form template.
235 - <code>**Type**</code> `textarea` specifies a multi-line text box. The `Size` field should specify columns x rows, e.g. `80x6`; default size is 40x5. As for `text`, the `Value` field specifies the initial text
236 - <code>**Type**</code> `date` specifies a single-line text box and a 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. `Size` specifies the text box width in characters. As for `text`, the `Value` field specifies the initial text
237
238 <code>**Tooltip message**</code> is a message that will be displayed when the cursor is hovered over the field in `edit` view.
239
240 <code>**Attributes**</code> specifies special attributes for the field. Multiple attributes can be entered, separated by spaces.
241
242 - An attribute `H` indicates that this field should not be shown in view mode. However, the field is available for editing and storing information.
243 - An attribute `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, an error is raised and the user is redirected to an `oops` page. Mandatory fields are indicated by an asterisks next to the field name.
244
245 For example, a simple form just supporting entry of a name and a date would look as follows:
246
247     | *Name* | *Type* | *Size* |
248     | Name   | text   | 80     |
249     | Date   | date   | 30     |
250
251 **Field Name Notes:**
252
253 - Field names have to be unique.
254 - A very few field names are reserved. If you try to use one of these names, TWiki will automatically append an underscore to the name when the form is used.
255 - You can space out the title of the field, and it will still find the topic e.g. `Aeroplane Manufacturers` is equivalent to `AeroplaneManufacturers`.
256 - If a `label` field has no name, it will **not** be shown when the form is **viewed**, only when it is **edited**.
257 - 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 <code>**\[[...]]**</code> 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.
258 - Leading and trailing spaces are _not_ significant.
259
260 **Field Value Notes:**
261
262 - 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 template.
263 - Leading and trailing spaces are _not_ significant.
264 - Field values can also be generated through a %SYSTEMWEB%.FormattedSearch, which must yield a suitable table as the result.
265 - Variables in the initial values of a form definition get expanded when the form definition is loaded.
266   - If you want to use a `|` character in the initial values field, you have to precede it with a backslash, thus: `\|`.
267   - You can use `<nop>` to prevent TWiki variables from being expanded.
268   - The [[FormatTokens]] can be used to prevent expansion of other characters.
269
270 **General Notes:**
271
272 - The topic definition is not read when a topic is viewed.
273 - Form definition topics can be protected in the usual manner, using [[TWikiAccessControl]], to limit who can change the form template 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.
274
275 ### <a name="Values in Other Topics"></a> Values in Other Topics
276
277 As described above, you can also retrieve possible values for select, checkbox or radio types from other topics. For example, if you have a rows defined like this:
278
279     | *Name*                 | *Type* | *Size* |
280     | AeroplaneManufacturers | select |        |
281
282 the TWiki will look for the topic AeroplaneManufacturers to get the possible values for the `select`.
283
284 The AeroplaneManufacturers topic must contain a table, where each row of the table describes a possible value. The table only requires one column, <code>**Name**</code>. Other columns may be present, but are ignored.
285
286 For example:
287
288     | *Name* |
289     | Routan |
290     | Focke-Wulf |
291     | De Havilland |
292
293 **Notes:**
294
295 - The <code>**Values**</code> column **must be empty** in the referring form definition.
296
297 ## <a name="Extending the range of form data"></a> Extending the range of form data types
298
299 You can extend the range of data types accepted by forms by using [[TWikiPlugins]]. All such extended data types are single-valued (can only have one value) with the following exceptions:
300
301 - any type name starting with `checkbox`
302 - any type name with `+multi` anywhere in the name
303
304 Types with names like this can both take multiple values.
305
306 ## <a name="Hints and Tips"></a> Hints and Tips
307
308 ### <a name="Build an HTML form to create new"></a> Build an HTML form to create new Form-based topics
309
310 - New topics with a form are created by simple HTML forms asking for a topic name. For example, you can have a `SubmitExpenseReport` topic where you can create new expense reports, a `SubmitVacationRequest` topic, and so on. These can specify the required template topic with its associated form. [[Template topics|Main/TWikiTemplates#TemplateTopics]] has more.
311
312 A Form Template specifies the fields in a form. A Form Template is simply a page containing a TWiki table, where each row of the table specifies one form field. <a name="UsingFormData"></a>
313
314 ### <a name="Searching for Form Data"></a> Searching for Form Data
315
316 TWiki Forms accept user-input data, stored as [[TWikiMetaData]]. Meta data also contains program-generated info about changes, attachments, etc. To find, format and display form and other meta data, see [[TWikiMetaData]], <code>**FORMFIELD**</code>, <code>**SEARCH**</code> and <code>**METASEARCH**</code> variables in [[TWikiVariables]], and [[TWiki Formatted Search|Main/FormattedSearch]].
317
318 > **Example**
319 >
320 > %BR% TWiki users often want to have an overview of topics they contributed to. With the
321 >
322 > <code>**$formfield**</code>
323 >
324 > parameter it is easy to display the value of a classification field next to the topic link:
325 >
326 >     | *Topic* | *Classification* |
327 >     %SEARCH{"%USERSWEB%.UserName" scope="text" nosearch="on" nototal="on" order="modified" reverse="on"
328 >     format="|<b>[[$web.$topic][$topic]]</b> |<nop>$formfield(TopicClassification) |" web="Sandbox"}%
329
330 Searching forms this way is obviously pretty inefficient, but it's easy to do. If you want better performance, take a look at some of the structured wiki extensions that support higher performance searching e.g. TWiki:Plugins.DBCachePlugin.
331
332 ### <a name="Gotcha!"></a> Gotcha!
333
334 - 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`.
335
336 **_Related Topics:_** [[UserDocumentationCategory]], [[TWikiTemplates]]