4 * HTML form generation and submission handling.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 * Object handling generic submission, CSRF protection, layout and
26 * other logic for UI forms. in a reusable manner.
28 * In order to generate the form, the HTMLForm object takes an array
29 * structure detailing the form fields available. Each element of the
30 * array is a basic property-list, including the type of field, the
31 * label it is to be given in the form, callbacks for validation and
32 * 'filtering', and other pertinent information.
34 * Field types are implemented as subclasses of the generic HTMLFormField
35 * object, and typically implement at least getInputHTML, which generates
36 * the HTML for the input field to be placed in the table.
38 * You can find extensive documentation on the www.mediawiki.org wiki:
39 * - https://www.mediawiki.org/wiki/HTMLForm
40 * - https://www.mediawiki.org/wiki/HTMLForm/tutorial
42 * The constructor input is an associative array of $fieldname => $info,
43 * where $info is an Associative Array with any of the following:
45 * 'class' -- the subclass of HTMLFormField that will be used
46 * to create the object. *NOT* the CSS class!
47 * 'type' -- roughly translates into the <select> type attribute.
48 * if 'class' is not specified, this is used as a map
49 * through HTMLForm::$typeMappings to get the class name.
50 * 'default' -- default value when the form is displayed
51 * 'id' -- HTML id attribute
52 * 'cssclass' -- CSS class
53 * 'csshelpclass' -- CSS class used to style help text
54 * 'dir' -- Direction of the element.
55 * 'options' -- associative array mapping labels to values.
56 * Some field types support multi-level arrays.
57 * 'options-messages' -- associative array mapping message keys to values.
58 * Some field types support multi-level arrays.
59 * 'options-message' -- message key or object to be parsed to extract the list of
60 * options (like 'ipbreason-dropdown').
61 * 'label-message' -- message key or object for a message to use as the label.
62 * can be an array of msg key and then parameters to
64 * 'label' -- alternatively, a raw text message. Overridden by
66 * 'help' -- message text for a message to use as a help text.
67 * 'help-message' -- message key or object for a message to use as a help text.
68 * can be an array of msg key and then parameters to
70 * Overwrites 'help-messages' and 'help'.
71 * 'help-messages' -- array of message keys/objects. As above, each item can
72 * be an array of msg key and then parameters.
74 * 'notice' -- message text for a message to use as a notice in the field.
75 * Currently used by OOUI form fields only.
76 * 'notice-messages' -- array of message keys/objects to use for notice.
78 * 'notice-message' -- message key or object to use as a notice.
79 * 'required' -- passed through to the object, indicating that it
80 * is a required field.
81 * 'size' -- the length of text fields
82 * 'filter-callback' -- a function name to give you the chance to
83 * massage the inputted value before it's processed.
84 * @see HTMLFormField::filter()
85 * 'validation-callback' -- a function name to give you the chance
86 * to impose extra validation on the field input.
87 * @see HTMLFormField::validate()
88 * 'name' -- By default, the 'name' attribute of the input field
89 * is "wp{$fieldname}". If you want a different name
90 * (eg one without the "wp" prefix), specify it here and
91 * it will be used without modification.
92 * 'hide-if' -- expression given as an array stating when the field
93 * should be hidden. The first array value has to be the
94 * expression's logic operator. Supported expressions:
96 * [ 'NOT', array $expression ]
97 * To hide a field if a given expression is not true.
99 * [ '===', string $fieldName, string $value ]
100 * To hide a field if another field identified by
101 * $field has the value $value.
103 * [ '!==', string $fieldName, string $value ]
104 * Same as [ 'NOT', [ '===', $fieldName, $value ]
105 * 'OR', 'AND', 'NOR', 'NAND'
106 * [ 'XXX', array $expression1, ..., array $expressionN ]
107 * To hide a field if one or more (OR), all (AND),
108 * neither (NOR) or not all (NAND) given expressions
109 * are evaluated as true.
110 * The expressions will be given to a JavaScript frontend
111 * module which will continually update the field's
114 * Since 1.20, you can chain mutators to ease the form generation:
117 * $form = new HTMLForm( $someFields );
118 * $form->setMethod( 'get' )
119 * ->setWrapperLegendMsg( 'message-key' )
121 * ->displayForm( '' );
123 * Note that you will have prepareForm and displayForm at the end. Other
124 * methods call done after that would simply not be part of the form :(
126 * @todo Document 'section' / 'subsection' stuff
128 class HTMLForm
extends ContextSource
{
129 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
130 public static $typeMappings = [
131 'api' => 'HTMLApiField',
132 'text' => 'HTMLTextField',
133 'textwithbutton' => 'HTMLTextFieldWithButton',
134 'textarea' => 'HTMLTextAreaField',
135 'select' => 'HTMLSelectField',
136 'combobox' => 'HTMLComboboxField',
137 'radio' => 'HTMLRadioField',
138 'multiselect' => 'HTMLMultiSelectField',
139 'limitselect' => 'HTMLSelectLimitField',
140 'check' => 'HTMLCheckField',
141 'toggle' => 'HTMLCheckField',
142 'int' => 'HTMLIntField',
143 'float' => 'HTMLFloatField',
144 'info' => 'HTMLInfoField',
145 'selectorother' => 'HTMLSelectOrOtherField',
146 'selectandother' => 'HTMLSelectAndOtherField',
147 'namespaceselect' => 'HTMLSelectNamespace',
148 'namespaceselectwithbutton' => 'HTMLSelectNamespaceWithButton',
149 'tagfilter' => 'HTMLTagFilter',
150 'sizefilter' => 'HTMLSizeFilterField',
151 'submit' => 'HTMLSubmitField',
152 'hidden' => 'HTMLHiddenField',
153 'edittools' => 'HTMLEditTools',
154 'checkmatrix' => 'HTMLCheckMatrix',
155 'cloner' => 'HTMLFormFieldCloner',
156 'autocompleteselect' => 'HTMLAutoCompleteSelectField',
157 'date' => 'HTMLDateTimeField',
158 'time' => 'HTMLDateTimeField',
159 'datetime' => 'HTMLDateTimeField',
160 // HTMLTextField will output the correct type="" attribute automagically.
161 // There are about four zillion other HTML5 input types, like range, but
162 // we don't use those at the moment, so no point in adding all of them.
163 'email' => 'HTMLTextField',
164 'password' => 'HTMLTextField',
165 'url' => 'HTMLTextField',
166 'title' => 'HTMLTitleTextField',
167 'user' => 'HTMLUserTextField',
172 protected $mMessagePrefix;
174 /** @var HTMLFormField[] */
175 protected $mFlatFields;
177 protected $mFieldTree;
178 protected $mShowReset = false;
179 protected $mShowSubmit = true;
180 protected $mSubmitFlags = [ 'primary', 'progressive' ];
181 protected $mShowCancel = false;
182 protected $mCancelTarget;
184 protected $mSubmitCallback;
185 protected $mValidationErrorMessage;
187 protected $mPre = '';
188 protected $mHeader = '';
189 protected $mFooter = '';
190 protected $mSectionHeaders = [];
191 protected $mSectionFooters = [];
192 protected $mPost = '';
195 protected $mTableId = '';
197 protected $mSubmitID;
198 protected $mSubmitName;
199 protected $mSubmitText;
200 protected $mSubmitTooltip;
202 protected $mFormIdentifier;
204 protected $mMethod = 'post';
205 protected $mWasSubmitted = false;
208 * Form action URL. false means we will use the URL to set Title
212 protected $mAction = false;
215 * Form attribute autocomplete. false does not set the attribute
219 protected $mAutocomplete = false;
221 protected $mUseMultipart = false;
222 protected $mHiddenFields = [];
223 protected $mButtons = [];
225 protected $mWrapperLegend = false;
228 * Salt for the edit token.
231 protected $mTokenSalt = '';
234 * If true, sections that contain both fields and subsections will
235 * render their subsections before their fields.
237 * Subclasses may set this to false to render subsections after fields
240 protected $mSubSectionBeforeFields = true;
243 * Format in which to display form. For viable options,
244 * @see $availableDisplayFormats
247 protected $displayFormat = 'table';
250 * Available formats in which to display the form
253 protected $availableDisplayFormats = [
261 * Available formats in which to display the form
264 protected $availableSubclassDisplayFormats = [
270 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
272 * @param string $displayFormat
273 * @param mixed $arguments... Additional arguments to pass to the constructor.
276 public static function factory( $displayFormat/*, $arguments...*/ ) {
277 $arguments = func_get_args();
278 array_shift( $arguments );
280 switch ( $displayFormat ) {
282 return ObjectFactory
::constructClassInstance( VFormHTMLForm
::class, $arguments );
284 return ObjectFactory
::constructClassInstance( OOUIHTMLForm
::class, $arguments );
286 /** @var HTMLForm $form */
287 $form = ObjectFactory
::constructClassInstance( HTMLForm
::class, $arguments );
288 $form->setDisplayFormat( $displayFormat );
294 * Build a new HTMLForm from an array of field attributes
296 * @param array $descriptor Array of Field constructs, as described above
297 * @param IContextSource $context Available since 1.18, will become compulsory in 1.18.
298 * Obviates the need to call $form->setTitle()
299 * @param string $messagePrefix A prefix to go in front of default messages
301 public function __construct( $descriptor, /*IContextSource*/ $context = null,
304 if ( $context instanceof IContextSource
) {
305 $this->setContext( $context );
306 $this->mTitle
= false; // We don't need them to set a title
307 $this->mMessagePrefix
= $messagePrefix;
308 } elseif ( $context === null && $messagePrefix !== '' ) {
309 $this->mMessagePrefix
= $messagePrefix;
310 } elseif ( is_string( $context ) && $messagePrefix === '' ) {
312 // it's actually $messagePrefix
313 $this->mMessagePrefix
= $context;
316 // Evil hack for mobile :(
318 !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
319 && $this->displayFormat
=== 'table'
321 $this->displayFormat
= 'div';
324 // Expand out into a tree.
325 $loadedDescriptor = [];
326 $this->mFlatFields
= [];
328 foreach ( $descriptor as $fieldname => $info ) {
329 $section = isset( $info['section'] )
333 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
334 $this->mUseMultipart
= true;
337 $field = static::loadInputFromParameters( $fieldname, $info, $this );
339 $setSection =& $loadedDescriptor;
341 $sectionParts = explode( '/', $section );
343 while ( count( $sectionParts ) ) {
344 $newName = array_shift( $sectionParts );
346 if ( !isset( $setSection[$newName] ) ) {
347 $setSection[$newName] = [];
350 $setSection =& $setSection[$newName];
354 $setSection[$fieldname] = $field;
355 $this->mFlatFields
[$fieldname] = $field;
358 $this->mFieldTree
= $loadedDescriptor;
362 * @param string $fieldname
365 public function hasField( $fieldname ) {
366 return isset( $this->mFlatFields
[$fieldname] );
370 * @param string $fieldname
371 * @return HTMLFormField
372 * @throws DomainException on invalid field name
374 public function getField( $fieldname ) {
375 if ( !$this->hasField( $fieldname ) ) {
376 throw new DomainException( __METHOD__
. ': no field named ' . $fieldname );
378 return $this->mFlatFields
[$fieldname];
382 * Set format in which to display the form
384 * @param string $format The name of the format to use, must be one of
385 * $this->availableDisplayFormats
387 * @throws MWException
389 * @return HTMLForm $this for chaining calls (since 1.20)
391 public function setDisplayFormat( $format ) {
393 in_array( $format, $this->availableSubclassDisplayFormats
, true ) ||
394 in_array( $this->displayFormat
, $this->availableSubclassDisplayFormats
, true )
396 throw new MWException( 'Cannot change display format after creation, ' .
397 'use HTMLForm::factory() instead' );
400 if ( !in_array( $format, $this->availableDisplayFormats
, true ) ) {
401 throw new MWException( 'Display format must be one of ' .
402 print_r( $this->availableDisplayFormats
, true ) );
405 // Evil hack for mobile :(
406 if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
410 $this->displayFormat
= $format;
416 * Getter for displayFormat
420 public function getDisplayFormat() {
421 return $this->displayFormat
;
425 * Test if displayFormat is 'vform'
427 * @deprecated since 1.25
430 public function isVForm() {
431 wfDeprecated( __METHOD__
, '1.25' );
436 * Get the HTMLFormField subclass for this descriptor.
438 * The descriptor can be passed either 'class' which is the name of
439 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
440 * This makes sure the 'class' is always set, and also is returned by
441 * this function for ease.
445 * @param string $fieldname Name of the field
446 * @param array $descriptor Input Descriptor, as described above
448 * @throws MWException
449 * @return string Name of a HTMLFormField subclass
451 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
452 if ( isset( $descriptor['class'] ) ) {
453 $class = $descriptor['class'];
454 } elseif ( isset( $descriptor['type'] ) ) {
455 $class = static::$typeMappings[$descriptor['type']];
456 $descriptor['class'] = $class;
462 throw new MWException( "Descriptor with no class for $fieldname: "
463 . print_r( $descriptor, true ) );
470 * Initialise a new Object for the field
472 * @param string $fieldname Name of the field
473 * @param array $descriptor Input Descriptor, as described above
474 * @param HTMLForm|null $parent Parent instance of HTMLForm
476 * @throws MWException
477 * @return HTMLFormField Instance of a subclass of HTMLFormField
479 public static function loadInputFromParameters( $fieldname, $descriptor,
480 HTMLForm
$parent = null
482 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
484 $descriptor['fieldname'] = $fieldname;
486 $descriptor['parent'] = $parent;
489 # @todo This will throw a fatal error whenever someone try to use
490 # 'class' to feed a CSS class instead of 'cssclass'. Would be
491 # great to avoid the fatal error and show a nice error.
492 return new $class( $descriptor );
496 * Prepare form for submission.
498 * @attention When doing method chaining, that should be the very last
499 * method call before displayForm().
501 * @throws MWException
502 * @return HTMLForm $this for chaining calls (since 1.20)
504 public function prepareForm() {
505 # Check if we have the info we need
506 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
507 throw new MWException( 'You must call setTitle() on an HTMLForm' );
510 # Load data from the request.
512 $this->mFormIdentifier
=== null ||
513 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
517 $this->mFieldData
= [];
524 * Try submitting, with edit token check first
525 * @return Status|bool
527 public function tryAuthorizedSubmit() {
531 if ( $this->mFormIdentifier
=== null ) {
534 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
;
538 if ( $this->getMethod() !== 'post' ) {
539 $tokenOkay = true; // no session check needed
540 } elseif ( $this->getRequest()->wasPosted() ) {
541 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
542 if ( $this->getUser()->isLoggedIn() ||
$editToken !== null ) {
543 // Session tokens for logged-out users have no security value.
544 // However, if the user gave one, check it in order to give a nice
545 // "session expired" error instead of "permission denied" or such.
546 $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt
);
552 if ( $tokenOkay && $identOkay ) {
553 $this->mWasSubmitted
= true;
554 $result = $this->trySubmit();
561 * The here's-one-I-made-earlier option: do the submission if
562 * posted, or display the form with or without funky validation
564 * @return bool|Status Whether submission was successful.
566 public function show() {
567 $this->prepareForm();
569 $result = $this->tryAuthorizedSubmit();
570 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
574 $this->displayForm( $result );
580 * Same as self::show with the difference, that the form will be
581 * added to the output, no matter, if the validation was good or not.
582 * @return bool|Status Whether submission was successful.
584 public function showAlways() {
585 $this->prepareForm();
587 $result = $this->tryAuthorizedSubmit();
589 $this->displayForm( $result );
595 * Validate all the fields, and call the submission callback
596 * function if everything is kosher.
597 * @throws MWException
598 * @return bool|string|array|Status
599 * - Bool true or a good Status object indicates success,
600 * - Bool false indicates no submission was attempted,
601 * - Anything else indicates failure. The value may be a fatal Status
602 * object, an HTML string, or an array of arrays (message keys and
603 * params) or strings (message keys)
605 public function trySubmit() {
608 $hoistedErrors[] = isset( $this->mValidationErrorMessage
)
609 ?
$this->mValidationErrorMessage
610 : [ 'htmlform-invalid-input' ];
612 $this->mWasSubmitted
= true;
614 # Check for cancelled submission
615 foreach ( $this->mFlatFields
as $fieldname => $field ) {
616 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
619 if ( $field->cancelSubmit( $this->mFieldData
[$fieldname], $this->mFieldData
) ) {
620 $this->mWasSubmitted
= false;
625 # Check for validation
626 foreach ( $this->mFlatFields
as $fieldname => $field ) {
627 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
630 if ( $field->isHidden( $this->mFieldData
) ) {
633 $res = $field->validate( $this->mFieldData
[$fieldname], $this->mFieldData
);
634 if ( $res !== true ) {
636 if ( $res !== false && !$field->canDisplayErrors() ) {
637 $hoistedErrors[] = [ 'rawmessage', $res ];
643 if ( count( $hoistedErrors ) === 1 ) {
644 $hoistedErrors = $hoistedErrors[0];
646 return $hoistedErrors;
649 $callback = $this->mSubmitCallback
;
650 if ( !is_callable( $callback ) ) {
651 throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
652 'setSubmitCallback() to set one.' );
655 $data = $this->filterDataForSubmit( $this->mFieldData
);
657 $res = call_user_func( $callback, $data, $this );
658 if ( $res === false ) {
659 $this->mWasSubmitted
= false;
666 * Test whether the form was considered to have been submitted or not, i.e.
667 * whether the last call to tryAuthorizedSubmit or trySubmit returned
670 * This will return false until HTMLForm::tryAuthorizedSubmit or
671 * HTMLForm::trySubmit is called.
676 public function wasSubmitted() {
677 return $this->mWasSubmitted
;
681 * Set a callback to a function to do something with the form
682 * once it's been successfully validated.
684 * @param callable $cb The function will be passed the output from
685 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
686 * return as documented for HTMLForm::trySubmit
688 * @return HTMLForm $this for chaining calls (since 1.20)
690 public function setSubmitCallback( $cb ) {
691 $this->mSubmitCallback
= $cb;
697 * Set a message to display on a validation error.
699 * @param string|array $msg String or Array of valid inputs to wfMessage()
700 * (so each entry can be either a String or Array)
702 * @return HTMLForm $this for chaining calls (since 1.20)
704 public function setValidationErrorMessage( $msg ) {
705 $this->mValidationErrorMessage
= $msg;
711 * Set the introductory message, overwriting any existing message.
713 * @param string $msg Complete text of message to display
715 * @return HTMLForm $this for chaining calls (since 1.20)
717 public function setIntro( $msg ) {
718 $this->setPreText( $msg );
724 * Set the introductory message HTML, overwriting any existing message.
727 * @param string $msg Complete HTML of message to display
729 * @return HTMLForm $this for chaining calls (since 1.20)
731 public function setPreText( $msg ) {
738 * Add HTML to introductory message.
740 * @param string $msg Complete HTML of message to display
742 * @return HTMLForm $this for chaining calls (since 1.20)
744 public function addPreText( $msg ) {
751 * Add HTML to the header, inside the form.
753 * @param string $msg Additional HTML to display in header
754 * @param string|null $section The section to add the header to
756 * @return HTMLForm $this for chaining calls (since 1.20)
758 public function addHeaderText( $msg, $section = null ) {
759 if ( $section === null ) {
760 $this->mHeader
.= $msg;
762 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
763 $this->mSectionHeaders
[$section] = '';
765 $this->mSectionHeaders
[$section] .= $msg;
772 * Set header text, inside the form.
775 * @param string $msg Complete HTML of header to display
776 * @param string|null $section The section to add the header to
778 * @return HTMLForm $this for chaining calls (since 1.20)
780 public function setHeaderText( $msg, $section = null ) {
781 if ( $section === null ) {
782 $this->mHeader
= $msg;
784 $this->mSectionHeaders
[$section] = $msg;
793 * @param string|null $section The section to get the header text for
795 * @return string HTML
797 public function getHeaderText( $section = null ) {
798 if ( $section === null ) {
799 return $this->mHeader
;
801 return isset( $this->mSectionHeaders
[$section] ) ?
$this->mSectionHeaders
[$section] : '';
806 * Add footer text, inside the form.
808 * @param string $msg Complete text of message to display
809 * @param string|null $section The section to add the footer text to
811 * @return HTMLForm $this for chaining calls (since 1.20)
813 public function addFooterText( $msg, $section = null ) {
814 if ( $section === null ) {
815 $this->mFooter
.= $msg;
817 if ( !isset( $this->mSectionFooters
[$section] ) ) {
818 $this->mSectionFooters
[$section] = '';
820 $this->mSectionFooters
[$section] .= $msg;
827 * Set footer text, inside the form.
830 * @param string $msg Complete text of message to display
831 * @param string|null $section The section to add the footer text to
833 * @return HTMLForm $this for chaining calls (since 1.20)
835 public function setFooterText( $msg, $section = null ) {
836 if ( $section === null ) {
837 $this->mFooter
= $msg;
839 $this->mSectionFooters
[$section] = $msg;
848 * @param string|null $section The section to get the footer text for
852 public function getFooterText( $section = null ) {
853 if ( $section === null ) {
854 return $this->mFooter
;
856 return isset( $this->mSectionFooters
[$section] ) ?
$this->mSectionFooters
[$section] : '';
861 * Add text to the end of the display.
863 * @param string $msg Complete text of message to display
865 * @return HTMLForm $this for chaining calls (since 1.20)
867 public function addPostText( $msg ) {
868 $this->mPost
.= $msg;
874 * Set text at the end of the display.
876 * @param string $msg Complete text of message to display
878 * @return HTMLForm $this for chaining calls (since 1.20)
880 public function setPostText( $msg ) {
887 * Add a hidden field to the output
889 * @param string $name Field name. This will be used exactly as entered
890 * @param string $value Field value
891 * @param array $attribs
893 * @return HTMLForm $this for chaining calls (since 1.20)
895 public function addHiddenField( $name, $value, array $attribs = [] ) {
896 $attribs +
= [ 'name' => $name ];
897 $this->mHiddenFields
[] = [ $value, $attribs ];
903 * Add an array of hidden fields to the output
907 * @param array $fields Associative array of fields to add;
908 * mapping names to their values
910 * @return HTMLForm $this for chaining calls
912 public function addHiddenFields( array $fields ) {
913 foreach ( $fields as $name => $value ) {
914 $this->mHiddenFields
[] = [ $value, [ 'name' => $name ] ];
921 * Add a button to the form
923 * @since 1.27 takes an array as shown. Earlier versions accepted
924 * 'name', 'value', 'id', and 'attribs' as separate parameters in that
926 * @note Custom labels ('label', 'label-message', 'label-raw') are not
927 * supported for IE6 and IE7 due to bugs in those browsers. If detected,
928 * they will be served buttons using 'value' as the button label.
929 * @param array $data Data to define the button:
930 * - name: (string) Button name.
931 * - value: (string) Button value.
932 * - label-message: (string, optional) Button label message key to use
933 * instead of 'value'. Overrides 'label' and 'label-raw'.
934 * - label: (string, optional) Button label text to use instead of
935 * 'value'. Overrides 'label-raw'.
936 * - label-raw: (string, optional) Button label HTML to use instead of
938 * - id: (string, optional) DOM id for the button.
939 * - attribs: (array, optional) Additional HTML attributes.
940 * - flags: (string|string[], optional) OOUI flags.
941 * - framed: (boolean=true, optional) OOUI framed attribute.
942 * @return HTMLForm $this for chaining calls (since 1.20)
944 public function addButton( $data ) {
945 if ( !is_array( $data ) ) {
946 $args = func_get_args();
947 if ( count( $args ) < 2 ||
count( $args ) > 4 ) {
948 throw new InvalidArgumentException(
949 'Incorrect number of arguments for deprecated calling style'
955 'id' => isset( $args[2] ) ?
$args[2] : null,
956 'attribs' => isset( $args[3] ) ?
$args[3] : null,
959 if ( !isset( $data['name'] ) ) {
960 throw new InvalidArgumentException( 'A name is required' );
962 if ( !isset( $data['value'] ) ) {
963 throw new InvalidArgumentException( 'A value is required' );
966 $this->mButtons
[] = $data +
[
977 * Set the salt for the edit token.
979 * Only useful when the method is "post".
982 * @param string|array $salt Salt to use
983 * @return HTMLForm $this For chaining calls
985 public function setTokenSalt( $salt ) {
986 $this->mTokenSalt
= $salt;
992 * Display the form (sending to the context's OutputPage object), with an
993 * appropriate error message or stack of messages, and any validation errors, etc.
995 * @attention You should call prepareForm() before calling this function.
996 * Moreover, when doing method chaining this should be the very last method
997 * call just after prepareForm().
999 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1001 * @return void Nothing, should be last call
1003 public function displayForm( $submitResult ) {
1004 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1008 * Returns the raw HTML generated by the form
1010 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1012 * @return string HTML
1014 public function getHTML( $submitResult ) {
1015 # For good measure (it is the default)
1016 $this->getOutput()->preventClickjacking();
1017 $this->getOutput()->addModules( 'mediawiki.htmlform' );
1018 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1021 . $this->getErrorsOrWarnings( $submitResult, 'error' )
1022 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1023 . $this->getHeaderText()
1025 . $this->getHiddenFields()
1026 . $this->getButtons()
1027 . $this->getFooterText();
1029 $html = $this->wrapForm( $html );
1031 return '' . $this->mPre
. $html . $this->mPost
;
1035 * Get HTML attributes for the `<form>` tag.
1038 protected function getFormAttributes() {
1039 # Use multipart/form-data
1040 $encType = $this->mUseMultipart
1041 ?
'multipart/form-data'
1042 : 'application/x-www-form-urlencoded';
1045 'action' => $this->getAction(),
1046 'method' => $this->getMethod(),
1047 'enctype' => $encType,
1050 $attribs['id'] = $this->mId
;
1052 if ( $this->mAutocomplete
) {
1053 $attribs['autocomplete'] = $this->mAutocomplete
;
1055 if ( $this->mName
) {
1056 $attribs['name'] = $this->mName
;
1062 * Wrap the form innards in an actual "<form>" element
1064 * @param string $html HTML contents to wrap.
1066 * @return string Wrapped HTML.
1068 public function wrapForm( $html ) {
1069 # Include a <fieldset> wrapper for style, if requested.
1070 if ( $this->mWrapperLegend
!== false ) {
1071 $legend = is_string( $this->mWrapperLegend
) ?
$this->mWrapperLegend
: false;
1072 $html = Xml
::fieldset( $legend, $html );
1075 return Html
::rawElement(
1077 $this->getFormAttributes() +
[ 'class' => 'visualClear' ],
1083 * Get the hidden fields that should go inside the form.
1084 * @return string HTML.
1086 public function getHiddenFields() {
1088 if ( $this->mFormIdentifier
!== null ) {
1089 $html .= Html
::hidden(
1091 $this->mFormIdentifier
1094 if ( $this->getMethod() === 'post' ) {
1095 $html .= Html
::hidden(
1097 $this->getUser()->getEditToken( $this->mTokenSalt
),
1098 [ 'id' => 'wpEditToken' ]
1100 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1103 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1104 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1105 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1108 foreach ( $this->mHiddenFields
as $data ) {
1109 list( $value, $attribs ) = $data;
1110 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
1117 * Get the submit and (potentially) reset buttons.
1118 * @return string HTML.
1120 public function getButtons() {
1122 $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1124 if ( $this->mShowSubmit
) {
1127 if ( isset( $this->mSubmitID
) ) {
1128 $attribs['id'] = $this->mSubmitID
;
1131 if ( isset( $this->mSubmitName
) ) {
1132 $attribs['name'] = $this->mSubmitName
;
1135 if ( isset( $this->mSubmitTooltip
) ) {
1136 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
1139 $attribs['class'] = [ 'mw-htmlform-submit' ];
1141 if ( $useMediaWikiUIEverywhere ) {
1142 foreach ( $this->mSubmitFlags
as $flag ) {
1143 $attribs['class'][] = 'mw-ui-' . $flag;
1145 $attribs['class'][] = 'mw-ui-button';
1148 $buttons .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1151 if ( $this->mShowReset
) {
1152 $buttons .= Html
::element(
1156 'value' => $this->msg( 'htmlform-reset' )->text(),
1157 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1162 if ( $this->mShowCancel
) {
1163 $target = $this->mCancelTarget ?
: Title
::newMainPage();
1164 if ( $target instanceof Title
) {
1165 $target = $target->getLocalURL();
1167 $buttons .= Html
::element(
1170 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1173 $this->msg( 'cancel' )->text()
1177 // IE<8 has bugs with <button>, so we'll need to avoid them.
1178 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
1180 foreach ( $this->mButtons
as $button ) {
1183 'name' => $button['name'],
1184 'value' => $button['value']
1187 if ( isset( $button['label-message'] ) ) {
1188 $label = $this->getMessage( $button['label-message'] )->parse();
1189 } elseif ( isset( $button['label'] ) ) {
1190 $label = htmlspecialchars( $button['label'] );
1191 } elseif ( isset( $button['label-raw'] ) ) {
1192 $label = $button['label-raw'];
1194 $label = htmlspecialchars( $button['value'] );
1197 if ( $button['attribs'] ) {
1198 $attrs +
= $button['attribs'];
1201 if ( isset( $button['id'] ) ) {
1202 $attrs['id'] = $button['id'];
1205 if ( $useMediaWikiUIEverywhere ) {
1206 $attrs['class'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : [];
1207 $attrs['class'][] = 'mw-ui-button';
1211 $buttons .= Html
::element( 'input', $attrs ) . "\n";
1213 $buttons .= Html
::rawElement( 'button', $attrs, $label ) . "\n";
1221 return Html
::rawElement( 'span',
1222 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1226 * Get the whole body of the form.
1229 public function getBody() {
1230 return $this->displaySection( $this->mFieldTree
, $this->mTableId
);
1234 * Format and display an error message stack.
1236 * @param string|array|Status $errors
1238 * @deprecated since 1.28, use getErrorsOrWarnings() instead
1242 public function getErrors( $errors ) {
1243 wfDeprecated( __METHOD__
);
1244 return $this->getErrorsOrWarnings( $errors, 'error' );
1248 * Returns a formatted list of errors or warnings from the given elements.
1250 * @param string|array|Status $elements The set of errors/warnings to process.
1251 * @param string $elementsType Should warnings or errors be returned. This is meant
1252 * for Status objects, all other valid types are always considered as errors.
1255 public function getErrorsOrWarnings( $elements, $elementsType ) {
1256 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1257 throw new DomainException( $elementsType . ' is not a valid type.' );
1259 $elementstr = false;
1260 if ( $elements instanceof Status
) {
1261 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1262 $status = $elementsType === 'error' ?
$errorStatus : $warningStatus;
1263 if ( $status->isGood() ) {
1266 $elementstr = $this->getOutput()->parse(
1267 $status->getWikiText()
1270 } elseif ( is_array( $elements ) && $elementsType === 'error' ) {
1271 $elementstr = $this->formatErrors( $elements );
1272 } elseif ( $elementsType === 'error' ) {
1273 $elementstr = $elements;
1277 ? Html
::rawElement( 'div', [ 'class' => $elementsType ], $elementstr )
1282 * Format a stack of error messages into a single HTML string
1284 * @param array $errors Array of message keys/values
1286 * @return string HTML, a "<ul>" list of errors
1288 public function formatErrors( $errors ) {
1291 foreach ( $errors as $error ) {
1292 $errorstr .= Html
::rawElement(
1295 $this->getMessage( $error )->parse()
1299 $errorstr = Html
::rawElement( 'ul', [], $errorstr );
1305 * Set the text for the submit button
1307 * @param string $t Plaintext
1309 * @return HTMLForm $this for chaining calls (since 1.20)
1311 public function setSubmitText( $t ) {
1312 $this->mSubmitText
= $t;
1318 * Identify that the submit button in the form has a destructive action
1321 * @return HTMLForm $this for chaining calls (since 1.28)
1323 public function setSubmitDestructive() {
1324 $this->mSubmitFlags
= [ 'destructive', 'primary' ];
1330 * Identify that the submit button in the form has a progressive action
1333 * @return HTMLForm $this for chaining calls (since 1.28)
1335 public function setSubmitProgressive() {
1336 $this->mSubmitFlags
= [ 'progressive', 'primary' ];
1342 * Set the text for the submit button to a message
1345 * @param string|Message $msg Message key or Message object
1347 * @return HTMLForm $this for chaining calls (since 1.20)
1349 public function setSubmitTextMsg( $msg ) {
1350 if ( !$msg instanceof Message
) {
1351 $msg = $this->msg( $msg );
1353 $this->setSubmitText( $msg->text() );
1359 * Get the text for the submit button, either customised or a default.
1362 public function getSubmitText() {
1363 return $this->mSubmitText ?
: $this->msg( 'htmlform-submit' )->text();
1367 * @param string $name Submit button name
1369 * @return HTMLForm $this for chaining calls (since 1.20)
1371 public function setSubmitName( $name ) {
1372 $this->mSubmitName
= $name;
1378 * @param string $name Tooltip for the submit button
1380 * @return HTMLForm $this for chaining calls (since 1.20)
1382 public function setSubmitTooltip( $name ) {
1383 $this->mSubmitTooltip
= $name;
1389 * Set the id for the submit button.
1393 * @todo FIXME: Integrity of $t is *not* validated
1394 * @return HTMLForm $this for chaining calls (since 1.20)
1396 public function setSubmitID( $t ) {
1397 $this->mSubmitID
= $t;
1403 * Set an internal identifier for this form. It will be submitted as a hidden form field, allowing
1404 * HTMLForm to determine whether the form was submitted (or merely viewed). Setting this serves
1407 * - If you use two or more forms on one page, it allows HTMLForm to identify which of the forms
1408 * was submitted, and not attempt to validate the other ones.
1409 * - If you use checkbox or multiselect fields inside a form using the GET method, it allows
1410 * HTMLForm to distinguish between the initial page view and a form submission with all
1411 * checkboxes or select options unchecked.
1414 * @param string $ident
1417 public function setFormIdentifier( $ident ) {
1418 $this->mFormIdentifier
= $ident;
1424 * Stop a default submit button being shown for this form. This implies that an
1425 * alternate submit method must be provided manually.
1429 * @param bool $suppressSubmit Set to false to re-enable the button again
1431 * @return HTMLForm $this for chaining calls
1433 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1434 $this->mShowSubmit
= !$suppressSubmit;
1440 * Show a cancel button (or prevent it). The button is not shown by default.
1442 * @return HTMLForm $this for chaining calls
1445 public function showCancel( $show = true ) {
1446 $this->mShowCancel
= $show;
1451 * Sets the target where the user is redirected to after clicking cancel.
1452 * @param Title|string $target Target as a Title object or an URL
1453 * @return HTMLForm $this for chaining calls
1456 public function setCancelTarget( $target ) {
1457 $this->mCancelTarget
= $target;
1462 * Set the id of the \<table\> or outermost \<div\> element.
1466 * @param string $id New value of the id attribute, or "" to remove
1468 * @return HTMLForm $this for chaining calls
1470 public function setTableId( $id ) {
1471 $this->mTableId
= $id;
1477 * @param string $id DOM id for the form
1479 * @return HTMLForm $this for chaining calls (since 1.20)
1481 public function setId( $id ) {
1488 * @param string $name 'name' attribute for the form
1489 * @return HTMLForm $this for chaining calls
1491 public function setName( $name ) {
1492 $this->mName
= $name;
1498 * Prompt the whole form to be wrapped in a "<fieldset>", with
1499 * this text as its "<legend>" element.
1501 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1502 * If true, a wrapper will be displayed, but no legend.
1503 * If a string, a wrapper will be displayed with that string as a legend.
1504 * The string will be escaped before being output (this doesn't support HTML).
1506 * @return HTMLForm $this for chaining calls (since 1.20)
1508 public function setWrapperLegend( $legend ) {
1509 $this->mWrapperLegend
= $legend;
1515 * Prompt the whole form to be wrapped in a "<fieldset>", with
1516 * this message as its "<legend>" element.
1519 * @param string|Message $msg Message key or Message object
1521 * @return HTMLForm $this for chaining calls (since 1.20)
1523 public function setWrapperLegendMsg( $msg ) {
1524 if ( !$msg instanceof Message
) {
1525 $msg = $this->msg( $msg );
1527 $this->setWrapperLegend( $msg->text() );
1533 * Set the prefix for various default messages
1534 * @todo Currently only used for the "<fieldset>" legend on forms
1535 * with multiple sections; should be used elsewhere?
1539 * @return HTMLForm $this for chaining calls (since 1.20)
1541 public function setMessagePrefix( $p ) {
1542 $this->mMessagePrefix
= $p;
1548 * Set the title for form submission
1550 * @param Title $t Title of page the form is on/should be posted to
1552 * @return HTMLForm $this for chaining calls (since 1.20)
1554 public function setTitle( $t ) {
1564 public function getTitle() {
1565 return $this->mTitle
=== false
1566 ?
$this->getContext()->getTitle()
1571 * Set the method used to submit the form
1573 * @param string $method
1575 * @return HTMLForm $this for chaining calls (since 1.20)
1577 public function setMethod( $method = 'post' ) {
1578 $this->mMethod
= strtolower( $method );
1584 * @return string Always lowercase
1586 public function getMethod() {
1587 return $this->mMethod
;
1591 * Wraps the given $section into an user-visible fieldset.
1593 * @param string $legend Legend text for the fieldset
1594 * @param string $section The section content in plain Html
1595 * @param array $attributes Additional attributes for the fieldset
1596 * @return string The fieldset's Html
1598 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
1599 return Xml
::fieldset( $legend, $section, $attributes ) . "\n";
1605 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
1607 * @param string $sectionName ID attribute of the "<table>" tag for this
1608 * section, ignored if empty.
1609 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
1610 * each subsection, ignored if empty.
1611 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
1612 * @throws LogicException When called on uninitialized field data, e.g. When
1613 * HTMLForm::displayForm was called without calling HTMLForm::prepareForm
1618 public function displaySection( $fields,
1620 $fieldsetIDPrefix = '',
1621 &$hasUserVisibleFields = false
1623 if ( $this->mFieldData
=== null ) {
1624 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1625 . 'You probably called displayForm() without calling prepareForm() first.' );
1628 $displayFormat = $this->getDisplayFormat();
1631 $subsectionHtml = '';
1634 // Conveniently, PHP method names are case-insensitive.
1635 // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1636 $getFieldHtmlMethod = $displayFormat === 'table' ?
'getTableRow' : ( 'get' . $displayFormat );
1638 foreach ( $fields as $key => $value ) {
1639 if ( $value instanceof HTMLFormField
) {
1640 $v = array_key_exists( $key, $this->mFieldData
)
1641 ?
$this->mFieldData
[$key]
1642 : $value->getDefault();
1644 $retval = $value->$getFieldHtmlMethod( $v );
1646 // check, if the form field should be added to
1648 if ( $value->hasVisibleOutput() ) {
1651 $labelValue = trim( $value->getLabel() );
1652 if ( $labelValue !== ' ' && $labelValue !== '' ) {
1656 $hasUserVisibleFields = true;
1658 } elseif ( is_array( $value ) ) {
1659 $subsectionHasVisibleFields = false;
1661 $this->displaySection( $value,
1663 "$fieldsetIDPrefix$key-",
1664 $subsectionHasVisibleFields );
1667 if ( $subsectionHasVisibleFields === true ) {
1668 // Display the section with various niceties.
1669 $hasUserVisibleFields = true;
1671 $legend = $this->getLegend( $key );
1673 $section = $this->getHeaderText( $key ) .
1675 $this->getFooterText( $key );
1678 if ( $fieldsetIDPrefix ) {
1679 $attributes['id'] = Sanitizer
::escapeId( "$fieldsetIDPrefix$key" );
1681 $subsectionHtml .= $this->wrapFieldSetSection( $legend, $section, $attributes );
1683 // Just return the inputs, nothing fancy.
1684 $subsectionHtml .= $section;
1689 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1691 if ( $subsectionHtml ) {
1692 if ( $this->mSubSectionBeforeFields
) {
1693 return $subsectionHtml . "\n" . $html;
1695 return $html . "\n" . $subsectionHtml;
1703 * Put a form section together from the individual fields' HTML, merging it and wrapping.
1704 * @param array $fieldsHtml
1705 * @param string $sectionName
1706 * @param bool $anyFieldHasLabel
1707 * @return string HTML
1709 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1710 $displayFormat = $this->getDisplayFormat();
1711 $html = implode( '', $fieldsHtml );
1713 if ( $displayFormat === 'raw' ) {
1719 if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1720 $classes[] = 'mw-htmlform-nolabel';
1724 'class' => implode( ' ', $classes ),
1727 if ( $sectionName ) {
1728 $attribs['id'] = Sanitizer
::escapeId( $sectionName );
1731 if ( $displayFormat === 'table' ) {
1732 return Html
::rawElement( 'table',
1734 Html
::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
1735 } elseif ( $displayFormat === 'inline' ) {
1736 return Html
::rawElement( 'span', $attribs, "\n$html\n" );
1738 return Html
::rawElement( 'div', $attribs, "\n$html\n" );
1743 * Construct the form fields from the Descriptor array
1745 public function loadData() {
1748 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1749 $request = $this->getRequest();
1750 if ( $field->skipLoadData( $request ) ) {
1752 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
1753 $fieldData[$fieldname] = $field->getDefault();
1755 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1760 foreach ( $fieldData as $name => &$value ) {
1761 $field = $this->mFlatFields
[$name];
1762 $value = $field->filter( $value, $this->mFlatFields
);
1765 $this->mFieldData
= $fieldData;
1769 * Stop a reset button being shown for this form
1771 * @param bool $suppressReset Set to false to re-enable the button again
1773 * @return HTMLForm $this for chaining calls (since 1.20)
1775 public function suppressReset( $suppressReset = true ) {
1776 $this->mShowReset
= !$suppressReset;
1782 * Overload this if you want to apply special filtration routines
1783 * to the form as a whole, after it's submitted but before it's
1786 * @param array $data
1790 public function filterDataForSubmit( $data ) {
1795 * Get a string to go in the "<legend>" of a section fieldset.
1796 * Override this if you want something more complicated.
1798 * @param string $key
1802 public function getLegend( $key ) {
1803 return $this->msg( "{$this->mMessagePrefix}-$key" )->text();
1807 * Set the value for the action attribute of the form.
1808 * When set to false (which is the default state), the set title is used.
1812 * @param string|bool $action
1814 * @return HTMLForm $this for chaining calls (since 1.20)
1816 public function setAction( $action ) {
1817 $this->mAction
= $action;
1823 * Get the value for the action attribute of the form.
1829 public function getAction() {
1830 // If an action is alredy provided, return it
1831 if ( $this->mAction
!== false ) {
1832 return $this->mAction
;
1835 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1836 // Check whether we are in GET mode and the ArticlePath contains a "?"
1837 // meaning that getLocalURL() would return something like "index.php?title=...".
1838 // As browser remove the query string before submitting GET forms,
1839 // it means that the title would be lost. In such case use wfScript() instead
1840 // and put title in an hidden field (see getHiddenFields()).
1841 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1845 return $this->getTitle()->getLocalURL();
1849 * Set the value for the autocomplete attribute of the form.
1850 * When set to false (which is the default state), the attribute get not set.
1854 * @param string|bool $autocomplete
1856 * @return HTMLForm $this for chaining calls
1858 public function setAutocomplete( $autocomplete ) {
1859 $this->mAutocomplete
= $autocomplete;
1865 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1866 * name + parameters array) into a Message.
1867 * @param mixed $value
1870 protected function getMessage( $value ) {
1871 return Message
::newFromSpecifier( $value )->setContext( $this );