Extension:Page Forms/Defining forms

Semantic Forms provides an entire syntax for defining forms, that makes use of tags contained within triple curly brackets. Pages that contain such syntax should always go in the "Form:" namespace (or, for non-English-language wikis, its equivalent in another language). Such pages are not called forms, but rather "form-definition pages", to distinguish them from the actual corresponding forms that users see.

It is recommended that, when starting out with Semantic Forms, you not try creating form-definition pages from scratch, but rather use one of Semantic Forms' helper pages to create them: Special:CreateForm or Special:CreateClass - or copy from some other example. Later on, however, to make changes to the form, you will unfortunately have to edit the form definition directly, and that is where the following documentation will hopefully be helpful.

Form markup language
Forms are defined using a set of tags that specify templates and fields within those templates. Wiki-text, and some HTML, can be freely embedded anywhere outside of the tags. The allowed tags are:

'info' tag
The  tag holds special information about the form. This tag is optional, but should be placed at the top of the form if it is present. Allowed parameters of this tag are:
 * - Indicates that this form is a partial form; see "Partial forms", below.
 * title - Sets the title for the 'FormEdit' page, if the form is being used to create a new page
 * title - Sets the title for the 'FormEdit' page, if the form is being used to edit an existing page
 * title - Sets the title for the 'RunQuery' page
 * formula - Sets a formula for automatic setting of the name of the page being added; see "Linking to forms#The one-step process".
 * - Places the form at the top, instead of the bottom, for the 'RunQuery' page
 * - Indicates that the free text in the page generated by the form should be placed within an " " tag, so that pages created by the form can be transcluded in other pages without affecting those other pages' semantic properties

'for template' tag
The  tag specifies a template name, and declares that all of the following fields (until end template is reached) will be those of this template. The name immediately following the for template declaration is the name of the template. Allowed parameters of this tag are:
 * label text - Specifies a label to be placed in a square around the entire set of this template's fields in the form. This is especially useful if the template can have multiple instances.
 * - Specifies that the user can change the number of instances of this template in the form, allowing multiple (or zero) occurrences; see "Multiple-instance templates", below.
 * - For multiple-instance templates, sets the text in the "Add another" button.
 * - Specifies that only fields in the form that correspond to fields used by the template should get turned into form elements.
 * template name[field name] - Specifies that the call(s) to this template within the generated pages will get embedded within a field of another template (i.e., " "). It is used in conjunction with the "holds template" parameter for the 'field' tag (see below). Note that the template to be embedded must appear after the template in which it will be embedded.

'end template' tag
The  tag ends the range of a template. There are no parameters for this tag.

'field' tag
The  tag specifies a field to be placed in a form, corresponding to a template field. The name immediately following the field declaration is the name of the template field. There are a large variety of possible parameters to this tag, some of which can only be used for certain input types.

