MediaWiki  REL1_19
HTMLForm Class Reference

Object handling generic submission, CSRF protection, layout and other logic for UI forms. More...

Inheritance diagram for HTMLForm:
Collaboration diagram for HTMLForm:

List of all members.

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)
 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= '')
 TODO: Document.
 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.
 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.
 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 TODO: currently only used for the <fieldset> legend on forms with multiple sections; should be used elsewhre?
 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 valiation 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

 $mAction = false
 $mButtons = array()
 $mFieldTree
 $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

Detailed Description

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.

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' -- 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'. 'help-messages' -- array of message key. As above, each item can be an array of msg key and then parameters. Overwrites 'help-message'. '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.

See also:
HTMLForm::filter() 'validation-callback' -- a function name to give you the chance to impose extra validation on the field input.
HTMLForm::validate() 'name' -- By default, the 'name' attribute of the input field is "wp{$fieldname}". If you want a different name (eg one without the "wp" prefix), specify it here and it will be used without modification.

TODO: Document 'section' / 'subsection' stuff

Definition at line 56 of file HTMLForm.php.


Constructor & Destructor Documentation

HTMLForm::__construct ( descriptor,
context = null,
messagePrefix = '' 
)

Build a new HTMLForm from an array of field attributes.

Parameters:
$descriptorArray of Field constructs, as described above
$contextIContextSource available since 1.18, will become compulsory in 1.18. Obviates the need to call $form->setTitle()
$messagePrefixString a prefix to go in front of default messages

Definition at line 141 of file HTMLForm.php.

References ContextSource\$context, $section, loadInputFromParameters(), and ContextSource\setContext().

Here is the call graph for this function:


Member Function Documentation

HTMLForm::addButton ( name,
value,
id = null,
attribs = null 
)

Definition at line 444 of file HTMLForm.php.

HTMLForm::addFooterText ( msg,
section = null 
)

Add footer text, inside the form.

Parameters:
$msgString complete text of message to display
$sectionstring The section to add the footer text to

Definition at line 396 of file HTMLForm.php.

References $section.

HTMLForm::addHeaderText ( msg,
section = null 
)

Add header text, inside the form.

Parameters:
$msgString complete text of message to display
$sectionstring The section to add the header to

Definition at line 366 of file HTMLForm.php.

References $section.

Referenced by SpecialBlock\alterForm(), and UploadForm\getDescriptionSection().

Here is the caller graph for this function:

HTMLForm::addHiddenField ( name,
value,
attribs = array() 
)

Add a hidden field to the output.

Parameters:
$nameString field name. This will be used exactly as entered
$valueString field value
$attribsArray

Definition at line 439 of file HTMLForm.php.

Referenced by RevertFileAction\alterForm().

Here is the caller graph for this function:

static HTMLForm::addJS ( ) [static]

Add the HTMLForm-specific JavaScript, if it hasn't been done already.

Deprecated:
since 1.18 load modules with ResourceLoader instead

Definition at line 197 of file HTMLForm.php.

References wfDeprecated().

Here is the call graph for this function:

Add text to the end of the display.

Parameters:
$msgString complete text of message to display

Definition at line 425 of file HTMLForm.php.

Add introductory text.

Parameters:
$msgString complete text of message to display

Definition at line 359 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.

Parameters:
$submitResultMixed output from HTMLForm::trySubmit()

Definition at line 453 of file HTMLForm.php.

References getHTML(), and ContextSource\getOutput().

Referenced by show().

Here is the call graph for this function:

Here is the caller graph for this function:

HTMLForm::displaySection ( fields,
sectionName = '',
fieldsetIDPrefix = '' 
)

TODO: Document.

Parameters:
$fieldsarray[]|HTMLFormField[] array of fields (either arrays or objects)
$sectionNamestring ID attribute of the
for this section, ignored if empty
$fieldsetIDPrefixstring ID prefix for the <fieldset> tag of each subsection, ignored if empty
Returns:
String

Definition at line 761 of file HTMLForm.php.

References $section, Sanitizer\escapeId(), Xml\fieldset(), getLegend(), and Html\rawElement().

Referenced by getBody(), EditWatchlistNormalHTMLForm\getBody(), and PreferencesForm\getBody().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Parameters:
$data
Returns:
unknown_type

Reimplemented in PreferencesForm.

