Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / HTMLForm.php
blobdec8d43c1828b37dcc201992ce0bcb3713617cd6
1 <?php
3 /**
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
21 * @file
24 namespace MediaWiki\HTMLForm;
26 use DomainException;
27 use InvalidArgumentException;
28 use LogicException;
29 use MediaWiki\Context\ContextSource;
30 use MediaWiki\Context\IContextSource;
31 use MediaWiki\Debug\MWDebug;
32 use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
33 use MediaWiki\Html\Html;
34 use MediaWiki\HTMLForm\Field\HTMLApiField;
35 use MediaWiki\HTMLForm\Field\HTMLAutoCompleteSelectField;
36 use MediaWiki\HTMLForm\Field\HTMLCheckField;
37 use MediaWiki\HTMLForm\Field\HTMLCheckMatrix;
38 use MediaWiki\HTMLForm\Field\HTMLComboboxField;
39 use MediaWiki\HTMLForm\Field\HTMLDateTimeField;
40 use MediaWiki\HTMLForm\Field\HTMLEditTools;
41 use MediaWiki\HTMLForm\Field\HTMLExpiryField;
42 use MediaWiki\HTMLForm\Field\HTMLFileField;
43 use MediaWiki\HTMLForm\Field\HTMLFloatField;
44 use MediaWiki\HTMLForm\Field\HTMLFormFieldCloner;
45 use MediaWiki\HTMLForm\Field\HTMLHiddenField;
46 use MediaWiki\HTMLForm\Field\HTMLInfoField;
47 use MediaWiki\HTMLForm\Field\HTMLIntField;
48 use MediaWiki\HTMLForm\Field\HTMLMultiSelectField;
49 use MediaWiki\HTMLForm\Field\HTMLNamespacesMultiselectField;
50 use MediaWiki\HTMLForm\Field\HTMLRadioField;
51 use MediaWiki\HTMLForm\Field\HTMLSelectAndOtherField;
52 use MediaWiki\HTMLForm\Field\HTMLSelectField;
53 use MediaWiki\HTMLForm\Field\HTMLSelectLanguageField;
54 use MediaWiki\HTMLForm\Field\HTMLSelectLimitField;
55 use MediaWiki\HTMLForm\Field\HTMLSelectNamespace;
56 use MediaWiki\HTMLForm\Field\HTMLSelectNamespaceWithButton;
57 use MediaWiki\HTMLForm\Field\HTMLSelectOrOtherField;
58 use MediaWiki\HTMLForm\Field\HTMLSizeFilterField;
59 use MediaWiki\HTMLForm\Field\HTMLSubmitField;
60 use MediaWiki\HTMLForm\Field\HTMLTagFilter;
61 use MediaWiki\HTMLForm\Field\HTMLTagMultiselectField;
62 use MediaWiki\HTMLForm\Field\HTMLTextAreaField;
63 use MediaWiki\HTMLForm\Field\HTMLTextField;
64 use MediaWiki\HTMLForm\Field\HTMLTextFieldWithButton;
65 use MediaWiki\HTMLForm\Field\HTMLTimezoneField;
66 use MediaWiki\HTMLForm\Field\HTMLTitlesMultiselectField;
67 use MediaWiki\HTMLForm\Field\HTMLTitleTextField;
68 use MediaWiki\HTMLForm\Field\HTMLUsersMultiselectField;
69 use MediaWiki\HTMLForm\Field\HTMLUserTextField;
70 use MediaWiki\Linker\Linker;
71 use MediaWiki\Linker\LinkTarget;
72 use MediaWiki\MainConfigNames;
73 use MediaWiki\Message\Message;
74 use MediaWiki\Page\PageReference;
75 use MediaWiki\Parser\Sanitizer;
76 use MediaWiki\Status\Status;
77 use MediaWiki\Title\Title;
78 use MediaWiki\Title\TitleValue;
79 use MediaWiki\Xml\Xml;
80 use StatusValue;
81 use Stringable;
82 use Wikimedia\Message\MessageParam;
83 use Wikimedia\Message\MessageSpecifier;
85 /**
86 * Object handling generic submission, CSRF protection, layout and
87 * other logic for UI forms in a reusable manner.
89 * In order to generate the form, the HTMLForm object takes an array
90 * structure detailing the form fields available. Each element of the
91 * array is a basic property-list, including the type of field, the
92 * label it is to be given in the form, callbacks for validation and
93 * 'filtering', and other pertinent information.
95 * Field types are implemented as subclasses of the generic HTMLFormField
96 * object, and typically implement at least getInputHTML, which generates
97 * the HTML for the input field to be placed in the table.
99 * You can find extensive documentation on the www.mediawiki.org wiki:
100 * - https://www.mediawiki.org/wiki/HTMLForm
101 * - https://www.mediawiki.org/wiki/HTMLForm/tutorial
103 * The constructor input is an associative array of $fieldname => $info,
104 * where $info is an Associative Array with any of the following:
106 * 'class' -- the subclass of HTMLFormField that will be used
107 * to create the object. *NOT* the CSS class!
108 * 'type' -- roughly translates into the <select> type attribute.
109 * if 'class' is not specified, this is used as a map
110 * through HTMLForm::$typeMappings to get the class name.
111 * 'default' -- default value when the form is displayed
112 * 'nodata' -- if set (to any value, which casts to true), the data
113 * for this field will not be loaded from the actual request. Instead,
114 * always the default data is set as the value of this field.
115 * 'id' -- HTML id attribute
116 * 'cssclass' -- CSS class
117 * 'csshelpclass' -- CSS class used to style help text
118 * 'dir' -- Direction of the element.
119 * 'options' -- associative array mapping raw HTML labels to values.
120 * Some field types support multi-level arrays.
121 * Overwrites 'options-message'.
122 * 'options-messages' -- associative array mapping message keys to values.
123 * Some field types support multi-level arrays.
124 * Overwrites 'options' and 'options-message'.
125 * 'options-messages-parse' -- Flag to parse the messages in 'options-messages'.
126 * 'options-message' -- message key or object to be parsed to extract the list of
127 * options (like 'ipbreason-dropdown').
128 * 'label-message' -- message key or object for a message to use as the label.
129 * can be an array of msg key and then parameters to
130 * the message.
131 * 'label' -- alternatively, a raw text message. Overridden by
132 * label-message
133 * 'help-raw' -- message text for a message to use as a help text.
134 * 'help-message' -- message key or object for a message to use as a help text.
135 * can be an array of msg key and then parameters to
136 * the message.
137 * Overwrites 'help-messages' and 'help-raw'.
138 * 'help-messages' -- array of message keys/objects. As above, each item can
139 * be an array of msg key and then parameters.
140 * Overwrites 'help-raw'.
141 * 'help-inline' -- Whether help text (defined using options above) will be shown
142 * inline after the input field, rather than in a popup.
143 * Defaults to true. Only used by OOUI form fields.
144 * 'notices' -- Array of plain text notices to display below the input field.
145 * Only used by OOUI form fields.
146 * 'required' -- passed through to the object, indicating that it
147 * is a required field.
148 * 'size' -- the length of text fields
149 * 'filter-callback' -- a function name to give you the chance to
150 * massage the inputted value before it's processed.
151 * @see HTMLFormField::filter()
152 * 'validation-callback' -- a function name to give you the chance
153 * to impose extra validation on the field input. The signature should be
154 * as documented in {@see HTMLFormField::$mValidationCallback}.
155 * @see HTMLFormField::validate()
156 * 'name' -- By default, the 'name' attribute of the input field
157 * is "wp{$fieldname}". If you want a different name
158 * (eg one without the "wp" prefix), specify it here and
159 * it will be used without modification.
160 * 'hide-if' -- expression given as an array stating when the field
161 * should be hidden. The first array value has to be the
162 * expression's logic operator. Supported expressions:
163 * 'NOT'
164 * [ 'NOT', array $expression ]
165 * To hide a field if a given expression is not true.
166 * '==='
167 * [ '===', string $fieldName, string $value ]
168 * To hide a field if another field identified by
169 * $field has the value $value.
170 * '!=='
171 * [ '!==', string $fieldName, string $value ]
172 * Same as [ 'NOT', [ '===', $fieldName, $value ]
173 * 'OR', 'AND', 'NOR', 'NAND'
174 * [ 'XXX', array $expression1, ..., array $expressionN ]
175 * To hide a field if one or more (OR), all (AND),
176 * neither (NOR) or not all (NAND) given expressions
177 * are evaluated as true.
178 * The expressions will be given to a JavaScript frontend
179 * module which will continually update the field's
180 * visibility.
181 * 'disable-if' -- expression given as an array stating when the field
182 * should be disabled. See 'hide-if' for supported expressions.
183 * The 'hide-if' logic would also disable fields, you don't need
184 * to set this attribute with the same condition manually.
185 * You can pass both 'disabled' and this attribute to omit extra
186 * check, but this would function only for not 'disabled' fields.
187 * 'section' -- A string name for the section of the form to which the field
188 * belongs. Subsections may be added using the separator '/', e.g.:
189 * 'section' => 'section1/subsection1'
190 * More levels may be added, e.g.:
191 * 'section' => 'section1/subsection2/subsubsection1'
192 * The message key for a section or subsection header is built from
193 * its name and the form's message prefix (if present).
195 * Since 1.20, you can chain mutators to ease the form generation:
196 * @par Example:
197 * @code
198 * $form = new HTMLForm( $someFields, $this->getContext() );
199 * $form->setMethod( 'get' )
200 * ->setWrapperLegendMsg( 'message-key' )
201 * ->prepareForm()
202 * ->displayForm( '' );
203 * @endcode
204 * Note that you will have prepareForm and displayForm at the end. Other
205 * method calls done after that would simply not be part of the form :(
207 * @stable to extend
209 class HTMLForm extends ContextSource {
210 use ProtectedHookAccessorTrait;
212 /** @var string[] A mapping of 'type' inputs onto standard HTMLFormField subclasses */
213 public static $typeMappings = [
214 'api' => HTMLApiField::class,
215 'text' => HTMLTextField::class,
216 'textwithbutton' => HTMLTextFieldWithButton::class,
217 'textarea' => HTMLTextAreaField::class,
218 'select' => HTMLSelectField::class,
219 'combobox' => HTMLComboboxField::class,
220 'radio' => HTMLRadioField::class,
221 'multiselect' => HTMLMultiSelectField::class,
222 'limitselect' => HTMLSelectLimitField::class,
223 'check' => HTMLCheckField::class,
224 'toggle' => HTMLCheckField::class,
225 'int' => HTMLIntField::class,
226 'file' => HTMLFileField::class,
227 'float' => HTMLFloatField::class,
228 'info' => HTMLInfoField::class,
229 'selectorother' => HTMLSelectOrOtherField::class,
230 'selectandother' => HTMLSelectAndOtherField::class,
231 'namespaceselect' => HTMLSelectNamespace::class,
232 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
233 'tagfilter' => HTMLTagFilter::class,
234 'sizefilter' => HTMLSizeFilterField::class,
235 'submit' => HTMLSubmitField::class,
236 'hidden' => HTMLHiddenField::class,
237 'edittools' => HTMLEditTools::class,
238 'checkmatrix' => HTMLCheckMatrix::class,
239 'cloner' => HTMLFormFieldCloner::class,
240 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
241 'language' => HTMLSelectLanguageField::class,
242 'date' => HTMLDateTimeField::class,
243 'time' => HTMLDateTimeField::class,
244 'datetime' => HTMLDateTimeField::class,
245 'expiry' => HTMLExpiryField::class,
246 'timezone' => HTMLTimezoneField::class,
247 // HTMLTextField will output the correct type="" attribute automagically.
248 // There are about four zillion other HTML5 input types, like range, but
249 // we don't use those at the moment, so no point in adding all of them.
250 'email' => HTMLTextField::class,
251 'password' => HTMLTextField::class,
252 'url' => HTMLTextField::class,
253 'title' => HTMLTitleTextField::class,
254 'user' => HTMLUserTextField::class,
255 'tagmultiselect' => HTMLTagMultiselectField::class,
256 'usersmultiselect' => HTMLUsersMultiselectField::class,
257 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
258 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
261 /** @var array */
262 public $mFieldData;
264 /** @var string */
265 protected $mMessagePrefix;
267 /** @var HTMLFormField[] */
268 protected $mFlatFields = [];
269 /** @var array */
270 protected $mFieldTree = [];
271 /** @var bool */
272 protected $mShowSubmit = true;
273 /** @var string[] */
274 protected $mSubmitFlags = [ 'primary', 'progressive' ];
275 /** @var bool */
276 protected $mShowCancel = false;
277 /** @var LinkTarget|string|null */
278 protected $mCancelTarget;
280 /** @var callable|null */
281 protected $mSubmitCallback;
283 * @var array[]
284 * @phan-var non-empty-array[]
286 protected $mValidationErrorMessage;
288 /** @var string */
289 protected $mPre = '';
290 /** @var string */
291 protected $mHeader = '';
292 /** @var string */
293 protected $mFooter = '';
294 /** @var string[] */
295 protected $mSectionHeaders = [];
296 /** @var string[] */
297 protected $mSectionFooters = [];
298 /** @var string */
299 protected $mPost = '';
300 /** @var string|null */
301 protected $mId;
302 /** @var string|null */
303 protected $mName;
304 /** @var string */
305 protected $mTableId = '';
307 /** @var string|null */
308 protected $mSubmitID;
309 /** @var string|null */
310 protected $mSubmitName;
311 /** @var string|null */
312 protected $mSubmitText;
313 /** @var string|null */
314 protected $mSubmitTooltip;
316 /** @var string|null */
317 protected $mFormIdentifier;
318 /** @var bool */
319 protected $mSingleForm = false;
321 /** @var Title|null */
322 protected $mTitle;
323 /** @var string */
324 protected $mMethod = 'post';
325 /** @var bool */
326 protected $mWasSubmitted = false;
329 * Form action URL. false means we will use the URL to set Title
330 * @since 1.19
331 * @var string|false
333 protected $mAction = false;
336 * Whether the form can be collapsed
337 * @since 1.34
338 * @var bool
340 protected $mCollapsible = false;
343 * Whether the form is collapsed by default
344 * @since 1.34
345 * @var bool
347 protected $mCollapsed = false;
350 * Form attribute autocomplete. A typical value is "off". null does not set the attribute
351 * @since 1.27
352 * @var string|null
354 protected $mAutocomplete = null;
356 /** @var bool */
357 protected $mUseMultipart = false;
359 * @var array[]
360 * @phan-var array<int,array{0:string,1:array}>
362 protected $mHiddenFields = [];
364 * @var array[]
365 * @phan-var array<array{name:string,value:string,label-message?:string|array<string|MessageParam>|MessageSpecifier,label?:string,label-raw?:string,id?:string,attribs?:array,flags?:string|string[],framed?:bool}>
367 protected $mButtons = [];
369 /** @var string|false */
370 protected $mWrapperLegend = false;
371 /** @var array */
372 protected $mWrapperAttributes = [];
375 * Salt for the edit token.
376 * @var string|array
378 protected $mTokenSalt = '';
381 * Additional information about form sections. Only supported by CodexHTMLForm.
383 * Array is keyed on section name. Options per section include:
384 * 'description' -- Description text placed below the section label.
385 * 'description-message' -- The same, but a message key.
386 * 'description-message-parse' -- Whether to parse the 'description-message'
387 * 'optional' -- Whether the section should be marked as optional.
389 * @since 1.42
390 * @var array[]
392 protected $mSections = [];
395 * If true, sections that contain both fields and subsections will
396 * render their subsections before their fields.
398 * Subclasses may set this to false to render subsections after fields
399 * instead.
400 * @var bool
402 protected $mSubSectionBeforeFields = true;
405 * Format in which to display form. For viable options,
406 * @see $availableDisplayFormats
407 * @var string
409 protected $displayFormat = 'table';
412 * Available formats in which to display the form
413 * @var array
415 protected $availableDisplayFormats = [
416 'table',
417 'div',
418 'raw',
419 'inline',
423 * Available formats in which to display the form
424 * @var array
426 protected $availableSubclassDisplayFormats = [
427 'vform',
428 'codex',
429 'ooui',
433 * Whether a hidden title field has been added to the form
434 * @var bool
436 private $hiddenTitleAddedToForm = false;
439 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
441 * @stable to call
443 * @param string $displayFormat
444 * @param array $descriptor Array of Field constructs, as described
445 * in the class documentation
446 * @param IContextSource $context Context used to fetch submitted form fields and
447 * generate localisation messages
448 * @param string $messagePrefix A prefix to go in front of default messages
449 * @return HTMLForm
451 public static function factory(
452 $displayFormat, $descriptor, IContextSource $context, $messagePrefix = ''
454 switch ( $displayFormat ) {
455 case 'codex':
456 return new CodexHTMLForm( $descriptor, $context, $messagePrefix );
457 case 'vform':
458 return new VFormHTMLForm( $descriptor, $context, $messagePrefix );
459 case 'ooui':
460 return new OOUIHTMLForm( $descriptor, $context, $messagePrefix );
461 default:
462 $form = new self( $descriptor, $context, $messagePrefix );
463 $form->setDisplayFormat( $displayFormat );
464 return $form;
469 * Build a new HTMLForm from an array of field attributes
471 * @stable to call
473 * @param array $descriptor Array of Field constructs, as described
474 * in the class documentation
475 * @param IContextSource $context Context used to fetch submitted form fields and
476 * generate localisation messages
477 * @param string $messagePrefix A prefix to go in front of default messages
479 public function __construct(
480 $descriptor, IContextSource $context, $messagePrefix = ''
482 $this->setContext( $context );
483 $this->mMessagePrefix = $messagePrefix;
484 $this->addFields( $descriptor );
488 * Add fields to the form
490 * @since 1.34
492 * @param array $descriptor Array of Field constructs, as described
493 * in the class documentation
494 * @return HTMLForm
496 public function addFields( $descriptor ) {
497 $loadedDescriptor = [];
499 foreach ( $descriptor as $fieldname => $info ) {
500 $section = $info['section'] ?? '';
502 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
503 $this->mUseMultipart = true;
506 $field = static::loadInputFromParameters( $fieldname, $info, $this );
508 $setSection =& $loadedDescriptor;
509 if ( $section ) {
510 foreach ( explode( '/', $section ) as $newName ) {
511 $setSection[$newName] ??= [];
512 $setSection =& $setSection[$newName];
516 $setSection[$fieldname] = $field;
517 $this->mFlatFields[$fieldname] = $field;
520 $this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor );
522 return $this;
526 * @param string $fieldname
527 * @return bool
529 public function hasField( $fieldname ) {
530 return isset( $this->mFlatFields[$fieldname] );
534 * @param string $fieldname
535 * @return HTMLFormField
536 * @throws DomainException on invalid field name
538 public function getField( $fieldname ) {
539 if ( !$this->hasField( $fieldname ) ) {
540 throw new DomainException( __METHOD__ . ': no field named ' . $fieldname );
542 return $this->mFlatFields[$fieldname];
546 * Set format in which to display the form
548 * @param string $format The name of the format to use, must be one of
549 * $this->availableDisplayFormats
551 * @since 1.20
552 * @return HTMLForm $this for chaining calls (since 1.20)
554 public function setDisplayFormat( $format ) {
555 if (
556 in_array( $format, $this->availableSubclassDisplayFormats, true ) ||
557 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true )
559 throw new LogicException( 'Cannot change display format after creation, ' .
560 'use HTMLForm::factory() instead' );
563 if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
564 throw new InvalidArgumentException( 'Display format must be one of ' .
565 print_r(
566 array_merge(
567 $this->availableDisplayFormats,
568 $this->availableSubclassDisplayFormats
570 true
571 ) );
574 $this->displayFormat = $format;
576 return $this;
580 * Getter for displayFormat
581 * @since 1.20
582 * @return string
584 public function getDisplayFormat() {
585 return $this->displayFormat;
589 * Get the HTMLFormField subclass for this descriptor.
591 * The descriptor can be passed either 'class' which is the name of
592 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
593 * This makes sure the 'class' is always set, and also is returned by
594 * this function for ease.
596 * @since 1.23
598 * @param string $fieldname Name of the field
599 * @param array &$descriptor Input Descriptor, as described
600 * in the class documentation
602 * @return string Name of a HTMLFormField subclass
604 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
605 if ( isset( $descriptor['class'] ) ) {
606 $class = $descriptor['class'];
607 } elseif ( isset( $descriptor['type'] ) ) {
608 $class = static::$typeMappings[$descriptor['type']];
609 $descriptor['class'] = $class;
610 } else {
611 $class = null;
614 if ( !$class ) {
615 throw new InvalidArgumentException( "Descriptor with no class for $fieldname: "
616 . print_r( $descriptor, true ) );
619 return $class;
623 * Initialise a new Object for the field
624 * @stable to override
626 * @param string $fieldname Name of the field
627 * @param array $descriptor Input Descriptor, as described
628 * in the class documentation
629 * @param HTMLForm|null $parent Parent instance of HTMLForm
631 * @warning Not passing (or passing null) for $parent is deprecated as of 1.40
632 * @return HTMLFormField Instance of a subclass of HTMLFormField
634 public static function loadInputFromParameters( $fieldname, $descriptor,
635 ?HTMLForm $parent = null
637 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
639 $descriptor['fieldname'] = $fieldname;
640 if ( $parent ) {
641 $descriptor['parent'] = $parent;
644 # @todo This will throw a fatal error whenever someone try to use
645 # 'class' to feed a CSS class instead of 'cssclass'. Would be
646 # great to avoid the fatal error and show a nice error.
647 return new $class( $descriptor );
651 * Prepare form for submission.
653 * @warning When doing method chaining, that should be the very last
654 * method call before displayForm().
656 * @return HTMLForm $this for chaining calls (since 1.20)
658 public function prepareForm() {
659 # Load data from the request.
660 if (
661 $this->mFormIdentifier === null ||
662 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier ||
663 ( $this->mSingleForm && $this->getMethod() === 'get' )
665 $this->loadFieldData();
666 } else {
667 $this->mFieldData = [];
670 return $this;
674 * Try submitting, with edit token check first
675 * @return bool|string|array|Status As documented for HTMLForm::trySubmit
677 public function tryAuthorizedSubmit() {
678 $result = false;
680 if ( $this->mFormIdentifier === null ) {
681 $identOkay = true;
682 } else {
683 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier;
686 $tokenOkay = false;
687 if ( $this->getMethod() !== 'post' ) {
688 $tokenOkay = true; // no session check needed
689 } elseif ( $this->getRequest()->wasPosted() ) {
690 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
691 if ( $this->getUser()->isRegistered() || $editToken !== null ) {
692 // Session tokens for logged-out users have no security value.
693 // However, if the user gave one, check it in order to give a nice
694 // "session expired" error instead of "permission denied" or such.
695 $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt );
696 } else {
697 $tokenOkay = true;
701 if ( $tokenOkay && $identOkay ) {
702 $this->mWasSubmitted = true;
703 $result = $this->trySubmit();
706 return $result;
710 * The here's-one-I-made-earlier option: do the submission if
711 * posted, or display the form with or without funky validation
712 * errors
713 * @stable to override
714 * @return bool|Status Whether submission was successful.
716 public function show() {
717 $this->prepareForm();
719 $result = $this->tryAuthorizedSubmit();
720 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
721 return $result;
724 $this->displayForm( $result );
726 return false;
730 * Same as self::show with the difference, that the form will be
731 * added to the output, no matter, if the validation was good or not.
732 * @return bool|Status Whether submission was successful.
734 public function showAlways() {
735 $this->prepareForm();
737 $result = $this->tryAuthorizedSubmit();
739 $this->displayForm( $result );
741 return $result;
745 * Validate all the fields, and call the submission callback
746 * function if everything is kosher.
747 * @stable to override
748 * @return bool|string|array|Status
749 * - Bool true or a good Status object indicates success,
750 * - Bool false indicates no submission was attempted,
751 * - Anything else indicates failure. The value may be a fatal Status
752 * object, an HTML string, or an array of arrays (message keys and
753 * params) or strings (message keys)
755 public function trySubmit() {
756 $valid = true;
757 $hoistedErrors = Status::newGood();
758 if ( $this->mValidationErrorMessage ) {
759 foreach ( $this->mValidationErrorMessage as $error ) {
760 $hoistedErrors->fatal( ...$error );
762 } else {
763 $hoistedErrors->fatal( 'htmlform-invalid-input' );
766 $this->mWasSubmitted = true;
768 # Check for cancelled submission
769 foreach ( $this->mFlatFields as $fieldname => $field ) {
770 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
771 continue;
773 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
774 $this->mWasSubmitted = false;
775 return false;
779 # Check for validation
780 $hasNonDefault = false;
781 foreach ( $this->mFlatFields as $fieldname => $field ) {
782 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
783 continue;
785 $hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault();
786 if ( $field->isDisabled( $this->mFieldData ) ) {
787 continue;
789 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
790 if ( $res !== true ) {
791 $valid = false;
792 if ( $res !== false && !$field->canDisplayErrors() ) {
793 if ( is_string( $res ) ) {
794 $hoistedErrors->fatal( 'rawmessage', $res );
795 } else {
796 $hoistedErrors->fatal( $res );
802 if ( !$valid ) {
803 // Treat as not submitted if got nothing from the user on GET forms.
804 if ( !$hasNonDefault && $this->getMethod() === 'get' &&
805 ( $this->mFormIdentifier === null ||
806 $this->getRequest()->getCheck( 'wpFormIdentifier' ) )
808 $this->mWasSubmitted = false;
809 return false;
811 return $hoistedErrors;
814 $callback = $this->mSubmitCallback;
815 if ( !is_callable( $callback ) ) {
816 throw new LogicException( 'HTMLForm: no submit callback provided. Use ' .
817 'setSubmitCallback() to set one.' );
820 $data = $this->filterDataForSubmit( $this->mFieldData );
822 $res = call_user_func( $callback, $data, $this );
823 if ( $res === false ) {
824 $this->mWasSubmitted = false;
825 } elseif ( $res instanceof StatusValue ) {
826 // DWIM - callbacks are not supposed to return a StatusValue but it's easy to mix up.
827 $res = Status::wrap( $res );
830 return $res;
834 * Test whether the form was considered to have been submitted or not, i.e.
835 * whether the last call to tryAuthorizedSubmit or trySubmit returned
836 * non-false.
838 * This will return false until HTMLForm::tryAuthorizedSubmit or
839 * HTMLForm::trySubmit is called.
841 * @since 1.23
842 * @return bool
844 public function wasSubmitted() {
845 return $this->mWasSubmitted;
849 * Set a callback to a function to do something with the form
850 * once it's been successfully validated.
852 * @param callable $cb The function will be passed the output from
853 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
854 * return as documented for HTMLForm::trySubmit
856 * @return HTMLForm $this for chaining calls (since 1.20)
858 public function setSubmitCallback( $cb ) {
859 $this->mSubmitCallback = $cb;
861 return $this;
865 * Set a message to display on a validation error.
867 * @param array[] $msg Array of valid inputs to wfMessage()
868 * (so each entry must itself be an array of arguments)
869 * @phan-param non-empty-array[] $msg
871 * @return HTMLForm $this for chaining calls (since 1.20)
873 public function setValidationErrorMessage( $msg ) {
874 $this->mValidationErrorMessage = $msg;
876 return $this;
880 * Set the introductory message, overwriting any existing message.
882 * @param string $msg Complete text of message to display
884 * @return HTMLForm $this for chaining calls (since 1.20)
885 * @deprecated since 1.38, use setPreHtml() instead, hard-deprecated since 1.43
887 public function setIntro( $msg ) {
888 wfDeprecated( __METHOD__, '1.38' );
889 return $this->setPreHtml( $msg );
893 * Set the introductory message HTML, overwriting any existing message.
895 * @param string $html Complete HTML of message to display
897 * @since 1.38
898 * @return $this for chaining calls
900 public function setPreHtml( $html ) {
901 $this->mPre = $html;
903 return $this;
907 * Add HTML to introductory message.
909 * @param string $html Complete HTML of message to display
911 * @since 1.38
912 * @return $this for chaining calls
914 public function addPreHtml( $html ) {
915 $this->mPre .= $html;
917 return $this;
921 * Get the introductory message HTML.
923 * @since 1.38
924 * @return string
926 public function getPreHtml() {
927 return $this->mPre;
931 * Set the introductory message HTML, overwriting any existing message.
933 * @param string $msg Complete HTML of message to display
935 * @return HTMLForm $this for chaining calls (since 1.20)
936 * @deprecated since 1.38, use setPreHtml() instead, hard-deprecated since 1.43
938 public function setPreText( $msg ) {
939 wfDeprecated( __METHOD__, '1.38' );
940 return $this->setPreHtml( $msg );
944 * Add HTML to introductory message.
946 * @param string $msg Complete HTML of message to display
948 * @return HTMLForm $this for chaining calls (since 1.20)
949 * @deprecated since 1.38, use addPreHtml() instead, hard-deprecated since 1.43
951 public function addPreText( $msg ) {
952 wfDeprecated( __METHOD__, '1.38' );
953 return $this->addPreHtml( $msg );
957 * Get the introductory message HTML.
959 * @since 1.32
960 * @return string
961 * @deprecated since 1.38, use getPreHtml() instead, hard-deprecated since 1.43
963 public function getPreText() {
964 wfDeprecated( __METHOD__, '1.38' );
965 return $this->getPreHtml();
969 * Add HTML to the header, inside the form.
971 * @param string $html Additional HTML to display in header
972 * @param string|null $section The section to add the header to
974 * @since 1.38
975 * @return $this for chaining calls
977 public function addHeaderHtml( $html, $section = null ) {
978 if ( $section === null ) {
979 $this->mHeader .= $html;
980 } else {
981 $this->mSectionHeaders[$section] ??= '';
982 $this->mSectionHeaders[$section] .= $html;
985 return $this;
989 * Set header HTML, inside the form.
991 * @param string $html Complete HTML of header to display
992 * @param string|null $section The section to add the header to
994 * @since 1.38
995 * @return $this for chaining calls
997 public function setHeaderHtml( $html, $section = null ) {
998 if ( $section === null ) {
999 $this->mHeader = $html;
1000 } else {
1001 $this->mSectionHeaders[$section] = $html;
1004 return $this;
1008 * Get header HTML.
1009 * @stable to override
1011 * @param string|null $section The section to get the header text for
1012 * @since 1.38
1013 * @return string HTML
1015 public function getHeaderHtml( $section = null ) {
1016 return $section ? $this->mSectionHeaders[$section] ?? '' : $this->mHeader;
1020 * Add HTML to the header, inside the form.
1022 * @param string $msg Additional HTML to display in header
1023 * @param string|null $section The section to add the header to
1025 * @return HTMLForm $this for chaining calls (since 1.20)
1026 * @deprecated since 1.38, use addHeaderHtml() instead, hard-deprecated since 1.43
1028 public function addHeaderText( $msg, $section = null ) {
1029 wfDeprecated( __METHOD__, '1.38' );
1030 return $this->addHeaderHtml( $msg, $section );
1034 * Set header text, inside the form.
1036 * @param string $msg Complete HTML of header to display
1037 * @param string|null $section The section to add the header to
1039 * @since 1.19
1040 * @return HTMLForm $this for chaining calls (since 1.20)
1041 * @deprecated since 1.38, use setHeaderHtml() instead, hard-deprecated since 1.43
1043 public function setHeaderText( $msg, $section = null ) {
1044 wfDeprecated( __METHOD__, '1.38' );
1045 return $this->setHeaderHtml( $msg, $section );
1049 * Get header text.
1050 * @stable to override
1052 * @param string|null $section The section to get the header text for
1053 * @since 1.26
1054 * @return string HTML
1055 * @deprecated since 1.38, use getHeaderHtml() instead, hard-deprecated since 1.43
1057 public function getHeaderText( $section = null ) {
1058 wfDeprecated( __METHOD__, '1.38' );
1059 return $this->getHeaderHtml( $section );
1063 * Add footer HTML, inside the form.
1065 * @param string $html Complete text of message to display
1066 * @param string|null $section The section to add the footer text to
1068 * @since 1.38
1069 * @return $this for chaining calls
1071 public function addFooterHtml( $html, $section = null ) {
1072 if ( $section === null ) {
1073 $this->mFooter .= $html;
1074 } else {
1075 $this->mSectionFooters[$section] ??= '';
1076 $this->mSectionFooters[$section] .= $html;
1079 return $this;
1083 * Set footer HTML, inside the form.
1085 * @param string $html Complete text of message to display
1086 * @param string|null $section The section to add the footer text to
1088 * @since 1.38
1089 * @return $this for chaining calls
1091 public function setFooterHtml( $html, $section = null ) {
1092 if ( $section === null ) {
1093 $this->mFooter = $html;
1094 } else {
1095 $this->mSectionFooters[$section] = $html;
1098 return $this;
1102 * Get footer HTML.
1104 * @param string|null $section The section to get the footer text for
1105 * @since 1.38
1106 * @return string
1108 public function getFooterHtml( $section = null ) {
1109 return $section ? $this->mSectionFooters[$section] ?? '' : $this->mFooter;
1113 * Add footer text, inside the form.
1115 * @param string $msg Complete text of message to display
1116 * @param string|null $section The section to add the footer text to
1118 * @return HTMLForm $this for chaining calls (since 1.20)
1119 * @deprecated since 1.38, use addFooterHtml() instead, hard-deprecated since 1.43
1121 public function addFooterText( $msg, $section = null ) {
1122 wfDeprecated( __METHOD__, '1.38' );
1123 return $this->addFooterHtml( $msg, $section );
1127 * Set footer text, inside the form.
1128 * @since 1.19
1130 * @param string $msg Complete text of message to display
1131 * @param string|null $section The section to add the footer text to
1133 * @return HTMLForm $this for chaining calls (since 1.20)
1134 * @deprecated since 1.38, use setFooterHtml() instead, hard-deprecated since 1.43
1136 public function setFooterText( $msg, $section = null ) {
1137 wfDeprecated( __METHOD__, '1.38' );
1138 return $this->setFooterHtml( $msg, $section );
1142 * Get footer text.
1144 * @param string|null $section The section to get the footer text for
1145 * @since 1.26
1146 * @return string
1147 * @deprecated since 1.38, use getFooterHtml() instead, hard-deprecated since 1.43
1149 public function getFooterText( $section = null ) {
1150 wfDeprecated( __METHOD__, '1.38' );
1151 return $this->getFooterHtml( $section );
1155 * Add HTML to the end of the display.
1157 * @param string $html Complete text of message to display
1159 * @since 1.38
1160 * @return $this for chaining calls
1162 public function addPostHtml( $html ) {
1163 $this->mPost .= $html;
1165 return $this;
1169 * Set HTML at the end of the display.
1171 * @param string $html Complete text of message to display
1173 * @since 1.38
1174 * @return $this for chaining calls
1176 public function setPostHtml( $html ) {
1177 $this->mPost = $html;
1179 return $this;
1183 * Get HTML at the end of the display.
1185 * @since 1.38
1186 * @return string HTML
1188 public function getPostHtml() {
1189 return $this->mPost;
1193 * Add text to the end of the display.
1195 * @param string $msg Complete text of message to display
1197 * @return HTMLForm $this for chaining calls (since 1.20)
1198 * @deprecated since 1.38, use addPostHtml() instead, hard-deprecated since 1.43
1200 public function addPostText( $msg ) {
1201 wfDeprecated( __METHOD__, '1.38' );
1202 return $this->addPostHtml( $msg );
1206 * Set text at the end of the display.
1208 * @param string $msg Complete text of message to display
1210 * @return HTMLForm $this for chaining calls (since 1.20)
1211 * @deprecated since 1.38, use setPostHtml() instead, hard-deprecated since 1.43
1213 public function setPostText( $msg ) {
1214 wfDeprecated( __METHOD__, '1.38' );
1215 return $this->setPostHtml( $msg );
1219 * Set an array of information about sections.
1221 * @since 1.42
1223 * @param array[] $sections Array of section information, keyed on section name.
1225 * @return HTMLForm $this for chaining calls
1227 public function setSections( $sections ) {
1228 if ( $this->getDisplayFormat() !== 'codex' ) {
1229 throw new \InvalidArgumentException(
1230 "Non-Codex HTMLForms do not support additional section information."
1234 $this->mSections = $sections;
1236 return $this;
1240 * Add a hidden field to the output
1241 * Array values are discarded for security reasons (per WebRequest::getVal)
1243 * @param string $name Field name. This will be used exactly as entered
1244 * @param mixed $value Field value
1245 * @param array $attribs
1247 * @return HTMLForm $this for chaining calls (since 1.20)
1249 public function addHiddenField( $name, $value, array $attribs = [] ) {
1250 if ( !is_array( $value ) ) {
1251 // Per WebRequest::getVal: Array values are discarded for security reasons.
1252 $attribs += [ 'name' => $name ];
1253 $this->mHiddenFields[] = [ $value, $attribs ];
1256 return $this;
1260 * Add an array of hidden fields to the output
1261 * Array values are discarded for security reasons (per WebRequest::getVal)
1263 * @since 1.22
1265 * @param array $fields Associative array of fields to add;
1266 * mapping names to their values
1268 * @return HTMLForm $this for chaining calls
1270 public function addHiddenFields( array $fields ) {
1271 foreach ( $fields as $name => $value ) {
1272 if ( is_array( $value ) ) {
1273 // Per WebRequest::getVal: Array values are discarded for security reasons.
1274 continue;
1276 $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
1279 return $this;
1283 * Add a button to the form
1285 * @since 1.27 takes an array as shown. Earlier versions accepted
1286 * 'name', 'value', 'id', and 'attribs' as separate parameters in that
1287 * order.
1288 * @param array $data Data to define the button:
1289 * - name: (string) Button name.
1290 * - value: (string) Button value.
1291 * - label-message: (string|array<string|array>|MessageSpecifier, optional) Button label
1292 * message key to use instead of 'value'. Overrides 'label' and 'label-raw'.
1293 * - label: (string, optional) Button label text to use instead of
1294 * 'value'. Overrides 'label-raw'.
1295 * - label-raw: (string, optional) Button label HTML to use instead of
1296 * 'value'.
1297 * - id: (string, optional) DOM id for the button.
1298 * - attribs: (array, optional) Additional HTML attributes.
1299 * - flags: (string|string[], optional) OOUI flags.
1300 * - framed: (boolean=true, optional) OOUI framed attribute.
1301 * @phpcs:ignore Generic.Files.LineLength
1302 * @phan-param array{name:string,value:string,label-message?:string|array<string|MessageParam>|MessageSpecifier,label?:string,label-raw?:string,id?:string,attribs?:array,flags?:string|string[],framed?:bool} $data
1303 * @return HTMLForm $this for chaining calls (since 1.20)
1305 public function addButton( $data ) {
1306 if ( !is_array( $data ) ) {
1307 $args = func_get_args();
1308 if ( count( $args ) < 2 || count( $args ) > 4 ) {
1309 throw new InvalidArgumentException(
1310 'Incorrect number of arguments for deprecated calling style'
1313 $data = [
1314 'name' => $args[0],
1315 'value' => $args[1],
1316 'id' => $args[2] ?? null,
1317 'attribs' => $args[3] ?? null,
1319 } else {
1320 if ( !isset( $data['name'] ) ) {
1321 throw new InvalidArgumentException( 'A name is required' );
1323 if ( !isset( $data['value'] ) ) {
1324 throw new InvalidArgumentException( 'A value is required' );
1327 $this->mButtons[] = $data + [
1328 'id' => null,
1329 'attribs' => null,
1330 'flags' => null,
1331 'framed' => true,
1334 return $this;
1338 * Set the salt for the edit token.
1340 * Only useful when the method is "post".
1342 * @since 1.24
1343 * @param string|array $salt Salt to use
1344 * @return HTMLForm $this For chaining calls
1346 public function setTokenSalt( $salt ) {
1347 $this->mTokenSalt = $salt;
1349 return $this;
1353 * Display the form (sending to the context's OutputPage object), with an
1354 * appropriate error message or stack of messages, and any validation errors, etc.
1356 * @warning You should call prepareForm() before calling this function.
1357 * Moreover, when doing method chaining this should be the very last method
1358 * call just after prepareForm().
1360 * @stable to override
1362 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1364 * @return void Nothing, should be last call
1366 public function displayForm( $submitResult ) {
1367 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1371 * Get a hidden field for the title of the page if necessary (empty string otherwise)
1372 * @return string
1374 private function getHiddenTitle(): string {
1375 if ( $this->hiddenTitleAddedToForm ) {
1376 return '';
1379 $html = '';
1380 if ( $this->getMethod() === 'post' ||
1381 $this->getAction() === $this->getConfig()->get( MainConfigNames::Script )
1383 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1385 $this->hiddenTitleAddedToForm = true;
1386 return $html;
1390 * Returns the raw HTML generated by the form
1392 * @stable to override
1394 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1396 * @return string HTML
1397 * @return-taint escaped
1399 public function getHTML( $submitResult ) {
1400 # For good measure (it is the default)
1401 $this->getOutput()->getMetadata()->setPreventClickjacking( true );
1402 $this->getOutput()->addModules( 'mediawiki.htmlform' );
1403 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1405 if ( $this->mCollapsible ) {
1406 // Preload jquery.makeCollapsible for mediawiki.htmlform
1407 $this->getOutput()->addModules( 'jquery.makeCollapsible' );
1410 $headerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getHeaderText', '1.38' )
1411 ? $this->getHeaderText()
1412 : $this->getHeaderHtml();
1413 $footerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getFooterText', '1.38' )
1414 ? $this->getFooterText()
1415 : $this->getFooterHtml();
1416 $html = $this->getErrorsOrWarnings( $submitResult, 'error' )
1417 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1418 . $headerHtml
1419 . $this->getHiddenTitle()
1420 . $this->getBody()
1421 . $this->getHiddenFields()
1422 . $this->getButtons()
1423 . $footerHtml;
1425 return $this->mPre . $this->wrapForm( $html ) . $this->mPost;
1429 * Enable collapsible mode, and set whether the form is collapsed by default.
1431 * @since 1.34
1432 * @param bool $collapsedByDefault Whether the form is collapsed by default (optional).
1433 * @return HTMLForm $this for chaining calls
1435 public function setCollapsibleOptions( $collapsedByDefault = false ) {
1436 $this->mCollapsible = true;
1437 $this->mCollapsed = $collapsedByDefault;
1438 return $this;
1442 * Get HTML attributes for the `<form>` tag.
1443 * @stable to override
1444 * @return array
1446 protected function getFormAttributes() {
1447 # Use multipart/form-data
1448 $encType = $this->mUseMultipart
1449 ? 'multipart/form-data'
1450 : 'application/x-www-form-urlencoded';
1451 # Attributes
1452 $attribs = [
1453 'class' => 'mw-htmlform',
1454 'action' => $this->getAction(),
1455 'method' => $this->getMethod(),
1456 'enctype' => $encType,
1458 if ( $this->mId ) {
1459 $attribs['id'] = $this->mId;
1461 if ( is_string( $this->mAutocomplete ) ) {
1462 $attribs['autocomplete'] = $this->mAutocomplete;
1464 if ( $this->mName ) {
1465 $attribs['name'] = $this->mName;
1467 if ( $this->needsJSForHtml5FormValidation() ) {
1468 $attribs['novalidate'] = true;
1470 return $attribs;
1474 * Wrap the form innards in an actual "<form>" element
1476 * @stable to override
1477 * @param string $html HTML contents to wrap.
1478 * @return string|\OOUI\Tag Wrapped HTML.
1480 public function wrapForm( $html ) {
1481 # Include a <fieldset> wrapper for style, if requested.
1482 if ( $this->mWrapperLegend !== false ) {
1483 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend : false;
1484 $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1487 return Html::rawElement(
1488 'form',
1489 $this->getFormAttributes(),
1490 $html
1495 * Get the hidden fields that should go inside the form.
1496 * @return string HTML.
1498 public function getHiddenFields() {
1499 $html = '';
1501 // add the title as a hidden file if it hasn't been added yet and if it is necessary
1502 // added for backward compatibility with the previous version of this public method
1503 $html .= $this->getHiddenTitle();
1505 if ( $this->mFormIdentifier !== null ) {
1506 $html .= Html::hidden(
1507 'wpFormIdentifier',
1508 $this->mFormIdentifier
1509 ) . "\n";
1511 if ( $this->getMethod() === 'post' ) {
1512 $html .= Html::hidden(
1513 'wpEditToken',
1514 $this->getUser()->getEditToken( $this->mTokenSalt ),
1515 [ 'id' => 'wpEditToken' ]
1516 ) . "\n";
1519 foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1520 $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1523 return $html;
1527 * Get the submit and (potentially) reset buttons.
1528 * @stable to override
1529 * @return string HTML.
1531 public function getButtons() {
1532 $buttons = '';
1534 if ( $this->mShowSubmit ) {
1535 $attribs = [];
1537 if ( $this->mSubmitID !== null ) {
1538 $attribs['id'] = $this->mSubmitID;
1541 if ( $this->mSubmitName !== null ) {
1542 $attribs['name'] = $this->mSubmitName;
1545 if ( $this->mSubmitTooltip !== null ) {
1546 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1549 $attribs['class'] = [ 'mw-htmlform-submit' ];
1551 $buttons .= Xml::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1554 if ( $this->mShowCancel ) {
1555 $target = $this->getCancelTargetURL();
1556 $buttons .= Html::element(
1557 'a',
1559 'href' => $target,
1561 $this->msg( 'cancel' )->text()
1562 ) . "\n";
1565 foreach ( $this->mButtons as $button ) {
1566 $attrs = [
1567 'type' => 'submit',
1568 'name' => $button['name'],
1569 'value' => $button['value']
1572 if ( isset( $button['label-message'] ) ) {
1573 $label = $this->getMessage( $button['label-message'] )->parse();
1574 } elseif ( isset( $button['label'] ) ) {
1575 $label = htmlspecialchars( $button['label'] );
1576 } elseif ( isset( $button['label-raw'] ) ) {
1577 $label = $button['label-raw'];
1578 } else {
1579 $label = htmlspecialchars( $button['value'] );
1582 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1583 if ( $button['attribs'] ) {
1584 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1585 $attrs += $button['attribs'];
1588 if ( isset( $button['id'] ) ) {
1589 $attrs['id'] = $button['id'];
1592 $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
1595 if ( !$buttons ) {
1596 return '';
1599 return Html::rawElement( 'span',
1600 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1604 * Get the whole body of the form.
1605 * @stable to override
1606 * @return string
1608 public function getBody() {
1609 return $this->displaySection( $this->mFieldTree, $this->mTableId );
1613 * Returns a formatted list of errors or warnings from the given elements.
1614 * @stable to override
1616 * @param string|array|Status $elements The set of errors/warnings to process.
1617 * @param string $elementsType Should warnings or errors be returned. This is meant
1618 * for Status objects, all other valid types are always considered as errors.
1619 * @return string
1621 public function getErrorsOrWarnings( $elements, $elementsType ) {
1622 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1623 throw new DomainException( $elementsType . ' is not a valid type.' );
1625 $elementstr = false;
1626 if ( $elements instanceof Status ) {
1627 [ $errorStatus, $warningStatus ] = $elements->splitByErrorType();
1628 $status = $elementsType === 'error' ? $errorStatus : $warningStatus;
1629 if ( $status->isGood() ) {
1630 $elementstr = '';
1631 } else {
1632 $elementstr = $status
1633 ->getMessage()
1634 ->setContext( $this )
1635 ->setInterfaceMessageFlag( true )
1636 ->parse();
1638 } elseif ( $elementsType === 'error' ) {
1639 if ( is_array( $elements ) ) {
1640 $elementstr = $this->formatErrors( $elements );
1641 } elseif ( $elements && $elements !== true ) {
1642 $elementstr = (string)$elements;
1646 if ( !$elementstr ) {
1647 return '';
1648 } elseif ( $elementsType === 'error' ) {
1649 return Html::errorBox( $elementstr );
1650 } else { // $elementsType can only be 'warning'
1651 return Html::warningBox( $elementstr );
1656 * Format a stack of error messages into a single HTML string
1658 * @param array $errors Array of message keys/values
1660 * @return string HTML, a "<ul>" list of errors
1662 public function formatErrors( $errors ) {
1663 $errorstr = '';
1665 foreach ( $errors as $error ) {
1666 $errorstr .= Html::rawElement(
1667 'li',
1669 $this->getMessage( $error )->parse()
1673 return Html::rawElement( 'ul', [], $errorstr );
1677 * Set the text for the submit button
1679 * @param string $t Plaintext
1681 * @return HTMLForm $this for chaining calls (since 1.20)
1683 public function setSubmitText( $t ) {
1684 $this->mSubmitText = $t;
1686 return $this;
1690 * Identify that the submit button in the form has a destructive action
1691 * @since 1.24
1693 * @return HTMLForm $this for chaining calls (since 1.28)
1695 public function setSubmitDestructive() {
1696 $this->mSubmitFlags = [ 'destructive', 'primary' ];
1698 return $this;
1702 * Set the text for the submit button to a message
1703 * @since 1.19
1705 * @param string|Message $msg Message key or Message object
1707 * @return HTMLForm $this for chaining calls (since 1.20)
1709 public function setSubmitTextMsg( $msg ) {
1710 if ( !$msg instanceof Message ) {
1711 $msg = $this->msg( $msg );
1713 $this->setSubmitText( $msg->text() );
1715 return $this;
1719 * Get the text for the submit button, either customised or a default.
1720 * @return string
1722 public function getSubmitText() {
1723 return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text();
1727 * @param string $name Submit button name
1729 * @return HTMLForm $this for chaining calls (since 1.20)
1731 public function setSubmitName( $name ) {
1732 $this->mSubmitName = $name;
1734 return $this;
1738 * @param string $name Tooltip for the submit button
1740 * @return HTMLForm $this for chaining calls (since 1.20)
1742 public function setSubmitTooltip( $name ) {
1743 $this->mSubmitTooltip = $name;
1745 return $this;
1749 * Set the id for the submit button.
1751 * @param string $t
1753 * @todo FIXME: Integrity of $t is *not* validated
1754 * @return HTMLForm $this for chaining calls (since 1.20)
1756 public function setSubmitID( $t ) {
1757 $this->mSubmitID = $t;
1759 return $this;
1763 * Set an internal identifier for this form. It will be submitted as a hidden form field, allowing
1764 * HTMLForm to determine whether the form was submitted (or merely viewed). Setting this serves
1765 * two purposes:
1767 * - If you use two or more forms on one page with the same submit target, it allows HTMLForm
1768 * to identify which of the forms was submitted, and not attempt to validate the other ones.
1769 * - If you use checkbox or multiselect fields inside a form using the GET method, it allows
1770 * HTMLForm to distinguish between the initial page view and a form submission with all
1771 * checkboxes or select options unchecked. Set the second parameter to true if you are sure
1772 * this is the only form on the page, which allows form fields to be prefilled with query
1773 * params.
1775 * @since 1.28
1776 * @param string $ident
1777 * @param bool $single Only work with GET form, see above. (since 1.41)
1778 * @return $this
1780 public function setFormIdentifier( string $ident, bool $single = false ) {
1781 $this->mFormIdentifier = $ident;
1782 $this->mSingleForm = $single;
1784 return $this;
1788 * Stop a default submit button being shown for this form. This implies that an
1789 * alternate submit method must be provided manually.
1791 * @since 1.22
1793 * @param bool $suppressSubmit Set to false to re-enable the button again
1795 * @return HTMLForm $this for chaining calls
1797 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1798 $this->mShowSubmit = !$suppressSubmit;
1800 return $this;
1804 * Show a cancel button (or prevent it). The button is not shown by default.
1805 * @param bool $show
1806 * @return HTMLForm $this for chaining calls
1807 * @since 1.27
1809 public function showCancel( $show = true ) {
1810 $this->mShowCancel = $show;
1811 return $this;
1815 * Sets the target where the user is redirected to after clicking cancel.
1816 * @param LinkTarget|PageReference|string $target Target as an object or an URL
1817 * @return HTMLForm $this for chaining calls
1818 * @since 1.27
1820 public function setCancelTarget( $target ) {
1821 if ( $target instanceof PageReference ) {
1822 $target = TitleValue::castPageToLinkTarget( $target );
1825 $this->mCancelTarget = $target;
1826 return $this;
1830 * @since 1.37
1831 * @return string
1833 protected function getCancelTargetURL() {
1834 if ( is_string( $this->mCancelTarget ) ) {
1835 return $this->mCancelTarget;
1836 } else {
1837 // TODO: use a service to get the local URL for a LinkTarget, see T282283
1838 $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1839 return $target->getLocalURL();
1844 * Set the id of the \<table\> or outermost \<div\> element.
1846 * @since 1.22
1848 * @param string $id New value of the id attribute, or "" to remove
1850 * @return HTMLForm $this for chaining calls
1852 public function setTableId( $id ) {
1853 $this->mTableId = $id;
1855 return $this;
1859 * @param string $id DOM id for the form
1861 * @return HTMLForm $this for chaining calls (since 1.20)
1863 public function setId( $id ) {
1864 $this->mId = $id;
1866 return $this;
1870 * @param string $name 'name' attribute for the form
1871 * @return HTMLForm $this for chaining calls
1873 public function setName( $name ) {
1874 $this->mName = $name;
1876 return $this;
1880 * Prompt the whole form to be wrapped in a "<fieldset>", with
1881 * this text as its "<legend>" element.
1883 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1884 * If true, a wrapper will be displayed, but no legend.
1885 * If a string, a wrapper will be displayed with that string as a legend.
1886 * The string will be escaped before being output (this doesn't support HTML).
1888 * @return HTMLForm $this for chaining calls (since 1.20)
1890 public function setWrapperLegend( $legend ) {
1891 $this->mWrapperLegend = $legend;
1893 return $this;
1897 * For internal use only. Use is discouraged, and should only be used where
1898 * support for gadgets/user scripts is warranted.
1899 * @param array $attributes
1900 * @internal
1901 * @return HTMLForm $this for chaining calls
1903 public function setWrapperAttributes( $attributes ) {
1904 $this->mWrapperAttributes = $attributes;
1906 return $this;
1910 * Prompt the whole form to be wrapped in a "<fieldset>", with
1911 * this message as its "<legend>" element.
1912 * @since 1.19
1914 * @param string|Message $msg Message key or Message object
1916 * @return HTMLForm $this for chaining calls (since 1.20)
1918 public function setWrapperLegendMsg( $msg ) {
1919 if ( !$msg instanceof Message ) {
1920 $msg = $this->msg( $msg );
1922 $this->setWrapperLegend( $msg->text() );
1924 return $this;
1928 * Set the prefix for various default messages
1929 * @todo Currently only used for the "<fieldset>" legend on forms
1930 * with multiple sections; should be used elsewhere?
1932 * @param string $p
1934 * @return HTMLForm $this for chaining calls (since 1.20)
1936 public function setMessagePrefix( $p ) {
1937 $this->mMessagePrefix = $p;
1939 return $this;
1943 * Set the title for form submission
1945 * @param PageReference $t The page the form is on/should be posted to
1947 * @return HTMLForm $this for chaining calls (since 1.20)
1949 public function setTitle( $t ) {
1950 // TODO: make mTitle a PageReference when we have a better way to get URLs, see T282283.
1951 $this->mTitle = Title::castFromPageReference( $t );
1953 return $this;
1957 * @return Title
1959 public function getTitle() {
1960 return $this->mTitle ?: $this->getContext()->getTitle();
1964 * Set the method used to submit the form
1966 * @param string $method
1968 * @return HTMLForm $this for chaining calls (since 1.20)
1970 public function setMethod( $method = 'post' ) {
1971 $this->mMethod = strtolower( $method );
1973 return $this;
1977 * @return string Always lowercase
1979 public function getMethod() {
1980 return $this->mMethod;
1984 * Wraps the given $section into a user-visible fieldset.
1985 * @stable to override
1987 * @param string $legend Legend text for the fieldset
1988 * @param string $section The section content in plain Html
1989 * @param array $attributes Additional attributes for the fieldset
1990 * @param bool $isRoot Section is at the root of the tree
1991 * @return string The fieldset's Html
1993 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1994 return Xml::fieldset( $legend, $section, $attributes ) . "\n";
1998 * @todo Document
1999 * @stable to override
2001 * Throws an exception when called on uninitialized field data, e.g. when
2002 * HTMLForm::displayForm was called without calling HTMLForm::prepareForm
2003 * first.
2005 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
2006 * objects).
2007 * @param string $sectionName ID attribute of the "<table>" tag for this
2008 * section, ignored if empty.
2009 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
2010 * each subsection, ignored if empty.
2011 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
2013 * @return string
2015 public function displaySection( $fields,
2016 $sectionName = '',
2017 $fieldsetIDPrefix = '',
2018 &$hasUserVisibleFields = false
2020 if ( $this->mFieldData === null ) {
2021 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
2022 . 'You probably called displayForm() without calling prepareForm() first.' );
2025 $html = [];
2026 $subsectionHtml = '';
2027 $hasLabel = false;
2029 foreach ( $fields as $key => $value ) {
2030 if ( $value instanceof HTMLFormField ) {
2031 $v = array_key_exists( $key, $this->mFieldData )
2032 ? $this->mFieldData[$key]
2033 : $value->getDefault();
2035 $retval = $this->formatField( $value, $v ?? '' );
2037 // check, if the form field should be added to
2038 // the output.
2039 if ( $value->hasVisibleOutput() ) {
2040 $html[] = $retval;
2042 $labelValue = trim( $value->getLabel() );
2043 if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
2044 $hasLabel = true;
2047 $hasUserVisibleFields = true;
2049 } elseif ( is_array( $value ) ) {
2050 $subsectionHasVisibleFields = false;
2051 $section =
2052 $this->displaySection( $value,
2053 "mw-htmlform-$key",
2054 "$fieldsetIDPrefix$key-",
2055 $subsectionHasVisibleFields );
2057 if ( $subsectionHasVisibleFields === true ) {
2058 // Display the section with various niceties.
2059 $hasUserVisibleFields = true;
2061 $legend = $this->getLegend( $key );
2063 $headerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getHeaderText', '1.38' )
2064 ? $this->getHeaderText( $key )
2065 : $this->getHeaderHtml( $key );
2066 $footerHtml = MWDebug::detectDeprecatedOverride( $this, __CLASS__, 'getFooterText', '1.38' )
2067 ? $this->getFooterText( $key )
2068 : $this->getFooterHtml( $key );
2069 $section = $headerHtml .
2070 $section .
2071 $footerHtml;
2073 $attributes = [];
2074 if ( $fieldsetIDPrefix ) {
2075 $attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
2077 $subsectionHtml .= $this->wrapFieldSetSection(
2078 $legend, $section, $attributes, $fields === $this->mFieldTree
2080 } else {
2081 // Just return the inputs, nothing fancy.
2082 $subsectionHtml .= $section;
2087 $html = $this->formatSection( $html, $sectionName, $hasLabel );
2089 if ( $subsectionHtml ) {
2090 if ( $this->mSubSectionBeforeFields ) {
2091 return $subsectionHtml . "\n" . $html;
2092 } else {
2093 return $html . "\n" . $subsectionHtml;
2095 } else {
2096 return $html;
2101 * Generate the HTML for an individual field in the current display format.
2102 * @since 1.41
2103 * @stable to override
2104 * @param HTMLFormField $field
2105 * @param mixed $value
2106 * @return string|Stringable HTML
2108 protected function formatField( HTMLFormField $field, $value ) {
2109 $displayFormat = $this->getDisplayFormat();
2110 switch ( $displayFormat ) {
2111 case 'table':
2112 return $field->getTableRow( $value );
2113 case 'div':
2114 return $field->getDiv( $value );
2115 case 'raw':
2116 return $field->getRaw( $value );
2117 case 'inline':
2118 return $field->getInline( $value );
2119 default:
2120 throw new LogicException( 'Not implemented' );
2125 * Put a form section together from the individual fields' HTML, merging it and wrapping.
2126 * @stable to override
2127 * @param array $fieldsHtml Array of outputs from formatField()
2128 * @param string $sectionName
2129 * @param bool $anyFieldHasLabel
2130 * @return string HTML
2132 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
2133 if ( !$fieldsHtml ) {
2134 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
2135 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
2136 return '';
2139 $displayFormat = $this->getDisplayFormat();
2140 $html = implode( '', $fieldsHtml );
2142 if ( $displayFormat === 'raw' ) {
2143 return $html;
2146 // Avoid strange spacing when no labels exist
2147 $attribs = $anyFieldHasLabel ? [] : [ 'class' => 'mw-htmlform-nolabel' ];
2149 if ( $sectionName ) {
2150 $attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
2153 if ( $displayFormat === 'table' ) {
2154 return Html::rawElement( 'table',
2155 $attribs,
2156 Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
2157 } elseif ( $displayFormat === 'inline' ) {
2158 return Html::rawElement( 'span', $attribs, "\n$html\n" );
2159 } else {
2160 return Html::rawElement( 'div', $attribs, "\n$html\n" );
2165 * @deprecated since 1.39, Use prepareForm() instead.
2167 public function loadData() {
2168 $this->prepareForm();
2172 * Load data of form fields from the request
2174 protected function loadFieldData() {
2175 $fieldData = [];
2176 $request = $this->getRequest();
2178 foreach ( $this->mFlatFields as $fieldname => $field ) {
2179 if ( $field->skipLoadData( $request ) ) {
2180 continue;
2182 if ( $field->mParams['disabled'] ?? false ) {
2183 $fieldData[$fieldname] = $field->getDefault();
2184 } else {
2185 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2189 // Reset to default for fields that are supposed to be disabled.
2190 // FIXME: Handle dependency chains, fields that a field checks on may need a reset too.
2191 foreach ( $fieldData as $name => &$value ) {
2192 $field = $this->mFlatFields[$name];
2193 if ( $field->isDisabled( $fieldData ) ) {
2194 $value = $field->getDefault();
2198 # Filter data.
2199 foreach ( $fieldData as $name => &$value ) {
2200 $field = $this->mFlatFields[$name];
2201 $value = $field->filter( $value, $fieldData );
2204 $this->mFieldData = $fieldData;
2208 * Overload this if you want to apply special filtration routines
2209 * to the form as a whole, after it's submitted but before it's
2210 * processed.
2211 * @stable to override
2213 * @param array $data
2215 * @return array
2217 public function filterDataForSubmit( $data ) {
2218 return $data;
2222 * Get a string to go in the "<legend>" of a section fieldset.
2223 * Override this if you want something more complicated.
2224 * @stable to override
2226 * @param string $key
2228 * @return string Plain text (not HTML-escaped)
2230 public function getLegend( $key ) {
2231 return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text();
2235 * Set the value for the action attribute of the form.
2236 * When set to false (which is the default state), the set title is used.
2238 * @since 1.19
2240 * @param string|bool $action
2242 * @return HTMLForm $this for chaining calls (since 1.20)
2244 public function setAction( $action ) {
2245 $this->mAction = $action;
2247 return $this;
2251 * Get the value for the action attribute of the form.
2253 * @since 1.22
2255 * @return string
2257 public function getAction() {
2258 // If an action is already provided, return it
2259 if ( $this->mAction !== false ) {
2260 return $this->mAction;
2263 $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2264 // Check whether we are in GET mode and the ArticlePath contains a "?"
2265 // meaning that getLocalURL() would return something like "index.php?title=...".
2266 // As browser remove the query string before submitting GET forms,
2267 // it means that the title would be lost. In such case use script path instead
2268 // and put title in a hidden field (see getHiddenFields()).
2269 if ( str_contains( $articlePath, '?' ) && $this->getMethod() === 'get' ) {
2270 return $this->getConfig()->get( MainConfigNames::Script );
2273 return $this->getTitle()->getLocalURL();
2277 * Set the value for the autocomplete attribute of the form. A typical value is "off".
2278 * When set to null (which is the default state), the attribute get not set.
2280 * @since 1.27
2282 * @param string|null $autocomplete
2284 * @return HTMLForm $this for chaining calls
2286 public function setAutocomplete( $autocomplete ) {
2287 $this->mAutocomplete = $autocomplete;
2289 return $this;
2293 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
2294 * name + parameters array) into a Message.
2295 * @param mixed $value
2296 * @return Message
2298 protected function getMessage( $value ) {
2299 return Message::newFromSpecifier( $value )->setContext( $this );
2303 * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
2304 * for the client-side HTML5 form validation to work correctly. If this function returns true, a
2305 * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
2306 * agent has JavaScript support, in htmlform.js.
2308 * @return bool
2309 * @since 1.29
2311 public function needsJSForHtml5FormValidation() {
2312 foreach ( $this->mFlatFields as $field ) {
2313 if ( $field->needsJSForHtml5FormValidation() ) {
2314 return true;
2317 return false;
2321 /** @deprecated class alias since 1.42 */
2322 class_alias( HTMLForm::class, 'HTMLForm' );