Parameters that can be applied to any field are:
 * input type - Specifies the type of input this field will have in the form. If a field corresponds to a semantic property, the form will usually have the correct input type by default; otherwise the default is text. If the corresponding semantic property cannot be automatically determined (e.g. if it's in a subtemplate), you can use the parameter "property" to manually specify it (see below). The allowed set of input types is different for every semantic property type; see below for the full list of options.
 * - Specifies that this field will be hidden in the form: used to preserve values in edited pages.
 * - Specifies that this field must be filled in by the user. Note:  should not be used in conjunction with  . Use of both in any field will cause the mandatory check on any field to fail when the form is saved.
 * - Specifies that this field will be editable only by admins/sysops and disabled for all other users. This parameter can also be called as " =group name", which restricts editing of the field to the specified user group.
 * default value - Specifies a default value for this field. For date-related fields, default=now will set the value to the current date and possibly time. For text fields, default=current user will set the value to the username of the user adding this page. For the checkbox input type, default=yes will check the checkbox by default. (The "checkboxes" input type, on the other hand, like the "listbox" type, requires specifying the actual values, like default=Value A, Value C.) You can also include templates, parser functions, and magic words within the "default=" value.
 * class name - Specifies a CSS class that the input for this field should have.
 * property name - Specifies that the field corresponds to a certain semantic property, and should thus get the corresponding input type, autocompletion, etc.
 * - Specifies that this field contains a list of values.
 * delimiter - Specifies the delimiter character or string this field should use, if it represents a list of values; the default is ",".
 * - Specifies that this field is meant to hold a multiple-instance template, and does not have its own form input. It is used in conjunction with the "embed in field" parameter for the 'for template' tag (see above).

Parameters that can only be applied to fields that hold a list of uploaded files are:
 * - Specifies that a link should be placed next to this field, that opens a popup window to let the user upload a file; see "Uploading files", below.
 * - Specifies that a thumbnail of the uploaded image should be placed under the field in the form.
 * filename - Specifies the default filename for files uploaded with this field.

Parameters that can only be applied to fields of type 'textarea' are:
 * num rows - Specifies the number of rows.
 * num cols - Specifies the number of columns.
 * - Sets the textarea to "auto-grow" its height to match that of its contents, so that a scrollbar won't be needed.
 * editor type - Adds a Javascript-based editor to the textarea to make editing its contents more user-friendly. Currently only one value is allowed: "wikieditor", for the WikiEditor extension (which must be installed for this to work). If you want to add any additional custom toolbars to WikiEditor, you will need to add them in the Javascript to, just as they exist for.

Parameters that can only be applied to inputs that have a set of pre-defined values, either that the user must select from (like the "dropdown" input) or that are suggested (the "autocomplete" and "combobox" inputs):
 * possible values - Specifies a set of either possible values or autocompletion values (depending on the input type) that this field can have, overriding whatever set of values may have been set from the semantic property. This set of values is separated by commas by default, but the delimiter can be modified using the  parameter.
 * property name - Similar to, but gets its values from the the set of all values that a certain property points to.
 * category name - Similar to, but gets its values from the names of all pages belonging to a specific category.
 * concept name - Similar to, but gets its values from the names of all pages belonging to a specific concept.
 * namespace name - Similar to, but gets its values from the names of all pages belonging to a specific namespace. (To get values from the main namespace, use "Main" for the namespace name, or just leave it blank.)
 * URL identifier - Used only for autocompletion. Specifies that autocompletion should be based on the values retrieved from an outside URL; see Autocompleting on outside values for how to do this.
 * - Specifies that autocompletion values should be retrieved dynamically from the server, instead of directly from a list of values hidden in the page's HTML.
 * - Set a combo box to only allow the autocomplete values, and not any arbitrary values, thus having it function even more like a dropdown.
 * value 1 element ID 1;value 2 element ID 2;value 3 element ID 3;etc. - Can be used for inputs of type 'checkbox', 'checkboxes', 'radiobutton', 'dropdown' and 'listbox' to specify that one or more tags with a certain ID should only be displayed to the user if a certain value (or values) is selected within that input. See here for an example of this feature in use (note what happens when you select different values for the "Publication type" dropdown), and see here for how it was implemented in the form. For inputs of type 'checkbox', simply " element ID" should be used. Note that the element IDs cannot contain spaces and/or multibyte characters.
 * template name[field name] - Can be used for inputs of type 'combobox', 'text with autocomplete' and 'textarea with autocomplete'. Specifies that the current set of allowed values for this field depends on the values a user selected, or will select, for another field in the form.

Other parameters that can be added for certain input types:
 * size - Used for text entries, combobox and listbox. Specifies the width of a text entry or combobox, or the height of a listbox.
 * placeholder text - Used for text and textarea fields. Specifies help text that is displayed in the input before the user clicks on it.
 * maximum length - Used for text and textarea fields. Specifies the maximum allowed length of the input.
 * - Used for inputs of type 'datetime'; specifies that a time zone field should be included.

'standard input' tag
The  tag is used for nine different inputs that usually appear at the bottom of every form. The text immediately after "standard input|" is the name of each input. The most notable of these inputs is 'free text', which is a textarea that holds all the non-template text in a page. The other seven are form elements such as the "Save" and "Preview" buttons; see Defining the bottom of the form for the full list.

The 'free text' input has more elaborate handling the other standard inputs; see The free text input for its allowed parameters.

For the other standard input types, the allowed parameters are:
 * label name - Specifies the text associated with this input on the form.
 * label name - Specifies the CSS class for this input.
 * label name - Specifies the CSS style for this input.

In addition, the 'watch' input type can take in the parameter 'checked', which checks the "Watch this page" checkbox by default.

Allowed input types for data types
Each defined Semantic MediaWiki data type has a default input type, and, when applicable, a default input size as well. Additionally, some data types have special handling if the field holds a delimited list of values, instead of just a single value.

Here are the defaults and the other allowed input types for each data type, for single values:

And here are the default and other allowed input types for delimited lists of a certain data type, enabled by the use of the "#arraymap" function:

In addition, several other extensions define additional form input types: most notably, Semantic Maps defines map inputs for properties of type Geographic coordinate, while Semantic Forms Inputs defines five additional input types: 'datepicker', 'timepicker', 'datetimepicker', 'menuselect' and 'regexp'.

Autocompletion
One of the big strengths of Semantic Forms is that it supports autocompletion - you can enable a field to show a dropdown list of possible completions when the user starts typing. Semantic Forms' autocompletion is enabled by the jQuery UI Javascript library's Autocomplete plugin.

If a field represents a semantic property of type "Page", autocompletion will be enabled by default - the field will autocomplete on the names of all pages that are already pointed to by that property. For fields representing a semantic property of type "String", there is no default autocompletion, but you can achieve this same effect simply by adding "input type=text with autocomplete" or "input type=textarea with autocomplete" to the field's definition. You can also autocomplete on other sets of values:
 * to autocomplete on all the values of a specific property, add "values from property=property-name" to the field definition
 * to autocomplete on the names of all pages in a category, add "values from category=category-name"
 * to autocomplete on the names of all pages in a namespace (like "File"), add "values from namespace=namespace-name" (if you want to autocomplete on the main namespace, use "values from namespace=Main", or "values from namespace=").
 * finally, you can autocomplete based on the values in an external URL; which lets you get autocompletion values from essentially any outside system. See Autocompleting on outside values for how to do this.

If a field is specified to hold multiple values (see below), autocompletion will, by default, support multiple values: after a value is entered, and a delimiter placed, a new autocompletion will start for the next value. You can manually specify that a field should have multiple-value autocompletion, by adding the "list" parameter to the field's definition. You can also specify the delimiter for this list of values, using the "delimiter=..." parameter (the default is a comma).

The set of a field's possible values for autocompletion is, by default, contained right within the form's HTML page, in a Javascript declaration. For performance reasons, there is a limit to how many values can be placed in the page; this number is defined by the variable $sfgMaxAutocompleteValues, which by default is set to 1000. If you have more than this number of possible values for a field, you may want to use remote autocompletion instead, where autocompletion happens through an Ajax call to the server, based on what the user has typed. This type of autocompletion is slower, but always returns a comprehensive set of results. You can enable this by adding the "remote autocompletion" parameter to the field's definition.

By default, Semantic Forms autocompletion matches on the beginning of every word in the set of possible values. However, you can change autocompletion to instead match on every character, by adding the following line to LocalSettings.php: This feature is especially important for wikis that have values with non-ASCII characters, such as wikis in languages with non-Roman alphabets; since the default, word-based autocompletion doesn't yet work with non-ASCII characters.

Finally, you can disable autocompletion, if it's enabled by default for a field, by setting the input type to be simply "text" or "textarea".

Combo box input
For any input that holds a single value, you can set the input to be a combo box by setting "input type=combobox" in the field definition. This input functions like a regular autocomplete field, but has an additional down-arrow icon, like that of a dropdown, to let the user see all the available values at once.

You can also set the combo box to only allow the autocomplete values, and not any arbitrary values, thus having it function even more like a dropdown, by adding the additional parameter "existing values only" to the field tag.

Multiple values for the same field
Semantic Forms supports having multiple values within a given field, and some form input types - like "checkboxes" and "listbox" - are specifically geared for fields that contain multiple values. Text and textarea fields can also support autocompletion for multiple values. If a form field is meant to hold multiple values, the corresponding template field should most likely contain a call to either #arraymap or #arraymaptemplate - see "Semantic Forms and templates#Multiple values for the same field". Regardless of what is contained in the template, though, the fact that a field is meant to hold multiple values can be hard-coded in the form definition by adding the parameter "list" to the tag. The parameter "delimiter=" can also be helpful, if the delimiter between values is meant to be something other than the default (a comma).

Multiple-instance templates
If you add the 'multiple' parameter to a template, it will allow for multiple (or no) instances of this template in the form, and therefore in the generated page. The sample form below, for the 'Item' form, contains two such templates. If you look at the form that this definition generates, you can see that there are two buttons labeled "Add another". Clicking on either of these will create a new instance of that template and its field(s). Instances can be rearranged, by clicking on the icon on the right-hand side and dragging the instance up or down within the set.

You can rename the "Add another" button to any other text, using the "add button text=" parameter. For instance, to change the button to read "Add another occupation" for a template called "Occupation", you could have " ".

If you want to semantically store all the data contained in these multiple-instance templates, it is recommended to use the Semantic Internal Objects extension. (You can also use Semantic MediaWiki subobjects, although they're not as convenient in this case.)

There is a small difference between regular and embedded multiple-instance templates. In the former case, every new instance of a template starts on a new line after the closing }}. If the template is embedded in another template (see "embed in field" and "holds template"), every new instance directly follows the closing }}.

