buildrelease
[openafs-wiki.git] / TWiki / TWikiForms.mdwn
1 <div>
2   <ul>
3     <li><a href="#TWiki Forms"> TWiki Forms</a><ul>
4         <li><a href="#Overview"> Overview</a></li>
5         <li><a href="#Defining a Form Template"> Defining a Form Template</a><ul>
6             <li><a href="#Form Template Elements"> Form Template Elements</a></li>
7             <li><a href="#Defining a Form"> Defining a Form</a></li>
8           </ul>
9         </li>
10         <li><a href="#Enabling Forms by Web"> Enabling Forms by Web</a></li>
11         <li><a href="#Add a form to a topic"> Add a form to a topic</a></li>
12         <li><a href="#Build an HTML form to create new"> Build an HTML form to create new Form-based topics</a></li>
13         <li><a href="#Changing a form"> Changing a form</a></li>
14         <li><a href="#Searching for Form Data"> Searching for Form Data</a></li>
15         <li><a href="#Extending the range of form data"> Extending the range of form data types</a></li>
16         <li><a href="#Gotcha!"> Gotcha!</a></li>
17         <li><a href="#Importing Category Table Data"> Importing Category Table Data</a></li>
18       </ul>
19     </li>
20   </ul>
21 </div>
22
23 # <a name="TWiki Forms"></a> TWiki Forms
24
25 _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._
26
27 ## <a name="Overview"></a> Overview
28
29 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 only form attached to it at a time.
30
31 Typical steps to build an application based on TWiki forms:
32
33 1. Define a form template
34 2. Enable the form for a web
35 3. Add the form to a [[template topic|Main/TWikiTemplates#Template_Topics]]
36 4. Build an HTML form to create new topics based on that template topic
37 5. Build a [[FormattedSearch]] to list topics that share the same form
38
39 ## <a name="Defining a Form Template"></a> Defining a Form Template
40
41 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 is one form field.
42
43 ### <a name="Form Template Elements"></a> Form Template Elements
44
45 - **form template** - a set of fields defining a form
46   - A web can use one or more form templates
47 - **form** - additional meta data (besides the freeform TEXTAREA) attached to a topic
48   - Within a form-enabled web, individual topics can have a form or no form
49 - **form field** - a named item in a form (also known as a _key_)
50 - **field type** - selects the field type: <table border="1" cellpadding="0" cellspacing="0">
51   <tr>
52     <th bgcolor="#99CCCC"><strong> Input type </strong></th>
53     <th bgcolor="#99CCCC"><strong> Type field </strong></th>
54     <th bgcolor="#99CCCC"><strong> Size field </strong></th>
55     <th bgcolor="#99CCCC"><strong> Value field </strong></th>
56   </tr>
57   <tr>
58     <td> One or more checkboxes </td>
59     <td><code>checkbox</code></td>
60     <td> number of items per line </td>
61     <td> comma list of item labels </td>
62   </tr>
63   <tr>
64     <td> One or more checkboxes, plus <strong>Set</strong> and <strong>Clear</strong> buttons </td>
65     <td><code>checkbox+buttons</code></td>
66     <td> (same) </td>
67     <td> (same) </td>
68   </tr>
69   <tr>
70     <td> One or more radio buttons (radio buttons are mutually exclusive; only one can be selected) </td>
71     <td><code>radio</code></td>
72     <td> (same) </td>
73     <td> (same) </td>
74   </tr>
75   <tr>
76     <td> Read-only label text </td>
77     <td><code>label</code></td>
78     <td><em>ignored</em></td>
79     <td> text </td>
80   </tr>
81   <tr>
82     <td> Drop-down menu or scrollable box </td>
83     <td><code>select</code></td>
84     <td><code>1</code> for drop down, <code>2</code> and up for scrollable box </td>
85     <td> comma-separated list of options </td>
86   </tr>
87   <tr>
88     <td> A one-line text field </td>
89     <td><code>text</code></td>
90     <td> text box width in number of characters </td>
91     <td> initial text, if a new topic is created with a form template </td>
92   </tr>
93   <tr>
94     <td> A text box </td>
95     <td><code>textarea</code></td>
96     <td> columns x rows, e.g. <code>80x6</code>; default size is 40x5 </td>
97     <td> initial text, if a new topic is created with a form template </td>
98   </tr>
99 </table>
100 - **field value** - one or more values from a fixed set (select, checkbox, radio type) or free-form (label, text, text area).
101
102 ### <a name="Defining a Form"></a> Defining a Form
103
104 1. Create a new topic with your form name: <code>**YourForm**</code>, <code>**ExpenseReportForm**</code>, <code>**InfoCategoryForm**</code>, <code>**RecordReviewForm**</code>, whatever you need.
105 2. Create a TWiki table, with each column head 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)_.
106 3. For each field, fill in a new line; for the type of field, select from the list.
107 4. Save the topic _(you can later choose to [[enable/disable|Main/WebHome#EnablingForms]] individual forms)_.
108
109 > **Example: WebForm**
110 >
111 > %BR%
112 >
113 > `| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* |`
114 >
115 > %BR%
116 >
117 > `| TopicClassification | select | 1 | NoDisclosure, PublicSupported, PublicFAQ | blah blah... |   |`
118 >
119 > %BR%
120 >
121 > `| OperatingSystem | checkbox | 3 | OsHPUX, OsLinux, OsSolaris, OsWin | blah blah... |   |`
122 >
123 > %BR%
124 >
125 > `| OsVersion | text | 16 | | blah blah... |   |`
126 >
127 > <table border="1" cellpadding="0" cellspacing="0">
128 >   <tr>
129 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
130 >     <th bgcolor="#99CCCC"><strong> Type </strong></th>
131 >     <th bgcolor="#99CCCC"><strong> Size </strong></th>
132 >     <th bgcolor="#99CCCC"><strong> Values </strong></th>
133 >     <th bgcolor="#99CCCC"><strong> Tooltip message </strong></th>
134 >     <th bgcolor="#99CCCC"><strong> Attributes </strong></th>
135 >   </tr>
136 >   <tr>
137 >     <td>[[Sandbox/TopicClassification]]</td>
138 >     <td> select </td>
139 >     <td align="center"> 1 </td>
140 >     <td>[[Sandbox/NoDisclosure]], [[Sandbox/PublicSupported]], [[Sandbox/PublicFAQ]]</td>
141 >     <td> blah blah... </td>
142 >     <td>   </td>
143 >   </tr>
144 >   <tr>
145 >     <td>[[Sandbox/OperatingSystem]]</td>
146 >     <td> checkbox </td>
147 >     <td align="center"> 3 </td>
148 >     <td>[[Sandbox/OsHPUX]], [[Sandbox/OsLinux]], [[Sandbox/OsSolaris]], [[Sandbox/OsWin]]</td>
149 >     <td> blah blah... </td>
150 >     <td>   </td>
151 >   </tr>
152 >   <tr>
153 >     <td>[[Sandbox/OsVersion]]</td>
154 >     <td> text </td>
155 >     <td align="center"> 16 </td>
156 >     <td>   </td>
157 >     <td> blah blah... </td>
158 >     <td>   </td>
159 >   </tr>
160 > </table>
161
162 You can also retrieve possible values for select, checkbox or radio types from other topics:
163
164 > **Example: WebForm**
165 >
166 > - In the WebForm topic, define the form: <br /><table border="1" cellpadding="0" cellspacing="0">
167 >   <tr>
168 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
169 >     <th bgcolor="#99CCCC"><strong> Type </strong></th>
170 >     <th bgcolor="#99CCCC"><strong> Size </strong></th>
171 >     <th bgcolor="#99CCCC"><strong> Values </strong></th>
172 >     <th bgcolor="#99CCCC"><strong> Tooltip message </strong></th>
173 >     <th bgcolor="#99CCCC"><strong> Attributes </strong></th>
174 >   </tr>
175 >   <tr>
176 >     <td> TopicClassification </td>
177 >     <td> select </td>
178 >     <td> 1 </td>
179 >     <td>   </td>
180 >     <td> blah blah... </td>
181 >     <td>   </td>
182 >   </tr>
183 >   <tr>
184 >     <td> OperatingSystem </td>
185 >     <td> checkbox </td>
186 >     <td> 3 </td>
187 >     <td>   </td>
188 >     <td> blah blah... </td>
189 >     <td>   </td>
190 >   </tr>
191 >   <tr>
192 >     <td> OsVersion </td>
193 >     <td> text </td>
194 >     <td> 16 </td>
195 >     <td>   </td>
196 >     <td> blah blah... </td>
197 >     <td>   </td>
198 >   </tr>
199 > </table>
200 > <br /> %X% Leave the <code>**Values**</code> field **blank**.
201 >
202 > - Then in the TopicClassification topic, define the possible values: <br /><table border="1" cellpadding="0" cellspacing="0">
203 >   <tr>
204 >     <th bgcolor="#99CCCC"><strong> Name </strong></th>
205 >     <th bgcolor="#99CCCC"><strong> Type </strong></th>
206 >     <th bgcolor="#99CCCC"><strong> Tooltip message </strong></th>
207 >   </tr>
208 >   <tr>
209 >     <td> NoDisclosure </td>
210 >     <td> option </td>
211 >     <td> blah blah... </td>
212 >   </tr>
213 >   <tr>
214 >     <td> Public Supported </td>
215 >     <td> option </td>
216 >     <td> blah blah... </td>
217 >   </tr>
218 >   <tr>
219 >     <td> Public FAQ </td>
220 >     <td> option </td>
221 >     <td> blah blah... </td>
222 >   </tr>
223 > </table>
224
225 Field values can also be obtained as the result of a [[FormattedSearch]]. For example,
226
227 > `%SEARCH{"Office$" scope="topic" web="%MAINWEB%" nototal="on" nosummary="on" nosearch="on" regex="on" format="$web.$topic" separator=", " }%`
228
229 when used in the value field of the form definition, will take the set of field values to be all topic names in the Main web which end in "Office".
230
231 **Notes:**
232
233 - 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.
234 - 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.
235 - If a `label` field has no name (blank first column in the form definition) it will **not** be shown when the form is **viewed**, only when it is **edited**.
236 - The topic definition is not read when a topic is viewed.
237 - Field names can 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.
238 - Field names have to be unique. If the same name is necessary (as when the field values for several fields are obtained from the same topic), an alternative name must be assigned using the <code>**\[[...]]**</code> notation.
239 - The topic defining field values can also be generated through a [[FormattedSearch]], which must yield a suitable table as the result.
240 - 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.
241 - The <code>**Tooltip message**</code> column is used as a tooltip for the field name (only if field name is a [[WikiName]]) - you only see the tooltip in edit view.
242 - The <code>**Attributes**</code> column is used to define special behavior for that form field (multiple attributes can be entered, with or without separators):
243   - An attribute `H` indicates that this field should not be shown in view mode. However, the field is available for editing and storing information.
244   - 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.
245
246 <a name="EnablingForms"></a>
247
248 ## <a name="Enabling Forms by Web"></a> Enabling Forms by Web
249
250 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.
251
252 > **Example:**
253 >
254 > - Set WEBFORMS = BugForm, FeatureForm, Books.BookLoanForm
255
256 - 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.
257
258 ## <a name="Add a form to a topic"></a> Add a form to a topic
259
260 - Edit a topic and follow the "Add form" button to add a Form to the topic. 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.
261
262 - 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:
263   - other than checkboxes: <code>**name**</code>, ex: <code>**?BugPriority=1**</code>
264   - checkbox: <code>**namevalue=1**</code>, ex: <code>**?ColorRed=1**</code>. <br /> Boxes with a tick must be specified.
265   - 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.
266         <form name="newtopic" action="%SCRIPTURLPATH{"edit"}%/%WEB%/">
267            <input type="hidden" name="formtemplate" value="MyForm" />
268            New topic name <input type="text" name="topic" size="40" />
269            <input type="submit" value="Create" />
270         </form>
271 - **_%T% Tip:_** For TWiki applications you can [[automatically generate unique topicnames|Main/TWikiTemplates#AutomaticallyGeneratedTopicname]].
272
273 - **_%X% Note:_** Initial values will not be submitted to the form of a new topic if you only use the formtemplate parameter.
274
275 ## <a name="Build an HTML form to create new"></a> Build an HTML form to create new Form-based topics
276
277 - 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.
278
279 ## <a name="Changing a form"></a> Changing a form
280
281 - 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.
282
283 - 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.
284
285 - If you add a new field to the form, then it will appear next time you edit a topic that uses the form.
286
287 - 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)
288
289 <a name="UsingFormData"></a>
290
291 ## <a name="Searching for Form Data"></a> Searching for Form Data
292
293 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]].
294
295 > **Example**
296 >
297 > %BR% TWiki users often want to have an overview of topics they contributed to. With the
298 >
299 > <code>**$formfield**</code>
300 >
301 > parameter it is easy to display the value of a classification field next to the topic link:
302 >
303 >     | *Topic* | *Classification* |
304 >     %SEARCH{"%MAINWEB%.UserName" scope="text" regex="off" nosearch="on" nototal="on" order="modified" reverse="on"
305 >     format="|<b>[[$web.$topic][$topic]]</b> |<nop>$formfield(TopicClassification) |" web="Sandbox"}%
306
307 ## <a name="Extending the range of form data"></a> Extending the range of form data types
308
309 Several Plugins allow you to extend the range of data types accepted by forms. For example, the TWiki:Plugins.DateFieldPlugin lets you add a 'date' type to the available data types. All data types are single-valued (can only have one value) with the following exceptions:
310
311 - any type name starting with `checkbox`
312 - any type name with `+multi` anywhere in the name
313
314 Types with names like this can both take multiple values.
315
316 ## <a name="Gotcha!"></a> Gotcha!
317
318 - 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`.
319
320 <a name="FormsVsCategoryTables"></a>
321
322 ## <a name="Importing Category Table Data"></a> Importing Category Table Data
323
324 Very, very old TWiki releases used a system called the "TWikiCategoryTable". Later releases support automatic import of this data.
325
326 On upgrading from the previous TWiki, a Form Template topic has to be built for each web that used a Category Table, recreating the fields and values from the old <code>**twikicatitems.tmpl**</code>. The replacement Form Template must be set as the first item in the [[WebPreferences]] variable `WEBFORMS`. If missing, pages will display, but attempting to edit results in an error message.
327
328 The new Form Template system should work with old Category Table data with no special conversion. Data is assigned to Meta variables the first time an imported topic is edited and saved in the new system.
329
330 %T% If things aren't working correctly, there may be useful entries in `data/warning.txt`.
331
332 **_Related Topics:_** [[UserDocumentationCategory]], [[TWikiTemplates]]