Definition at line 858 of file HTMLForm.php.

Referenced by trySubmit().

Here is the caller graph for this function:

static HTMLForm::formatErrors ( errors) [static]

Format a stack of error messages into a single HTML string.

Parameters:
$errorsArray of message keys/values
Returns:
String HTML, a
of errors

Definition at line 626 of file HTMLForm.php.

References Html\rawElement(), and wfMsgExt().

Referenced by SpecialBlock\alterForm(), and getErrors().

Here is the call graph for this function:

Here is the caller graph for this function:

Get the whole body of the form.

Returns:
String

Reimplemented in PreferencesForm, and EditWatchlistNormalHTMLForm.

Definition at line 594 of file HTMLForm.php.

References displaySection().

Referenced by getHTML().

Here is the call graph for this function:

Here is the caller graph for this function:

Get the submit and (potentially) reset buttons.

Returns:
String HTML.

Reimplemented in PreferencesForm.

Definition at line 539 of file HTMLForm.php.

References Html\element(), getSubmitText(), Xml\submitButton(), Linker\tooltipAndAccesskeyAttribs(), and wfMsg().

Referenced by getHTML().

Here is the call graph for this function:

Here is the caller graph for this function:

HTMLForm::getErrors ( errors)

Format and display an error message stack.

Parameters:
$errorsString|Array|Status
Returns:
String

Definition at line 603 of file HTMLForm.php.

References formatErrors(), ContextSource\getOutput(), and Html\rawElement().

Referenced by getHTML().

Here is the call graph for this function:

Here is the caller graph for this function:

Get the hidden fields that should go inside the form.

Returns:
String HTML.

Definition at line 514 of file HTMLForm.php.

References $wgUsePathInfo, getMethod(), getTitle(), ContextSource\getUser(), and Html\hidden().

Referenced by getHTML().

Here is the call graph for this function:

Here is the caller graph for this function:

HTMLForm::getHTML ( submitResult)

Returns the raw HTML generated by the form.

Parameters:
$submitResultMixed output from HTMLForm::trySubmit()
Returns:
string

Definition at line 462 of file HTMLForm.php.

References getBody(), getButtons(), getErrors(), getHiddenFields(), ContextSource\getOutput(), and wrapForm().

Referenced by displayForm().

Here is the call graph for this function:

Here is the caller graph for this function:

HTMLForm::getLegend ( key)

Get a string to go in the <legend> of a section fieldset.

Override this if you want something more complicated

Parameters:
$keyString
Returns:
String

Reimplemented in PreferencesForm, and EditWatchlistNormalHTMLForm.

Definition at line 868 of file HTMLForm.php.

References wfMsg().

Referenced by displaySection().

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 750 of file HTMLForm.php.

Referenced by getHiddenFields(), and tryAuthorizedSubmit().

Here is the caller graph for this function:

Get the text for the submit button, either customised or a default.

Returns:
unknown_type

Definition at line 670 of file HTMLForm.php.

References wfMsg().

Referenced by getButtons().

Here is the call graph for this function:

Here is the caller graph for this function:

Get the title.

Returns:
Title

Reimplemented from ContextSource.

Definition at line 736 of file HTMLForm.php.

References ContextSource\getContext().

Referenced by getHiddenFields(), and wrapForm().

Here is the call graph for this function:

Here is the caller graph for this function:

Construct the form fields from the Descriptor array.

Definition at line 820 of file HTMLForm.php.

References ContextSource\getRequest().

Referenced by prepareForm().

Here is the call graph for this function:

Here is the caller graph for this function:

static HTMLForm::loadInputFromParameters ( fieldname,
descriptor 
) [static]

Initialise a new Object for the field.

Parameters:
$fieldnamestring
$descriptorstring input Descriptor, as described above
Returns:
HTMLFormField subclass

Definition at line 205 of file HTMLForm.php.

Referenced by __construct(), and Preferences\getPreferences().

Here is the caller graph for this function:

Prepare form for submission.

Definition at line 229 of file HTMLForm.php.

References loadData().

Referenced by show().

Here is the call graph for this function:

Here is the caller graph for this function:

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.

Since:
1.19
Parameters:
string | false$action

Definition at line 880 of file HTMLForm.php.

HTMLForm::setFooterText ( msg,
section = null 
)

