~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/data/System/DataForms.txt

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
%META:TOPICINFO{author="ProjectContributor" date="1231502400" format="1.1" version="1"}%
 
2
%STARTINCLUDE%
 
3
---+ Data Forms
 
4
Data forms allow you to add structured data to topics. The data stored in the form fields can be used to search and filter topics. The combination of structured data and search queries are the base for building database applications.
 
5
 
 
6
%TOC%
 
7
 
 
8
---++ Overview
 
9
 
 
10
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.
 
11
 
 
12
Typical steps to build an application based on Foswiki forms:
 
13
   1. Define a form definition
 
14
   1. Enable the form for a web
 
15
   1. Build an HTML form to create new topics based on that template topic
 
16
   1. Build a FormattedSearch to list topics that share the same form
 
17
 
 
18
For a step by step tutorial, see AnApplicationWithWikiForm.
 
19
 
 
20
#FormDefinition
 
21
---++ Defining a form
 
22
A form definition specifies the fields in a form. A form definition is simply a page containing a Foswiki table, where each row of the table specifies one form field.
 
23
   1. Create a new topic with your form name: ==<nop>YourForm==, ==<nop>ExpenseReportForm==, ==<nop>InfoCategoryForm==, ==<nop>RecordReviewForm==, whatever you need.
 
24
   1. Create a TML table, with each column representing one element of an entry field: ==Name==, ==Type==, ==Size==, ==Values==, ==Tooltip message==, and ==Attributes== _(see sample below)_.
 
25
   1. For each field, fill in a new line; for the type of field, select from the list.
 