Uploading files
If a field in the form is meant to hold the name of an uploaded file (say, an image), you can allow users to upload this file directly through the form. This is done simply by adding the parameter "uploadable" to that field's declaration in the form definition. This will add a link reading "Upload file" next to this field in the form; if the user clicks on this link, it will pop up a "lightbox"-style window (using the FancyBox Javascript library) that lets the user upload a file. Once the user has done so, the window will close, and the field will contain the name of the uploaded file. If the field is configured to contain a list of values, the new file name will be appended to whatever was there before; otherwise, the file name will overwrite whatever the field contained before.

For uploadable fields, you can also set the default filename of the uploaded files, by setting the "default filename=" parameter in the field definition. The value of this parameter can include parser functions, magic words and the like. It can also include the variable " ", which gets substituted with the name of the page being added or edited. So adding to the field definition the parameter "default filename=Image for ", for instance, for a page called "Abc", would set the default name of any uploaded file to "Image for Abc". Note that if you simply want to specify a default file to use such as "Imageneeded.png" the regular "default=Imageneeded.png" parameter will do.

You can see a demonstration of file uploading here.

Note that the upload window will not work if  is set to   in your LocalSettings.php.

Using a combination of the "uploadable" feature and the one-step process, you can create a form for users to upload a file and add semantic data to its wiki page at the same time - see "Using Semantic Forms for file pages and uploads".