Set footer text, inside the form.

Since:
1.19
Parameters:
$msgString complete text of message to display
$sectionstring The section to add the footer text to

Definition at line 413 of file HTMLForm.php.

References $section.

HTMLForm::setHeaderText ( msg,
section = null 
)

Set header text, inside the form.

Since:
1.19
Parameters:
$msgString complete text of message to display
$sectionThe section to add the header to

Definition at line 383 of file HTMLForm.php.

References $section.

Referenced by SpecialUnlockdb\alterForm(), SpecialLockdb\alterForm(), and SpecialBlock\alterForm().

Here is the caller graph for this function:

HTMLForm::setId ( id)

Definition at line 693 of file HTMLForm.php.

Referenced by UploadForm\__construct().

Here is the caller graph for this function:

HTMLForm::setIntro ( msg)

Set the introductory message, overwriting any existing message.

Parameters:
$msgString complete text of message to display

Definition at line 344 of file HTMLForm.php.

References setPreText().

Here is the call graph for this function:

Set the prefix for various default messages TODO: currently only used for the <fieldset> legend on forms with multiple sections; should be used elsewhre?

Parameters:
$pString

Definition at line 720 of file HTMLForm.php.

HTMLForm::setMethod ( method = 'post')

Set the method used to submit the form.

Parameters:
$methodString

Definition at line 746 of file HTMLForm.php.

Set text at the end of the display.

Parameters:
$msgString complete text of message to display

Definition at line 431 of file HTMLForm.php.

Set the introductory message, overwriting any existing message.

Since:
1.19
Parameters:
$msgString complete text of message to display

Definition at line 353 of file HTMLForm.php.

Referenced by setIntro().

Here is the caller graph for this function:

Set a callback to a function to do something with the form once it's been successfully validated.

Parameters:
$cbString 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 327 of file HTMLForm.php.

Referenced by SpecialBlock\alterForm().

Here is the caller graph for this function:

Set the id for the submit button.

Parameters:
$tString.
Todo:
FIXME: Integrity of $t is *not* validated

Definition at line 689 of file HTMLForm.php.

References $t.

Definition at line 676 of file HTMLForm.php.

Referenced by UploadForm\__construct().

Here is the caller graph for this function:

Set the text for the submit button.

Parameters:
$tString plaintext.

Definition at line 653 of file HTMLForm.php.

References $t.

Referenced by UploadForm\__construct(), RevertFileAction\alterForm(), PurgeAction\alterForm(), SpecialPasswordReset\alterForm(), WatchAction\alterForm(), UnwatchAction\alterForm(), and setSubmitTextMsg().

Here is the caller graph for this function:

Set the text for the submit button to a message.

Since:
1.19
Parameters:
$msgString message key

Definition at line 662 of file HTMLForm.php.

References ContextSource\msg(), and setSubmitText().

Referenced by SpecialUnlockdb\alterForm(), SpecialLockdb\alterForm(), and SpecialBlock\alterForm().

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 680 of file HTMLForm.php.

Referenced by UploadForm\__construct().

Here is the caller graph for this function:

Set the title for form submission.

Parameters:
$tTitle of page the form is on/should be posted to

Definition at line 728 of file HTMLForm.php.

References $t.

Set a message to display on a validation error.

Parameters:
$msgMixed String or Array of valid inputs to wfMsgExt() (so each entry can be either a String or Array)

Definition at line 336 of file HTMLForm.php.

Prompt the whole form to be wrapped in a <fieldset>, with this text as its <legend> element.

Parameters:
$legendString HTML to go inside the <legend> element. Will be escaped

Definition at line 702 of file HTMLForm.php.

Referenced by SpecialUnlockdb\alterForm(), SpecialLockdb\alterForm(), RevertFileAction\alterForm(), and setWrapperLegendMsg().

Here is the caller graph for this function:

Prompt the whole form to be wrapped in a <fieldset>, with this message as its <legend> element.

Since:
1.19
Parameters:
$msgString message key

Definition at line 710 of file HTMLForm.php.

References ContextSource\msg(), and setWrapperLegend().

Referenced by SpecialBlock\alterForm().

Here is the call graph for this function:

Here is the caller graph for this function:

The here's-one-I-made-earlier option: do the submission if posted, or display the form with or without funky valiation errors.