26
   1. Save the topic _(you can later choose to [[#EnablingForms][enable/disable]] individual forms)_.
 
27
 
 
28
<blockquote>
 
29
*Example:* %BR%
 
30
=| <nop>*Name* | <nop>*Type* | <nop>*Size* | <nop>*Values* | <nop>*Tooltip message* | <nop>*Attributes* |= %BR%
 
31
=| !TopicClassification | select | 1 | !NoDisclosure, !PublicSupported, !PublicFAQ | blah blah... | &nbsp; |= %BR%
 
32
=| !OperatingSystem | checkbox | 3 | !OsHPUX, !OsLinux, !OsSolaris, !OsWin | blah blah... | &nbsp; |= %BR%
 
33
=| !OsVersion | text | 16 | | blah blah... | &nbsp; |=
 
34
 
 
35
| *Name* | *Type* | *Size* | *Values* | *Tooltip message* | *Attributes* |
 
36
| <a href="%SCRIPTURLPATH{"view"}%/Sandbox/TopicClassification"><nop>TopicClassification</a> | select |  1  | <a href="%SCRIPTURLPATH{"view"}%/Sandbox/NoDisclosure"><nop>NoDisclosure</a>, <a href="%SCRIPTURLPATH{"view"}%/Sandbox/PublicSupported"><nop>PublicSupported</a>, <a href="%SCRIPTURLPATH{"view"}%/Sandbox/PublicFAQ"><nop>PublicFAQ</a> | blah blah... | &nbsp; |
 
37
| <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OperatingSystem"><nop>OperatingSystem</a> | checkbox |  3  | <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsHPUX"><nop>OsHPUX</a>, <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsLinux"><nop>OsLinux</a>, <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsSolaris"><nop>OsSolaris</a>, <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsWin"><nop>OsWin</a> | blah blah... | &nbsp; |
 
38
| <a href="%SCRIPTURLPATH{"view"}%/Sandbox/OsVersion"><nop>OsVersion</a> | text |  16  | | blah blah... | &nbsp; |
 
39
</blockquote>
 
40
 
 
41
See [[#FormStructure][structure of a form]] for full details of what types are available and what all the columns mean.
 
42
 
 
43
You can also retrieve possible values for =select=, =checkbox= or =radio= types from other topics:
 
44
 
 
45
<blockquote>
 
46
*Example:*
 
47
 
 
48
   * In the <nop>WebForm topic, define the form:
 
49
     <br />
 
50
     | *Name* | *Type* | *Size* | *Values*  | *Tooltip message* | *Attributes* |
 
51
     | <nop>TopicClassification | select | 1 |    | blah blah... | &nbsp; |
 
52
     | <nop>OperatingSystem | checkbox | 3 |    | blah blah... | &nbsp; |
 
53
     | <nop>OsVersion | text | 16 |    | blah blah... | &nbsp; |
 
54
     <br />
 
55
     %X% Leave the ==Values== field *blank*.
 
56
 
 
57
   * Then in the <nop>TopicClassification topic, define the possible values:
 
58
     <br />
 
59
     =| <nop>*Name*            |= %BR%
 
60
     =| <nop>NoDisclosure      |= %BR%
 
61
     =| Public Supported  |= %BR%
 
62
     =| Public FAQ        |= %BR%
 
63
     | *Name* |
 
64
     | <nop>NoDisclosure |
 
65
     | Public Supported |
 
66
     | Public FAQ |
 
67
</blockquote>
 
68
 
 
69
Field values can also be set using the result of expanding other macros. For example,
 
70
 
 
71
<blockquote>
 
72
=%<nop>SEARCH{"Office$" scope="topic" web="%<nop>USERSWEB%" nonoise="on" type="regex" format="$web.$topic" separator=", " }%=
 
73
</blockquote>
 
74
 
 
75
When used in the value field of the form definition, this will find all topic names in the %USERSWEB% web which end in "Office" and use them as the legal field values.
 
76
 
 
77
#EnablingForms
 
78
---++ Enabling forms by Web
 
79
 
 
80
Forms have to be enabled for each individual web. The ==WEBFORMS== setting in WebPreferences is optional and defines a list of possible form definitions.
 
81
<blockquote>
 
82
*Example:*
 
83
   * <nop>Set WEBFORMS = <nop>BugForm, <nop>FeatureForm, Books.<nop>BookLoanForm
 
84
</blockquote>
 
85
   * With ==WEBFORMS== 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.
 
86
   * You have to list the available form topics explicitly. You cannot use a =SEARCH= to define =WEBFORMS=.
 
87
 
 
88
---++ Adding a form to a topic
 
89
 
 
90
   * Edit the topic and follow the "Add form" button to add a Form. This is typically done to a [[TemplateTopics][template topic]], 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.
 
91
 
 
92
   * 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:
 
93
      * other than checkboxes: ==name==, ex: ==?BugPriority=1==
 
94
      * checkbox: ==namevalue=1==, ex: ==?ColorRed=1==. <br /> 
 
95
        Boxes with a tick must be specified.
 
96
      * 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.
 
97
       <verbatim>
 
98
    <form name="newtopic" action="%SCRIPTURLPATH{"edit"}%/%WEB%/">
 
99
       <input type="hidden" name="formtemplate" value="MyForm" />
 
100
       New topic name <input type="text" name="topic" size="40" />
 
101
       <input type="submit" class="foswikiSubmit" value="Create" />
 
102
    </form>
 
103
       </verbatim>
 
104
   * *%X% Note:* You can create a topic in one step, without going through the edit screen. To do that, specify the =save= script instead of the =edit= script in the form action. When you specify the =save= script you must to use the "post" method. Example:
 
105
       <verbatim>
 
106
    <form name="newtopic" action="%SCRIPTURLPATH{"save"}%/%WEB%/" method="post">
 
107
       .....
 
108
    </form>
 
109
       </verbatim>
 
110
   * %H% The =edit= and =save= scripts understand many more parameters, see CommandAndCGIScripts#edit and CommandAndCGIScripts#save for details.
 
111
 
 
112
   * *%T% Tip:* For Wiki Applications you can [[TemplateTopics#AutomaticallyGeneratedTopicname][automatically generate unique topicnames]].
 
113
 
 
114
   * *%X% Note:* Initial values will *not* be set in the form of a new topic if you _only_ use the formtemplate parameter.
 
115
 
 
116
---++ Changing a form
 
117
   * 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. 
 
118
 
 
119
   * 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.
 
120
 
 
121
   * If you add a new field to the form, then it will appear next time you edit a topic that uses the form.
 
122
 
 
123
   * 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)
 
124
 
 
125
   * 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.
 
126
 
 
127
#FormStructure
 
128
---++ Structure of a form definition
 
129
 
 
130
A form definition specifies the fields in a form. A form definition is simply a TML table contained in a topic, where each row of the table specifies one form field.
 
131
 
 
132
Each *column* of the table is one element of an entry field: ==Name==, ==Type==, ==Size==, ==Values==, ==Tooltip message==, and ==Attributes==.
 
133
 
 
134
The =Name=, =Type= and =Size= columns are required. Other columns are optional. The form *must* have a header row (e.g. =| <nop>*Name* | <nop>*Type* | <nop>*Size* |=).
 
135
 
 
136
==Name== is the name of the form field.
 
137
 
 
138
The ==Type==, ==Size== and ==Value== fields describe the legal values for this field, and how to display them.
 
139
   * ==Type== =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.
 
140
      * ==Type== =checkbox+buttons= will add *Set* and *Clear* buttons to the basic =checkbox= type.
 
141
   * ==Type== =radio= is like =checkbox= except that radio buttons are mutually exclusive; only one can be selected.
 
142
   * ==Type== =label= specifies read-only label text. The =Value= field should contain the text of the label.
 
143
   * ==Type== =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.
 
144
      * There are two modifiers that can be applied to the =select= type:
 
145
         * =select+multi= turns multiselect on for the select, to allow Shift+Click and Ctrl+Click to select (or deselect) multiple items.
 
146
         * =select+values= allows the definition of values that are different to the displayed text. For example:
 
147
         <pre>| Field 9 | select+values | 5 | One, Two=2, Three=III, Four | Various values formats |</pre> shows <select size="2"><option>One</option><option>Two</option><option>Three</option><option>Four</option></select> but the values or options =Two= and =Three= are =2= and =III= respectively.<br />
 
148
      You can combine these modifiers e.g. =select+multi+values=
 
149
   * ==Type== =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 definition.
 
150
   * ==Type== =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
 
151
   * ==Type== =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
 
152
 
 
153
==Tooltip message== is a message that will be displayed when the cursor is hovered over the field in =edit= view.
 
154
 
 
155
==Attributes== specifies special attributes for the field. Multiple attributes can be entered, separated by spaces.
 
156
   * An attribute =H= indicates that this field should not be shown in view mode. However, the field is available for editing and storing information.
 
157
   * 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.
 
158
 
 
159
For example, a simple form just supporting entry of a name and a date would look as follows:
 
160
<verbatim>
 
161
| *Name* | *Type* | *Size* |
 
162
| Name   | text   | 80     |
 
163
| Date   | date   | 30     |
 
164
</verbatim>
 
165
*Field Name Notes:*
 
166
   * Field names have to be unique.
 
167
   * 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.
 
168
   * You can space out the title of the field, and it will still find the topic e.g. =Aeroplane Manufacturers= is equivalent to =AeroplaneManufacturers=.
 
169
   * If a =label= field has no name, it will *not* be shown when the form is *viewed*, only when it is *edited*.
 
170
   * 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  ==[<nop>[...]]== 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. 
 
171
   * Leading and trailing spaces are _not_ significant.
 
172
*Field Value Notes:*
 
173
   * 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.
 
174
   * Leading and trailing spaces are _not_ significant.
 
175
   * Field values can also be generated through a %SYSTEMWEB%.FormattedSearch, which must yield a suitable table as the result.
 
176
   * Macros in the initial values of a form definition get expanded when the form definition is loaded.
 
177
      * If you want to use a =|= character in the initial values field, you have to precede it with a backslash, thus: =\|=.
 
178
      * You can use =&lt;nop>= to prevent macros from being expanded.
 
179
      * The FormatTokens can be used to prevent expansion of other characters.
 
180
*General Notes:*
 
181
   * The topic definition is not read when a topic is viewed.
 
182
   * 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.
 
183
 
 
184
---+++ Values in other topics
 
185
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:
 
186
<verbatim>
 
187
| *Name*                 | *Type* | *Size* |
 
188
| AeroplaneManufacturers | select |        |
 
189
</verbatim>
 
190
the Foswiki will look for the topic !AeroplaneManufacturers to get the possible values for the =select=.
 
191
 
 
192
The !AeroplaneManufacturers topic must contain a table, where each row of the table describes a possible value. The table only requires one column, ==Name==. Other columns may be present, but are ignored.
 
193
 
 
194
For example:
 
195
<verbatim>
 
196
| *Name* |
 
197
| Routan |
 
198
| Focke-Wulf |
 
199
| De Havilland |
 
200
</verbatim>
 
201
 
 
202
*Notes:*
 
203
   * The ==Values== column *must be empty* in the referring form definition.
 
204
 
 
205
---++ Extending the range of form data types
 
206
You can extend the range of data types accepted by forms by using [[Plugins]]. All such extended data types are single-valued (can only have one value) with the following exceptions:
 
207
   * any type name starting with =checkbox=
 
208
   * any type name with =+multi= anywhere in the name
 
209
Types with names like this can both take multiple values.
 
210
 
 
211
---++ Hints and tips
 
212
---+++ Build an HTML form to create new form-based topics
 
213
   * New topics with a form are created by simple HTML forms asking for a topic name. For example, you can have a =<nop>SubmitExpenseReport= topic where you can create new expense reports, a =<nop>SubmitVacationRequest= topic, and so on. These can specify the required template topic with its associated form. [[TemplateTopics][Template topics]] has more.
 
214
A form definition specifies the fields in a form. A form definition is simply a page containing a Foswiki table, where each row of the table specifies one form field.
 
215
 
 
216
#UsingFormData
 
217
---+++ Searching in form data
 
218
The best way to search in form data is using the structured query language in the SEARCH macro. See QuerySearch for more information.
 
219
 
 
220
---+++ Gotcha!
 
221
   * 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=.
 
222
 
 
223
%STOPINCLUDE%
 
224
 
 
225
---
 
226
*Related Topics:* UserDocumentationCategory, TemplateTopics, AnApplicationWithWikiForm