Category inputs
Two input types allow for a category-tree-style input: 'category' and 'categories'. 'category' is used for a single value in a field, and uses radiobuttons; while 'categories' is used for a multiple-value field, and uses checkboxes. Both input types use the code of the MediaWiki CategoryTree extension, so this extension must be installed for these inputs to work.

You also need to specify an additional parameter:
 * - sets the name of the category at the top of the "tree".

You can also optionally set the parameters:
 * - sets the height in px (pixel) of the box in which the category tree appears.
 * - sets the width in px (pixel) of the box in which the category tree appears.
 * - sets the delimiter when using 'categories'. Default is ','.
 * - hides the name of the top category.
 * - sets the number of levels of categories that are shown at the beginning. Default is 10.

It should be noted that these two input types print only the names of the categories selected, without the "Category:" namespace before it; so if you want that to show up in the page as well, the template will have to add it.

If the field specifies multiple categories, and the template uses #arraymap to do it, the call to #arraymap should look something like:

...in other words, you need to specify the final "delimiter" parameter for #arraymap, and make it a space, blank or something similar, to avoid printing commas between the category tags.

You can see a sample form that uses the 'category' input type here.

The free text input
The "free text" input sets a textarea that holds all the non-template text in a page. The following parameters for "textarea" fields can also be used for free text inputs:
 * rows</tt>
 * cols</tt>
 * hidden</tt>
 * restricted</tt>
 * default</tt>
 * class</tt>
 * autogrow</tt>
 * editor</tt>

In addition, there is one parameter available only for the "free text" input - "preload":
 * page title - Specifies a page whose contents should be preloaded into this field.

If it is left out of the form definition, any free text in a page will be preserved in the form as a hidden field, not viewable or modifiable by the user.

It is possible to have a WYSIWYG editor for the free text input: if your wiki has the MediaWiki FCKeditor extension installed, and it shows up correctly in regular 'edit' pages, then it should show up automatically as well for the free text input (currently, this is the only form input for which the FCKeditor can be made to work).

If you are using the FCKeditor, you can remove the links for users to disable it in the form, if you think those links will be distracting; you can do that by adding the following to LocalSettings.php:

Defining the bottom of the form
The user inputs at the bottom of the form can be customized using the "standard input" tag. The layout, inclusion and text of each input can be modified. Each user input is defined as a "standard input" tag with its own value; the allowed values are:
 * "save" (for the "Save page" button)
 * "preview" (for the "Show preview" button)
 * "save and continue" (for the "Save and continue" button - this lets users save the page without leaving the form)
 * "changes" (for the "Show changes" button)
 * "summary" (for the "Summary" text field)
 * "minor edit" (for the "This is a minor edit" checkbox)
 * "watch" (for the "Watch this page" checkbox)
 * "cancel" (for the "Cancel" link)
 * "run query" (for the "Run query" button in query forms)

So, for example, the button for "Save page" could be specified with " ", which would place the button where it was defined, with the text on the button reading "Save this page". If no  tags are included in the form definition, all six will appear at the bottom of the form, just as they do for regular "Edit" pages. However, if even one such tag is included, then only those inputs which have been included will be displayed, in the order, and with the wiki-text, that they appear in in the form definition.

Sample form
Here is the source code for the 'Item' form definition page at Discourse DB:

<div id="wikiPreview" style="display: none; padding-bottom: 25px; margin-bottom: 25px; border-bottom: 1px solid #AAAAAA;">

Topic:

Position:

Stance:

Item name:

Free text:

Note the presence of both wiki-text and some limited HTML (the 'div' tag) within the code. This markup is based around the templates Item, Opinion and Reference. You can see the working form at this add data page ; the form itself is created on-the-fly from the form definition file. In the 'Items' category page, if you click on any of the pages, you can see the 'edit with form' tab on the top right-hand side. If you click on that tab, you can see this same form, this time populated with the data contained in that page.

The 'wikiPreview' div tag, by the way, is included so that, when the user hits the "Preview" button, they'll be shown the preview directly within the form page, instead of being taken to another page. It is not necessary, and can be safely removed from any form definition.

The #forminput function
Form-definition pages generated by Semantic Forms (i.e., by Special:CreateForm or Special:CreateClass) always include a call to the #forminput function, which provides an input for users to reach that form. #forminput is not actually a part of the form-definition syntax, though. For information on how to modify the #forminput call, see Linking to forms#The two-step process.

Turning forms into tabbed sections
If you think a form is too long for users to fill out comfortably, you can turn it into sections, with a tab interface to navigate between them, using the Header Tabs extension. If you install this extension, it is very easy to add tabs to a form: just add a top-level section wherever you want a tab to appear, of the form "=Tab 1="; and then add the tag  near the bottom of the form, right above the "standard input" declarations. You can place tab declarations between templates, or within templates, to split up the fields of a template into different tabs. You can see a simple example of a form in tabbed sections here; the form definition used to create it is here.

Such tabs can also be used, in a similar way, to display data pages themselves.

Partial forms
You can specify a form to be a partial form by adding the property "partial form" to its tag. This will make the form cover only the single or multiple templates that it contains, and not modify any part of the page before or after these templates. A partial form is not recommended as a default form for a category or namespace, but rather as a tool to modify parts of pages after they have already been created. You can see an example of a partial form here, and you can see it in action here, by clicking on the link reading "Add or change this opinion item's references".

Reusing form elements
If you have a set of forms that all contain the same text or sections, you can avoid duplication by creating templates that are used within those forms. Simply place all the text that you want repeated within a template, and call that template within each form. If that template contains form elements (like field definitions), the characters within the form elements simply need to be HTML-escaped - replace '{' with '&amp;#123;', '|' with '&amp;#124;' and '}' with '&amp;#125;'. Or just use &lt;nowiki> around them.

Caching form definitions
When a form is displayed, the form definition is parsed twice - first to turn the wiki-text into HTML, and then to handle the SF-specific tags like "". If a form on your wiki takes a long time to display, it might be due to the first parse, which has been known to take seconds to run. You can have form definitions be cached, so the (mostly) HTML version can be retrieved directly, eliminating the first parse. To do this, add the following line to LocalSettings.php:

$sfgFormCacheType is set to null by default. If left at null, the parser cache will be used for caching. If set to one of the possible cache types (e.g. CACHE_ANYTHING), the selected cache will be used. This way it is possible to switch off the parser cache (e.g. to force re-evaluation of queries on every page load) and still cache the (rarely changed) form definitions.

Note: if any of your form definitions contain any elements that do dynamic display, such as or , these will not get displayed correctly if forms are cached, so you shouldn't use caching.

Tooltips
You can add tooltips to your form by using the  parser function defined by Semantic MediaWiki, e.g.,. (This parser function is not to be confused with Semantic Form's " " tag.)