Returns:
Bool or Status whether submission was successful.

Reimplemented in UploadForm.

Definition at line 274 of file HTMLForm.php.

References $result, displayForm(), prepareForm(), and tryAuthorizedSubmit().

Here is the call graph for this function:

HTMLForm::suppressReset ( suppressReset = true)

Stop a reset button being shown for this form.

Parameters:
$suppressResetBool set to false to re-enable the button again

Definition at line 847 of file HTMLForm.php.

Try submitting, with edit token check first.

Returns:
Status|boolean

Definition at line 243 of file HTMLForm.php.

References $result, getMethod(), ContextSource\getRequest(), ContextSource\getUser(), and trySubmit().

Referenced by show().

Here is the call graph for this function:

Here is the caller graph for this function:

Validate all the fields, and call the submision callback function if everything is kosher.

Returns:
Mixed Bool true == Successful submission, Bool false == No submission attempted, anything else == Error to display.

Reimplemented in UploadForm.

Definition at line 293 of file HTMLForm.php.

References $res, and filterDataForSubmit().

Referenced by tryAuthorizedSubmit().

Here is the call graph for this function:

Here is the caller graph for this function:

HTMLForm::wrapForm ( html)

Wrap the form innards in an actual <form> element.

Parameters:
$htmlString HTML contents to wrap.
Returns:
String wrapped HTML.

Reimplemented in PreferencesForm.

Definition at line 486 of file HTMLForm.php.

References Xml\fieldset(), getTitle(), and Html\rawElement().

Referenced by getHTML().

Here is the call graph for this function:

Here is the caller graph for this function:


Member Data Documentation

HTMLForm::$mAction = false [protected]

Definition at line 117 of file HTMLForm.php.

HTMLForm::$mButtons = array() [protected]

Definition at line 121 of file HTMLForm.php.

HTMLForm::$mFieldData

Definition at line 91 of file HTMLForm.php.

HTMLForm::$mFieldTree [protected]

Definition at line 89 of file HTMLForm.php.

HTMLForm::$mFlatFields [protected]

Definition at line 87 of file HTMLForm.php.

HTMLForm::$mFooter = '' [protected]

Definition at line 98 of file HTMLForm.php.

HTMLForm::$mHeader = '' [protected]

Definition at line 97 of file HTMLForm.php.

HTMLForm::$mHiddenFields = array() [protected]

Definition at line 120 of file HTMLForm.php.

HTMLForm::$mId [protected]

Definition at line 102 of file HTMLForm.php.

HTMLForm::$mMessagePrefix [protected]

Definition at line 84 of file HTMLForm.php.

HTMLForm::$mMethod = 'post' [protected]

Definition at line 110 of file HTMLForm.php.

HTMLForm::$mPost = '' [protected]

Definition at line 101 of file HTMLForm.php.

HTMLForm::$mPre = '' [protected]

Definition at line 96 of file HTMLForm.php.

HTMLForm::$mSectionFooters = array() [protected]

Definition at line 100 of file HTMLForm.php.

HTMLForm::$mSectionHeaders = array() [protected]

Definition at line 99 of file HTMLForm.php.

HTMLForm::$mShowReset = false [protected]

Definition at line 90 of file HTMLForm.php.

HTMLForm::$mSubmitCallback [protected]

Definition at line 93 of file HTMLForm.php.

HTMLForm::$mSubmitID [protected]

Definition at line 104 of file HTMLForm.php.

HTMLForm::$mSubmitName [protected]

Definition at line 105 of file HTMLForm.php.

HTMLForm::$mSubmitText [protected]

Definition at line 106 of file HTMLForm.php.

HTMLForm::$mSubmitTooltip [protected]

Definition at line 107 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 132 of file HTMLForm.php.

HTMLForm::$mTitle [protected]

Definition at line 109 of file HTMLForm.php.

HTMLForm::$mUseMultipart = false [protected]

Definition at line 119 of file HTMLForm.php.

HTMLForm::$mValidationErrorMessage [protected]

Definition at line 94 of file HTMLForm.php.

HTMLForm::$mWrapperLegend = false [protected]

Definition at line 123 of file HTMLForm.php.

HTMLForm::$typeMappings [static]
Initial value:
 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 59 of file HTMLForm.php.


The documentation for this class was generated from the following file: