MediaWiki
REL1_20
|
Object handling generic submission, CSRF protection, layout and other logic for UI forms. More...
Public Member Functions | |
__construct ($descriptor, $context=null, $messagePrefix= '') | |
Build a new HTMLForm from an array of field attributes. | |
addButton ($name, $value, $id=null, $attribs=null) | |
Add a button to the form. | |
addFooterText ($msg, $section=null) | |
Add footer text, inside the form. | |
addHeaderText ($msg, $section=null) | |
Add header text, inside the form. | |
addHiddenField ($name, $value, $attribs=array()) | |
Add a hidden field to the output. | |
addPostText ($msg) | |
Add text to the end of the display. | |
addPreText ($msg) | |
Add introductory text. | |
displayForm ($submitResult) | |
Display the form (sending to $wgOut), with an appropriate error message or stack of messages, and any validation errors, etc. | |
displaySection ($fields, $sectionName= '', $fieldsetIDPrefix= '') | |
filterDataForSubmit ($data) | |
Overload this if you want to apply special filtration routines to the form as a whole, after it's submitted but before it's processed. | |
getBody () | |
Get the whole body of the form. | |
getButtons () | |
Get the submit and (potentially) reset buttons. | |
getDisplayFormat () | |
Getter for displayFormat. | |
getErrors ($errors) | |
Format and display an error message stack. | |
getHiddenFields () | |
Get the hidden fields that should go inside the form. | |
getHTML ($submitResult) | |
Returns the raw HTML generated by the form. | |
getLegend ($key) | |
Get a string to go in the "<legend>" of a section fieldset. | |
getMethod () | |
getSubmitText () | |
Get the text for the submit button, either customised or a default. | |
getTitle () | |
Get the title. | |
loadData () | |
Construct the form fields from the Descriptor array. | |
prepareForm () | |
Prepare form for submission. | |
setAction ($action) | |
Set the value for the action attribute of the form. | |
setDisplayFormat ($format) | |
Set format in which to display the form. | |
setFooterText ($msg, $section=null) | |
Set footer text, inside the form. | |
setHeaderText ($msg, $section=null) | |
Set header text, inside the form. | |
setId ($id) | |
setIntro ($msg) | |
Set the introductory message, overwriting any existing message. | |
setMessagePrefix ($p) | |
Set the prefix for various default messages. | |
setMethod ($method= 'post') | |
Set the method used to submit the form. | |
setPostText ($msg) | |
Set text at the end of the display. | |
setPreText ($msg) | |
Set the introductory message, overwriting any existing message. | |
setSubmitCallback ($cb) | |
Set a callback to a function to do something with the form once it's been successfully validated. | |
setSubmitID ($t) | |
Set the id for the submit button. | |
setSubmitName ($name) | |
setSubmitText ($t) | |
Set the text for the submit button. | |
setSubmitTextMsg ($msg) | |
Set the text for the submit button to a message. | |
setSubmitTooltip ($name) | |
setTitle ($t) | |
Set the title for form submission. | |
setValidationErrorMessage ($msg) | |
Set a message to display on a validation error. | |
setWrapperLegend ($legend) | |
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element. | |
setWrapperLegendMsg ($msg) | |
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element. | |
show () | |
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or without funky validation errors. | |
suppressReset ($suppressReset=true) | |
Stop a reset button being shown for this form. | |
tryAuthorizedSubmit () | |
Try submitting, with edit token check first. | |
trySubmit () | |
Validate all the fields, and call the submision callback function if everything is kosher. | |
wrapForm ($html) | |
Wrap the form innards in an actual "<form>" element. | |
Static Public Member Functions | |
static | addJS () |
Add the HTMLForm-specific JavaScript, if it hasn't been done already. | |
static | formatErrors ($errors) |
Format a stack of error messages into a single HTML string. | |
static | loadInputFromParameters ($fieldname, $descriptor) |
Initialise a new Object for the field. | |
Public Attributes | |
$mFieldData | |
Static Public Attributes | |
static | $typeMappings |
Protected Attributes | |
Array | $availableDisplayFormats |
Available formats in which to display the form. | |
String | $displayFormat = 'table' |
Format in which to display form. | |
bool string | $mAction = false |
Form action URL. | |
$mButtons = array() | |
$mFieldTree | |
HTMLFormField[] | $mFlatFields |
* | |
$mFooter = '' | |
$mHeader = '' | |
$mHiddenFields = array() | |
$mId | |
$mMessagePrefix | |
$mMethod = 'post' | |
$mPost = '' | |
$mPre = '' | |
$mSectionFooters = array() | |
$mSectionHeaders = array() | |
$mShowReset = false | |
$mSubmitCallback | |
$mSubmitID | |
$mSubmitName | |
$mSubmitText | |
$mSubmitTooltip | |
$mSubSectionBeforeFields = true | |
If true, sections that contain both fields and subsections will render their subsections before their fields. | |
$mTitle | |
$mUseMultipart = false | |
$mValidationErrorMessage | |
$mWrapperLegend = false |
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
in a reusable manner.
In order to generate the form, the HTMLForm object takes an array structure detailing the form fields available. Each element of the array is a basic property-list, including the type of field, the label it is to be given in the form, callbacks for validation and 'filtering', and other pertinent information.
Field types are implemented as subclasses of the generic HTMLFormField object, and typically implement at least getInputHTML, which generates the HTML for the input field to be placed in the table.
You can find extensive documentation on the www.mediawiki.org wiki:
The constructor input is an associative array of $fieldname => $info, where $info is an Associative Array with any of the following:
'class' -- the subclass of HTMLFormField that will be used to create the object. *NOT* the CSS class! 'type' -- roughly translates into the <select> type attribute. if 'class' is not specified, this is used as a map through HTMLForm::$typeMappings to get the class name. 'default' -- default value when the form is displayed 'id' -- HTML id attribute 'cssclass' -- CSS class 'options' -- varies according to the specific object. 'label-message' -- message key for a message to use as the label. can be an array of msg key and then parameters to the message. 'label' -- alternatively, a raw text message. Overridden by label-message 'help' -- message text for a message to use as a help text. 'help-message' -- message key for a message to use as a help text. can be an array of msg key and then parameters to the message. Overwrites 'help-messages' and 'help'. 'help-messages' -- array of message key. As above, each item can be an array of msg key and then parameters. Overwrites 'help'. 'required' -- passed through to the object, indicating that it is a required field. 'size' -- the length of text fields 'filter-callback -- a function name to give you the chance to massage the inputted value before it's processed.
Since 1.20, you can chain mutators to ease the form generation:
$form = new HTMLForm( $someFields ); $form->setMethod( 'get' ) ->setWrapperLegendMsg( 'message-key' ) ->suppressReset() ->prepareForm() ->displayForm();
TODO: Document 'section' / 'subsection' stuff
Definition at line 95 of file HTMLForm.php.
HTMLForm::__construct | ( | $ | descriptor, |
$ | context = null , |
||
$ | messagePrefix = '' |
||
) |
Build a new HTMLForm from an array of field attributes.
$descriptor | Array of Field constructs, as described above |
$context | IContextSource available since 1.18, will become compulsory in 1.18. Obviates the need to call $form->setTitle() |
$messagePrefix | String a prefix to go in front of default messages |
Definition at line 193 of file HTMLForm.php.
HTMLForm::addButton | ( | $ | name, |
$ | value, | ||
$ | id = null , |
||
$ | attribs = null |
||
) |
Add a button to the form.
$name | String field name. |
$value | String field value |
$id | String DOM id for the button (default: null) |
$attribs | Array |
Definition at line 573 of file HTMLForm.php.
HTMLForm::addFooterText | ( | $ | msg, |
$ | section = null |
||
) |
Add footer text, inside the form.
$msg | String complete text of message to display |
$section | string The section to add the footer text to |
Definition at line 504 of file HTMLForm.php.
HTMLForm::addHeaderText | ( | $ | msg, |
$ | section = null |
||
) |
Add header text, inside the form.
$msg | String complete text of message to display |
$section | string The section to add the header to |
Definition at line 470 of file HTMLForm.php.
Referenced by SpecialBlock\alterForm().
HTMLForm::addHiddenField | ( | $ | name, |
$ | value, | ||
$ | attribs = array() |
||
) |
Add a hidden field to the output.
$name | String field name. This will be used exactly as entered |
$value | String field value |
$attribs | Array |
Definition at line 559 of file HTMLForm.php.
Referenced by RevertFileAction\alterForm().
static HTMLForm::addJS | ( | ) | [static] |
Add the HTMLForm-specific JavaScript, if it hasn't been done already.
Definition at line 273 of file HTMLForm.php.
HTMLForm::addPostText | ( | $ | msg | ) |
Add text to the end of the display.
$msg | String complete text of message to display |
Definition at line 537 of file HTMLForm.php.
HTMLForm::addPreText | ( | $ | msg | ) |
Add introductory text.
$msg | String complete text of message to display |
Definition at line 459 of file HTMLForm.php.
HTMLForm::displayForm | ( | $ | submitResult | ) |
Display the form (sending to $wgOut), with an appropriate error message or stack of messages, and any validation errors, etc.
$submitResult | Mixed output from HTMLForm::trySubmit() |
Definition at line 589 of file HTMLForm.php.
HTMLForm::displaySection | ( | $ | fields, |
$ | sectionName = '' , |
||
$ | fieldsetIDPrefix = '' |
||
) |
$fields | array[]|HTMLFormField[] array of fields (either arrays or objects) |
$sectionName | string ID attribute of the "<table>" tag for this section, ignored if empty |
$fieldsetIDPrefix | string ID prefix for the "<fieldset>" tag of each subsection, ignored if empty |
Definition at line 930 of file HTMLForm.php.
Referenced by EditWatchlistNormalHTMLForm\getBody(), and PreferencesForm\getBody().
HTMLForm::filterDataForSubmit | ( | $ | data | ) |
Overload this if you want to apply special filtration routines to the form as a whole, after it's submitted but before it's processed.
$data |
Reimplemented in PreferencesForm.
Definition at line 1040 of file HTMLForm.php.
static HTMLForm::formatErrors | ( | $ | errors | ) | [static] |
Format a stack of error messages into a single HTML string.
$errors | Array of message keys/values |
Definition at line 762 of file HTMLForm.php.
Referenced by SpecialBlock\alterForm().
Get the whole body of the form.
Reimplemented in PreferencesForm, and EditWatchlistNormalHTMLForm.
Definition at line 730 of file HTMLForm.php.
Get the submit and (potentially) reset buttons.
Reimplemented in PreferencesForm.
Definition at line 675 of file HTMLForm.php.
HTMLForm::getErrors | ( | $ | errors | ) |
Format and display an error message stack.
$errors | String|Array|Status |
Definition at line 739 of file HTMLForm.php.
Get the hidden fields that should go inside the form.
Definition at line 650 of file HTMLForm.php.
HTMLForm::getHTML | ( | $ | submitResult | ) |
Returns the raw HTML generated by the form.
$submitResult | Mixed output from HTMLForm::trySubmit() |
Definition at line 598 of file HTMLForm.php.
HTMLForm::getLegend | ( | $ | key | ) |
Get a string to go in the "<legend>" of a section fieldset.
Override this if you want something more complicated.
$key | String |
Reimplemented in PreferencesForm, and EditWatchlistNormalHTMLForm.
Definition at line 1050 of file HTMLForm.php.
Definition at line 919 of file HTMLForm.php.
Get the text for the submit button, either customised or a default.
Definition at line 810 of file HTMLForm.php.
Get the title.
Reimplemented from ContextSource.
Definition at line 903 of file HTMLForm.php.
Construct the form fields from the Descriptor array.
Definition at line 1000 of file HTMLForm.php.
References $value, and ContextSource\getRequest().
static HTMLForm::loadInputFromParameters | ( | $ | fieldname, |
$ | descriptor | ||
) | [static] |
Initialise a new Object for the field.
$fieldname | string |
$descriptor | string input Descriptor, as described above |
Definition at line 281 of file HTMLForm.php.
Referenced by ApiOptions\execute(), and Preferences\getPreferences().
Prepare form for submission.
Definition at line 314 of file HTMLForm.php.
HTMLForm::setAction | ( | $ | action | ) |
Set the value for the action attribute of the form.
When set to false (which is the default state), the set title is used.
string | bool | $action |
Definition at line 1063 of file HTMLForm.php.
HTMLForm::setDisplayFormat | ( | $ | format | ) |
Set format in which to display the form.
$format | String the name of the format to use, must be one of $this->availableDisplayFormats |
Definition at line 251 of file HTMLForm.php.
HTMLForm::setFooterText | ( | $ | msg, |
$ | section = null |
||
) |
Set footer text, inside the form.
$msg | String complete text of message to display |
$section | string The section to add the footer text to |
Definition at line 523 of file HTMLForm.php.
HTMLForm::setHeaderText | ( | $ | msg, |
$ | section = null |
||
) |
Set header text, inside the form.
$msg | String complete text of message to display |
$section | The section to add the header to |
Definition at line 489 of file HTMLForm.php.
Referenced by SpecialUnlockdb\alterForm(), SpecialLockdb\alterForm(), and SpecialBlock\alterForm().
HTMLForm::setId | ( | $ | id | ) |
$id | String DOM id for the form |
Definition at line 849 of file HTMLForm.php.
HTMLForm::setIntro | ( | $ | msg | ) |
Set the introductory message, overwriting any existing message.
$msg | String complete text of message to display |
Definition at line 438 of file HTMLForm.php.
HTMLForm::setMessagePrefix | ( | $ | p | ) |
Set the prefix for various default messages.
$p | String |
Definition at line 884 of file HTMLForm.php.
HTMLForm::setMethod | ( | $ | method = 'post' | ) |
Set the method used to submit the form.
$method | String |
Definition at line 914 of file HTMLForm.php.
HTMLForm::setPostText | ( | $ | msg | ) |
Set text at the end of the display.
$msg | String complete text of message to display |
Definition at line 547 of file HTMLForm.php.
HTMLForm::setPreText | ( | $ | msg | ) |
Set the introductory message, overwriting any existing message.
$msg | String complete text of message to display |
Definition at line 449 of file HTMLForm.php.
HTMLForm::setSubmitCallback | ( | $ | cb | ) |
Set a callback to a function to do something with the form once it's been successfully validated.
$cb | String function name. The function will be passed the output from HTMLForm::filterDataForSubmit, and must return Bool true on success, Bool false if no submission was attempted, or String HTML output to display on error. |
Definition at line 417 of file HTMLForm.php.
Referenced by SpecialBlock\alterForm().
HTMLForm::setSubmitID | ( | $ | t | ) |
Set the id for the submit button.
$t | String. |
Definition at line 840 of file HTMLForm.php.
HTMLForm::setSubmitName | ( | $ | name | ) |
$name | String Submit button name |
Definition at line 820 of file HTMLForm.php.
HTMLForm::setSubmitText | ( | $ | t | ) |
Set the text for the submit button.
$t | String plaintext. |
Definition at line 790 of file HTMLForm.php.
HTMLForm::setSubmitTextMsg | ( | $ | msg | ) |
Set the text for the submit button to a message.
$msg | String message key |
Definition at line 801 of file HTMLForm.php.
Referenced by SpecialUnlockdb\alterForm(), SpecialLockdb\alterForm(), RevertFileAction\alterForm(), PurgeAction\alterForm(), SpecialBlock\alterForm(), WatchAction\alterForm(), and UnwatchAction\alterForm().
HTMLForm::setSubmitTooltip | ( | $ | name | ) |
$name | String Tooltip for the submit button |
Definition at line 829 of file HTMLForm.php.
HTMLForm::setTitle | ( | $ | t | ) |
Set the title for form submission.
$t | Title of page the form is on/should be posted to |
Definition at line 894 of file HTMLForm.php.
HTMLForm::setValidationErrorMessage | ( | $ | msg | ) |
Set a message to display on a validation error.
$msg | Mixed String or Array of valid inputs to wfMessage() (so each entry can be either a String or Array) |
Definition at line 428 of file HTMLForm.php.
HTMLForm::setWrapperLegend | ( | $ | legend | ) |
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
$legend | String HTML to go inside the "<legend>" element. Will be escaped |
Definition at line 860 of file HTMLForm.php.
Referenced by SpecialUnlockdb\alterForm(), and SpecialLockdb\alterForm().
HTMLForm::setWrapperLegendMsg | ( | $ | msg | ) |
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
$msg | String message key |
Definition at line 872 of file HTMLForm.php.
Referenced by RevertFileAction\alterForm(), and SpecialBlock\alterForm().
HTMLForm::show | ( | ) |
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or without funky validation errors.
Reimplemented in UploadForm.
Definition at line 360 of file HTMLForm.php.
HTMLForm::suppressReset | ( | $ | suppressReset = true | ) |
Stop a reset button being shown for this form.
$suppressReset | Bool set to false to re-enable the button again |
Definition at line 1028 of file HTMLForm.php.
Try submitting, with edit token check first.
Definition at line 329 of file HTMLForm.php.
Validate all the fields, and call the submision callback function if everything is kosher.
Reimplemented in UploadForm.
Definition at line 379 of file HTMLForm.php.
HTMLForm::wrapForm | ( | $ | html | ) |
Wrap the form innards in an actual "<form>" element.
$html | String HTML contents to wrap. |
Reimplemented in PreferencesForm.
Definition at line 622 of file HTMLForm.php.
Array HTMLForm::$availableDisplayFormats [protected] |
array( 'table', 'div', 'raw', )
Available formats in which to display the form.
Definition at line 180 of file HTMLForm.php.
String HTMLForm::$displayFormat = 'table' [protected] |
Format in which to display form.
For viable options,
Definition at line 175 of file HTMLForm.php.
bool string HTMLForm::$mAction = false [protected] |
Form action URL.
false means we will use the URL to set Title
Definition at line 154 of file HTMLForm.php.
HTMLForm::$mButtons = array() [protected] |
Definition at line 158 of file HTMLForm.php.
HTMLForm::$mFieldData |
Definition at line 129 of file HTMLForm.php.
HTMLForm::$mFieldTree [protected] |
Definition at line 127 of file HTMLForm.php.
HTMLFormField [] HTMLForm::$mFlatFields [protected] |
*
Definition at line 125 of file HTMLForm.php.
HTMLForm::$mFooter = '' [protected] |
Definition at line 136 of file HTMLForm.php.
HTMLForm::$mHeader = '' [protected] |
Definition at line 135 of file HTMLForm.php.
HTMLForm::$mHiddenFields = array() [protected] |
Definition at line 157 of file HTMLForm.php.
HTMLForm::$mId [protected] |
Definition at line 140 of file HTMLForm.php.
HTMLForm::$mMessagePrefix [protected] |
Definition at line 123 of file HTMLForm.php.
HTMLForm::$mMethod = 'post' [protected] |
Definition at line 148 of file HTMLForm.php.
HTMLForm::$mPost = '' [protected] |
Definition at line 139 of file HTMLForm.php.
HTMLForm::$mPre = '' [protected] |
Definition at line 134 of file HTMLForm.php.
HTMLForm::$mSectionFooters = array() [protected] |
Definition at line 138 of file HTMLForm.php.
HTMLForm::$mSectionHeaders = array() [protected] |
Definition at line 137 of file HTMLForm.php.
HTMLForm::$mShowReset = false [protected] |
Definition at line 128 of file HTMLForm.php.
HTMLForm::$mSubmitCallback [protected] |
Definition at line 131 of file HTMLForm.php.
HTMLForm::$mSubmitID [protected] |
Definition at line 142 of file HTMLForm.php.
HTMLForm::$mSubmitName [protected] |
Definition at line 143 of file HTMLForm.php.
HTMLForm::$mSubmitText [protected] |
Definition at line 144 of file HTMLForm.php.
HTMLForm::$mSubmitTooltip [protected] |
Definition at line 145 of file HTMLForm.php.
HTMLForm::$mSubSectionBeforeFields = true [protected] |
If true, sections that contain both fields and subsections will render their subsections before their fields.
Subclasses may set this to false to render subsections after fields instead.
Reimplemented in PreferencesForm.
Definition at line 169 of file HTMLForm.php.
HTMLForm::$mTitle [protected] |
Definition at line 147 of file HTMLForm.php.
HTMLForm::$mUseMultipart = false [protected] |
Definition at line 156 of file HTMLForm.php.
HTMLForm::$mValidationErrorMessage [protected] |
Definition at line 132 of file HTMLForm.php.
HTMLForm::$mWrapperLegend = false [protected] |
Definition at line 160 of file HTMLForm.php.
HTMLForm::$typeMappings [static] |
array( 'api' => 'HTMLApiField', 'text' => 'HTMLTextField', 'textarea' => 'HTMLTextAreaField', 'select' => 'HTMLSelectField', 'radio' => 'HTMLRadioField', 'multiselect' => 'HTMLMultiSelectField', 'check' => 'HTMLCheckField', 'toggle' => 'HTMLCheckField', 'int' => 'HTMLIntField', 'float' => 'HTMLFloatField', 'info' => 'HTMLInfoField', 'selectorother' => 'HTMLSelectOrOtherField', 'selectandother' => 'HTMLSelectAndOtherField', 'submit' => 'HTMLSubmitField', 'hidden' => 'HTMLHiddenField', 'edittools' => 'HTMLEditTools', 'email' => 'HTMLTextField', 'password' => 'HTMLTextField', )
Definition at line 98 of file HTMLForm.php.