Manual:HTMLForm Tutorial 2

The rest of this page is about the use of HTMLForm through generic   entries (settings that are common to all fields types). We are therefore working only on HTMLTextField input.

tutorial3>Special:MyLanguage/HTMLForm/tutorial3|Skip the generic part and directly go to the field specification Stuff

Here we are in   And working on the   function...

Adding a Simple Text Field
Let's replace "Hello World" by a text field called   Now, the form looks like:



Adding Callback
Sadly, the previous code displays the form, but the form doesn't work. We need to write some logic to process the form input! Now, the form processes the submitted data:



Submission method
The form can be set to use either the POST or GET submission method. if you are trying to submit the data via a GET request, you will have to add the following:
 * POST (default): The form fields' contents are not shown in the page URL. This method should be used for forms that change the wiki content (e.g. edit pages or block users).
 * GET: The form fields' contents are shown in the page URL. The resulting URL can be bookmarked or linked to allow others to view it. However, submission of very long forms (multiple kilobytes of text) may fail. This method should be used for forms that do not change the wiki content (e.g. show a list of pages).

Note that GET forms using a   or   field have to have a "form identifier" set to work correctly, same as if you were using multiple forms on one page – see Using multiple forms on a single page below.

Adding Validation
Fields can be individually validated before submit callback.

First we tell HTMLForm by adding this line: in : Then we write the validation logic: Now, the validation logic checks submitted data before processing:



Required Field
You can specify that a field is required by simply adding to the formDescriptor. Any validation-callback will overwrite required. If you want to validate a required field, add the following logic to your validation callback function:

Adding Filtering
Filtering happens BEFORE validation to change input.

formDescriptor declaration: Filtering logic:

Adding i18n support
Replacing 'label'</> by <tvar|labelmessage>'label-message'</> in formDescriptor will automatically fetch the string through i18n routine: Don't forget to add the correct entry in the localization files: For example here, English and French... For the submit button, we need to do it "manually": Of course, as always, you'll need to add the <tvar|message>myform-submit</> entry in the localization files.

Adding sections
Now, we need to add some fields and organize them, let's switch to a bigger formDescriptor.

The section string displayed is automatically fetched from the localization files. Here we therefore need to add this in en.json :

Now, the form looks like:



Subsections
Sections can be easily nested with the incredible power of /.

The i18n ID will be like:

Now, the form looks like:



Adding Help-text
What about providing users with instructions to use your form easily? <tvar|help> </> or <tvar|helpmessage>help-message</> (an i18n message name) are here for you.

Now, the form looks like:



Adding html CLASS and ID
<tvar|cssclass> </> and <tvar|id> </> are here for you.

Changing Name of input
By default, input name is <tvar|default> </>, for the previous example, the input name is therefore <tvar|fieldname> </>. This can be changed with <tvar|name> </>: Just to give you an idea, here is the HTML output generated:

Disabling input
<tvar|disabled>disabled</> is there and the user cannot copy (CTRL-C) input. The item will not be valid for submission. As simple as that!

Turning input read-only
<tvar|readonly>readonly</> is there, and the user can copy (CTRL-C) input. Again, as simple as that!

Using multiple forms on a single page
If you use multiple forms on a single special page (e.g. one to show search results, and another to modify a result), you need to set a "form identifier" to allow HTMLForm to detect which of the forms was submitted. The identifier can be any string, and it must be different for every form.

For technical reasons, this is also necessary when using a <tvar|check> </> or <tvar|multiselect> </> field in a get>#Submission method</>|GET form; otherwise HTMLForm can't detect whether the form was submitted and therefore whether it should load the default values or not.

tutorial3>Special:MyLanguage/Manual:HTMLForm/tutorial3</>|NEXT PAGE OF THE TUTORIAL