A little extension of the PmWiki scripts/forms.php file allows an easy building and validation of forms embedded in a wiki page.
The usage is very close to the original forms.php, with the following extensions:
- the form input tag has an additional attribute named "
_action" which enables the new form validation functionality for a specific form (such a form is called "validated" in further reading of this document).
- a number of new global variables have been added:
This variable enables the validation functionality in general when set to non-zero.
If defined and a validated form is shown to the user, the specified function is called with the form name, the "
_action" value and an array of input values (passed as reference). The function can enter default values for each field if the values defined in the input tags shall be overriden.
If defined and a valid form was posted by the user, the specified function is called with the form name, the "
_action" value and an array of input values. The function can then perform the corresponding action (like sending an email, or storing the input values somewhere).
Is set to 1 after a valid form has been posted.
- a new action "validateinput" is available when form validation is enabled (
$EnableFormValidation). It is used when a validated form was posted.
- the validation of a form is based on preg regular expressions specifying valid input for each field in the fields "
_regex" attribute. When the field has another attribute named "
_message", then the text contained in the attribute will be appended to the
markup when a field is invalid.
The following markup:
(:input form _action=test:)
Name: (:input text text _regex='/.+$/' _message='Please enter a name':)
produces the following form:
Download and Installation
Download the new forms.php and put it in the scripts folder of your PmWiki.
The current version is adapted to and running on PmWiki Version 2.1.1 .