Data FormsStructured data, how to set up and work with data forms.On this page:
OverviewNext 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:
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 definitionTopics 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 data form topicThe data definition is defined in a TML table and looks like this:| *Name* | *Type* | *Size* | *Values* | *Description* | *Attributes* | *Default* | | TopicTitle | text | 100 | | Title of the topic | H | | | Version | select | 1 | Version in SVN,Foswiki 1.1.4,Foswiki 1.1.3,Foswiki 1.1.2,Foswiki 1.1.1 | | | | | Status | select | 1 | Asked,More info required,Answered | Mandatory status | M | Asked | | Related Topics | textboxlist | | | | | |In the next section we'll go into the details of the table contents. 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 form tableA form is to a web as a table is to a database. -- Andrew Steele The data form table is a kind of spreadsheet:
Header rowData form tables often have an optional header row as a useful reminder of the column names:| *Name* | *Type* | *Size* | *Values* | *Description* | *Attributes* | *Default* | Form field rowsOne form field is defined by each row in the table.
Form field attributesNameThe name of the form field.
TypeThe 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.
Example of select+valuesA formfield definition like:| Field 9 | select+values | 1 | One, Two=2, Three=III, Four | Various values formats |displays as: The generated HTML code reveals that the form values differ from the option labels: <select name="Field9" size="1" class="foswikiSelect"> <option value="One" class="foswikiOption">One</option> <option value="2" class="foswikiOption">Two</option> <option value="III" class="foswikiOption">Three</option> <option value="Four" class="foswikiOption">Four</option> </select> Extending the range of form data typesSome plugins add data types. For instance, JQueryPlugin's sub-plugin JQueryFarbtastic adds the data typecolor :
| *Name* | *Type* | *Size* | | MyColor | color | 10 |Example (click in the field to view): Note to extension developers Such extended data types are single-valued (can only have one value) with the following exceptions:
SizeThe 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.ValuesFor checkboxes, radio buttons and dropdown lists: predefined input to select from. More advanced: this can be a dynamically generated list of values.
DefaultIf this column exists in the form definition, then it will be used to determine the default value for a field. It over-rides all defaulting from theValues column.
How to retrieve values from other topicsIf you have rows defined like this:| *Name* | *Type* | *Size* | | AeroplaneManufacturers | select | |… Foswiki will look for the topic AeroplaneManufacturers to get the possible values for the select field.
The Values column must be empty.
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.
For example:
| *Name* | | Routan | | Focke-Wulf | | De Havilland | How to set field values using a macroA powerful way to populate selectable field values is by using SEARCH to generate a comma-separated list of values. For example, to create a list of documentation topics whose name contain "Wiki", you write:| *Name* | *Type* | *Size* | *Values* | | Subject | select+multi | 10 | ,%SEARCH{ \ "Wiki" \ scope="topic" \ web="%SYSTEMWEB%" \ nonoise="on" \ type="regex" \ format="$topic" \ separator=", " \ }% |Note the use of the backslash to be able to write the macro in a more readable way using multiple lines. The comma just before the SEARCH means "empty value" to make it possible to select none.
Sometimes you have a topic with a bullet list that can be used as selectable values. The rather advanced SEARCH expression would be:
| Subject | select+multi | 10 | ,%SEARCH{ \ " *\s*.*?" \ topic="QuestionSubjectCategory" \ type="regex" \ multiple="on" \ casesensitive="on" \ nonoise="on" \ separator="," \ format="$pattern(.* \*\s*([^\n]*).*)" \ }% | | |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 .
DescriptionDescription of the field. Sometimes used in help information.AttributesWhether the field is mandatory or hidden when viewed.
| TopicTitle | text | 100 | | | H M | Enabling formsBefore 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 theWEBFORMS setting. The setting accepts a comma-separated list of form topics:
* Set WEBFORMS = BugForm, FeatureForm, Books.BookLoanForm, %USERSWEB%.UserFormAs you can see, form topics located in other webs can be added by using their web prefix. You have to list the available form topics explicitly. You cannot use a SEARCH to define WEBFORMS .
Adding a form to a topicWithWEBFORMS enabled, a form can be added or changed on the edit screen.
Manual operation
Automatically adding a form to a new topicUsing a template topicIf you don't want users to select a form themselves, this step can be automated by using a template topic that has the form connected. For example, Question topics on foswiki.org are created using QuestionTemplate:%META:FORM{name="QuestionForm"}% %META:FIELD{name="TopicTitle" attributes="H" title="TopicTitle" value=""}% %META:FIELD{name="Subject" attributes="" title="Subject" value=""}% %META:FIELD{name="Status" attributes="M" title="Status" value="Asked"}%In the template initial field values can be set, like the "Status" field in this example. Using the edit templateIf you will use only one form in a web, you can also modify the web'sWebTopicEditTemplate topic to contain the form reference.
Using url parametersYou can also pass theformtemplate parameter to the edit (not save) URL.
Initial values can then be provided in the URLs or as form values.
For example:
%SCRIPTURL{edit}%/%WEB%/KnowledgeTopicAUTOINC00001?formtemplate=MyForm;Subject=FAQWhatIsWikiWiki;Category=One;Category=Two;action=form
Using a web form to create a topic and pass dataIf you want to create+save a topic instead of bringing up the edit screen, you must use a web form. The same parameters as above can be set in HTML (hidden) form fields:<form name="newtopic" action="%SCRIPTURLPATH{"save"}%/Sandbox/" method="post"> <input type="hidden" name="formtemplate" value="SimpleForm" /> <input type="hidden" name="topic" value="KnowledgeTopicAUTOINC00001" /> <input type="hidden" name="Subject" value="FAQWhatIsWikiWiki" /> <input type="hidden" name="Category" value="One" /> <input type="hidden" name="Category" value="Two" /> <input type="submit" class="foswikiSubmit" value="Create topic" /> </form>Creates: For an overview of web form parameters, see CGI and Command Line Scripts. Changing a formYou 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.
Searching in form dataThe best way to search in form data is using the structured query language in the SEARCH macro. As an example, the search used on Foswiki Support questions is:%SEARCH{ "QuestionForm.Status='Asked'" type="query" excludetopic="QuestionTemplate" web="%WEB%" format=" * [[$web.$topic][$formfield(TopicTitle)]] $formfield(Subject)" order="formfield(Extension)" nonoise="on" }%For a step by step tutorial, see the FAQ How can I create a simple data form based application?. See SEARCH for an overview of SEARCH parameters, and Query Search for the query language specifics.
Related Topics: DeveloperDocumentationCategory, TemplateTopics, Creating applications with DataForms, VarFORMFIELD, SkinTemplates |