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 * 'nodata' -- if set (to any value, which casts to true), the data
52 * for this field will not be loaded from the actual request. Instead,
53 * always the default data is set as the value of this field.
54 * 'id' -- HTML id attribute
55 * 'cssclass' -- CSS class
56 * 'csshelpclass' -- CSS class used to style help text
57 * 'dir' -- Direction of the element.
58 * 'options' -- associative array mapping raw text labels to values.
59 * Some field types support multi-level arrays.
60 * Overwrites 'options-message'.
61 * 'options-messages' -- associative array mapping message keys to values.
62 * Some field types support multi-level arrays.
63 * Overwrites 'options' and 'options-message'.
64 * 'options-message' -- message key or object to be parsed to extract the list of
65 * options (like 'ipbreason-dropdown').
66 * 'label-message' -- message key or object for a message to use as the label.
67 * can be an array of msg key and then parameters to
69 * 'label' -- alternatively, a raw text message. Overridden by
71 * 'help' -- message text for a message to use as a help text.
72 * 'help-message' -- message key or object for a message to use as a help text.
73 * can be an array of msg key and then parameters to
75 * Overwrites 'help-messages' and 'help'.
76 * 'help-messages' -- array of message keys/objects. As above, each item can
77 * be an array of msg key and then parameters.
79 * 'help-inline' -- Whether help text (defined using options above) will be shown
80 * inline after the input field, rather than in a popup.
81 * Defaults to true. Only used by OOUI form fields.
82 * 'required' -- passed through to the object, indicating that it
83 * is a required field.
84 * 'size' -- the length of text fields
85 * 'filter-callback' -- a function name to give you the chance to
86 * massage the inputted value before it's processed.
87 * @see HTMLFormField::filter()
88 * 'validation-callback' -- a function name to give you the chance
89 * to impose extra validation on the field input.
90 * @see HTMLFormField::validate()
91 * 'name' -- By default, the 'name' attribute of the input field
92 * is "wp{$fieldname}". If you want a different name
93 * (eg one without the "wp" prefix), specify it here and
94 * it will be used without modification.
95 * 'hide-if' -- expression given as an array stating when the field
96 * should be hidden. The first array value has to be the
97 * expression's logic operator. Supported expressions:
99 * [ 'NOT', array $expression ]
100 * To hide a field if a given expression is not true.
102 * [ '===', string $fieldName, string $value ]
103 * To hide a field if another field identified by
104 * $field has the value $value.
106 * [ '!==', string $fieldName, string $value ]
107 * Same as [ 'NOT', [ '===', $fieldName, $value ]
108 * 'OR', 'AND', 'NOR', 'NAND'
109 * [ 'XXX', array $expression1, ..., array $expressionN ]
110 * To hide a field if one or more (OR), all (AND),
111 * neither (NOR) or not all (NAND) given expressions
112 * are evaluated as true.
113 * The expressions will be given to a JavaScript frontend
114 * module which will continually update the field's
117 * Since 1.20, you can chain mutators to ease the form generation:
120 * $form = new HTMLForm( $someFields );
121 * $form->setMethod( 'get' )
122 * ->setWrapperLegendMsg( 'message-key' )
124 * ->displayForm( '' );
126 * Note that you will have prepareForm and displayForm at the end. Other
127 * methods call done after that would simply not be part of the form :(
129 * @todo Document 'section' / 'subsection' stuff
131 class HTMLForm
extends ContextSource
{
132 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
133 public static $typeMappings = [
134 'api' => HTMLApiField
::class,
135 'text' => HTMLTextField
::class,
136 'textwithbutton' => HTMLTextFieldWithButton
::class,
137 'textarea' => HTMLTextAreaField
::class,
138 'select' => HTMLSelectField
::class,
139 'combobox' => HTMLComboboxField
::class,
140 'radio' => HTMLRadioField
::class,
141 'multiselect' => HTMLMultiSelectField
::class,
142 'limitselect' => HTMLSelectLimitField
::class,
143 'check' => HTMLCheckField
::class,
144 'toggle' => HTMLCheckField
::class,
145 'int' => HTMLIntField
::class,
146 'float' => HTMLFloatField
::class,
147 'info' => HTMLInfoField
::class,
148 'selectorother' => HTMLSelectOrOtherField
::class,
149 'selectandother' => HTMLSelectAndOtherField
::class,
150 'namespaceselect' => HTMLSelectNamespace
::class,
151 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton
::class,
152 'tagfilter' => HTMLTagFilter
::class,
153 'sizefilter' => HTMLSizeFilterField
::class,
154 'submit' => HTMLSubmitField
::class,
155 'hidden' => HTMLHiddenField
::class,
156 'edittools' => HTMLEditTools
::class,
157 'checkmatrix' => HTMLCheckMatrix
::class,
158 'cloner' => HTMLFormFieldCloner
::class,
159 'autocompleteselect' => HTMLAutoCompleteSelectField
::class,
160 'language' => HTMLSelectLanguageField
::class,
161 'date' => HTMLDateTimeField
::class,
162 'time' => HTMLDateTimeField
::class,
163 'datetime' => HTMLDateTimeField
::class,
164 'expiry' => HTMLExpiryField
::class,
165 // HTMLTextField will output the correct type="" attribute automagically.
166 // There are about four zillion other HTML5 input types, like range, but
167 // we don't use those at the moment, so no point in adding all of them.
168 'email' => HTMLTextField
::class,
169 'password' => HTMLTextField
::class,
170 'url' => HTMLTextField
::class,
171 'title' => HTMLTitleTextField
::class,
172 'user' => HTMLUserTextField
::class,
173 'usersmultiselect' => HTMLUsersMultiselectField
::class,
174 'titlesmultiselect' => HTMLTitlesMultiselectField
::class,
175 'namespacesmultiselect' => HTMLNamespacesMultiselectField
::class,
180 protected $mMessagePrefix;
182 /** @var HTMLFormField[] */
183 protected $mFlatFields = [];
184 protected $mFieldTree = [];
185 protected $mShowReset = false;
186 protected $mShowSubmit = true;
188 protected $mSubmitFlags = [ 'primary', 'progressive' ];
189 protected $mShowCancel = false;
190 protected $mCancelTarget;
192 protected $mSubmitCallback;
193 protected $mValidationErrorMessage;
195 protected $mPre = '';
196 protected $mHeader = '';
197 protected $mFooter = '';
198 protected $mSectionHeaders = [];
199 protected $mSectionFooters = [];
200 protected $mPost = '';
203 protected $mTableId = '';
205 protected $mSubmitID;
206 protected $mSubmitName;
207 protected $mSubmitText;
208 protected $mSubmitTooltip;
210 protected $mFormIdentifier;
212 protected $mMethod = 'post';
213 protected $mWasSubmitted = false;
216 * Form action URL. false means we will use the URL to set Title
220 protected $mAction = false;
223 * Whether the form can be collapsed
227 protected $mCollapsible = false;
230 * Whether the form is collapsed by default
234 protected $mCollapsed = false;
237 * Form attribute autocomplete. A typical value is "off". null does not set the attribute
241 protected $mAutocomplete = null;
243 protected $mUseMultipart = false;
244 protected $mHiddenFields = [];
245 protected $mButtons = [];
247 protected $mWrapperLegend = false;
248 protected $mWrapperAttributes = [];
251 * Salt for the edit token.
254 protected $mTokenSalt = '';
257 * If true, sections that contain both fields and subsections will
258 * render their subsections before their fields.
260 * Subclasses may set this to false to render subsections after fields
263 protected $mSubSectionBeforeFields = true;
266 * Format in which to display form. For viable options,
267 * @see $availableDisplayFormats
270 protected $displayFormat = 'table';
273 * Available formats in which to display the form
276 protected $availableDisplayFormats = [
284 * Available formats in which to display the form
287 protected $availableSubclassDisplayFormats = [
293 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
295 * @param string $displayFormat
296 * @param mixed $arguments,... Additional arguments to pass to the constructor.
297 * @suppress PhanCommentParamWithoutRealParam HHVM bug T228695#5450847
300 public static function factory( $displayFormat/*, $arguments...*/ ) {
301 $arguments = func_get_args();
302 array_shift( $arguments );
304 switch ( $displayFormat ) {
306 return new VFormHTMLForm( ...$arguments );
308 return new OOUIHTMLForm( ...$arguments );
310 $form = new self( ...$arguments );
311 $form->setDisplayFormat( $displayFormat );
317 * Build a new HTMLForm from an array of field attributes
319 * @param array $descriptor Array of Field constructs, as described
320 * in the class documentation
321 * @param IContextSource|null $context Available since 1.18, will become compulsory in 1.18.
322 * Obviates the need to call $form->setTitle()
323 * @param string $messagePrefix A prefix to go in front of default messages
325 public function __construct( $descriptor, /*IContextSource*/ $context = null,
328 if ( $context instanceof IContextSource
) {
329 $this->setContext( $context );
330 $this->mTitle
= false; // We don't need them to set a title
331 $this->mMessagePrefix
= $messagePrefix;
332 } elseif ( $context === null && $messagePrefix !== '' ) {
333 $this->mMessagePrefix
= $messagePrefix;
334 } elseif ( is_string( $context ) && $messagePrefix === '' ) {
336 // it's actually $messagePrefix
337 $this->mMessagePrefix
= $context;
340 // Evil hack for mobile :(
342 !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
343 && $this->displayFormat
=== 'table'
345 $this->displayFormat
= 'div';
348 $this->addFields( $descriptor );
352 * Add fields to the form
356 * @param array $descriptor Array of Field constructs, as described
357 * in the class documentation
360 public function addFields( $descriptor ) {
361 $loadedDescriptor = [];
363 foreach ( $descriptor as $fieldname => $info ) {
365 $section = $info['section'] ??
'';
367 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
368 $this->mUseMultipart
= true;
371 $field = static::loadInputFromParameters( $fieldname, $info, $this );
373 $setSection =& $loadedDescriptor;
375 foreach ( explode( '/', $section ) as $newName ) {
376 if ( !isset( $setSection[$newName] ) ) {
377 $setSection[$newName] = [];
380 $setSection =& $setSection[$newName];
384 $setSection[$fieldname] = $field;
385 $this->mFlatFields
[$fieldname] = $field;
388 $this->mFieldTree
= array_merge( $this->mFieldTree
, $loadedDescriptor );
394 * @param string $fieldname
397 public function hasField( $fieldname ) {
398 return isset( $this->mFlatFields
[$fieldname] );
402 * @param string $fieldname
403 * @return HTMLFormField
404 * @throws DomainException on invalid field name
406 public function getField( $fieldname ) {
407 if ( !$this->hasField( $fieldname ) ) {
408 throw new DomainException( __METHOD__
. ': no field named ' . $fieldname );
410 return $this->mFlatFields
[$fieldname];
414 * Set format in which to display the form
416 * @param string $format The name of the format to use, must be one of
417 * $this->availableDisplayFormats
419 * @throws MWException
421 * @return HTMLForm $this for chaining calls (since 1.20)
423 public function setDisplayFormat( $format ) {
425 in_array( $format, $this->availableSubclassDisplayFormats
, true ) ||
426 in_array( $this->displayFormat
, $this->availableSubclassDisplayFormats
, true )
428 throw new MWException( 'Cannot change display format after creation, ' .
429 'use HTMLForm::factory() instead' );
432 if ( !in_array( $format, $this->availableDisplayFormats
, true ) ) {
433 throw new MWException( 'Display format must be one of ' .
436 $this->availableDisplayFormats
,
437 $this->availableSubclassDisplayFormats
443 // Evil hack for mobile :(
444 if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
448 $this->displayFormat
= $format;
454 * Getter for displayFormat
458 public function getDisplayFormat() {
459 return $this->displayFormat
;
463 * Get the HTMLFormField subclass for this descriptor.
465 * The descriptor can be passed either 'class' which is the name of
466 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
467 * This makes sure the 'class' is always set, and also is returned by
468 * this function for ease.
472 * @param string $fieldname Name of the field
473 * @param array &$descriptor Input Descriptor, as described
474 * in the class documentation
476 * @throws MWException
477 * @return string Name of a HTMLFormField subclass
479 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
480 if ( isset( $descriptor['class'] ) ) {
481 $class = $descriptor['class'];
482 } elseif ( isset( $descriptor['type'] ) ) {
483 $class = static::$typeMappings[$descriptor['type']];
484 $descriptor['class'] = $class;
490 throw new MWException( "Descriptor with no class for $fieldname: "
491 . print_r( $descriptor, true ) );
498 * Initialise a new Object for the field
500 * @param string $fieldname Name of the field
501 * @param array $descriptor Input Descriptor, as described
502 * in the class documentation
503 * @param HTMLForm|null $parent Parent instance of HTMLForm
505 * @throws MWException
506 * @return HTMLFormField Instance of a subclass of HTMLFormField
508 public static function loadInputFromParameters( $fieldname, $descriptor,
509 HTMLForm
$parent = null
511 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
513 $descriptor['fieldname'] = $fieldname;
515 $descriptor['parent'] = $parent;
518 # @todo This will throw a fatal error whenever someone try to use
519 # 'class' to feed a CSS class instead of 'cssclass'. Would be
520 # great to avoid the fatal error and show a nice error.
521 return new $class( $descriptor );
525 * Prepare form for submission.
527 * @warning When doing method chaining, that should be the very last
528 * method call before displayForm().
530 * @throws MWException
531 * @return HTMLForm $this for chaining calls (since 1.20)
533 public function prepareForm() {
534 # Check if we have the info we need
535 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
536 throw new MWException( 'You must call setTitle() on an HTMLForm' );
539 # Load data from the request.
541 $this->mFormIdentifier
=== null ||
542 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
546 $this->mFieldData
= [];
553 * Try submitting, with edit token check first
554 * @return Status|bool
556 public function tryAuthorizedSubmit() {
559 if ( $this->mFormIdentifier
=== null ) {
562 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
;
566 if ( $this->getMethod() !== 'post' ) {
567 $tokenOkay = true; // no session check needed
568 } elseif ( $this->getRequest()->wasPosted() ) {
569 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
570 if ( $this->getUser()->isLoggedIn() ||
$editToken !== null ) {
571 // Session tokens for logged-out users have no security value.
572 // However, if the user gave one, check it in order to give a nice
573 // "session expired" error instead of "permission denied" or such.
574 $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt
);
580 if ( $tokenOkay && $identOkay ) {
581 $this->mWasSubmitted
= true;
582 $result = $this->trySubmit();
589 * The here's-one-I-made-earlier option: do the submission if
590 * posted, or display the form with or without funky validation
592 * @return bool|Status Whether submission was successful.
594 public function show() {
595 $this->prepareForm();
597 $result = $this->tryAuthorizedSubmit();
598 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
602 $this->displayForm( $result );
608 * Same as self::show with the difference, that the form will be
609 * added to the output, no matter, if the validation was good or not.
610 * @return bool|Status Whether submission was successful.
612 public function showAlways() {
613 $this->prepareForm();
615 $result = $this->tryAuthorizedSubmit();
617 $this->displayForm( $result );
623 * Validate all the fields, and call the submission callback
624 * function if everything is kosher.
625 * @throws MWException
626 * @return bool|string|array|Status
627 * - Bool true or a good Status object indicates success,
628 * - Bool false indicates no submission was attempted,
629 * - Anything else indicates failure. The value may be a fatal Status
630 * object, an HTML string, or an array of arrays (message keys and
631 * params) or strings (message keys)
633 public function trySubmit() {
635 $hoistedErrors = Status
::newGood();
636 if ( $this->mValidationErrorMessage
) {
637 foreach ( $this->mValidationErrorMessage
as $error ) {
638 $hoistedErrors->fatal( ...$error );
641 $hoistedErrors->fatal( 'htmlform-invalid-input' );
644 $this->mWasSubmitted
= true;
646 # Check for cancelled submission
647 foreach ( $this->mFlatFields
as $fieldname => $field ) {
648 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
651 if ( $field->cancelSubmit( $this->mFieldData
[$fieldname], $this->mFieldData
) ) {
652 $this->mWasSubmitted
= false;
657 # Check for validation
658 foreach ( $this->mFlatFields
as $fieldname => $field ) {
659 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
662 if ( $field->isHidden( $this->mFieldData
) ) {
665 $res = $field->validate( $this->mFieldData
[$fieldname], $this->mFieldData
);
666 if ( $res !== true ) {
668 if ( $res !== false && !$field->canDisplayErrors() ) {
669 if ( is_string( $res ) ) {
670 $hoistedErrors->fatal( 'rawmessage', $res );
672 $hoistedErrors->fatal( $res );
679 return $hoistedErrors;
682 $callback = $this->mSubmitCallback
;
683 if ( !is_callable( $callback ) ) {
684 throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
685 'setSubmitCallback() to set one.' );
688 $data = $this->filterDataForSubmit( $this->mFieldData
);
690 $res = call_user_func( $callback, $data, $this );
691 if ( $res === false ) {
692 $this->mWasSubmitted
= false;
699 * Test whether the form was considered to have been submitted or not, i.e.
700 * whether the last call to tryAuthorizedSubmit or trySubmit returned
703 * This will return false until HTMLForm::tryAuthorizedSubmit or
704 * HTMLForm::trySubmit is called.
709 public function wasSubmitted() {
710 return $this->mWasSubmitted
;
714 * Set a callback to a function to do something with the form
715 * once it's been successfully validated.
717 * @param callable $cb The function will be passed the output from
718 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
719 * return as documented for HTMLForm::trySubmit
721 * @return HTMLForm $this for chaining calls (since 1.20)
723 public function setSubmitCallback( $cb ) {
724 $this->mSubmitCallback
= $cb;
730 * Set a message to display on a validation error.
732 * @param array $msg Array of valid inputs to wfMessage()
733 * (so each entry must itself be an array of arguments)
735 * @return HTMLForm $this for chaining calls (since 1.20)
737 public function setValidationErrorMessage( $msg ) {
738 $this->mValidationErrorMessage
= $msg;
744 * Set the introductory message, overwriting any existing message.
746 * @param string $msg Complete text of message to display
748 * @return HTMLForm $this for chaining calls (since 1.20)
750 public function setIntro( $msg ) {
751 $this->setPreText( $msg );
757 * Set the introductory message HTML, overwriting any existing message.
760 * @param string $msg Complete HTML of message to display
762 * @return HTMLForm $this for chaining calls (since 1.20)
764 public function setPreText( $msg ) {
771 * Add HTML to introductory message.
773 * @param string $msg Complete HTML of message to display
775 * @return HTMLForm $this for chaining calls (since 1.20)
777 public function addPreText( $msg ) {
784 * Get the introductory message HTML.
790 public function getPreText() {
795 * Add HTML to the header, inside the form.
797 * @param string $msg Additional HTML to display in header
798 * @param string|null $section The section to add the header to
800 * @return HTMLForm $this for chaining calls (since 1.20)
802 public function addHeaderText( $msg, $section = null ) {
803 if ( $section === null ) {
804 $this->mHeader
.= $msg;
806 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
807 $this->mSectionHeaders
[$section] = '';
809 $this->mSectionHeaders
[$section] .= $msg;
816 * Set header text, inside the form.
819 * @param string $msg Complete HTML of header to display
820 * @param string|null $section The section to add the header to
822 * @return HTMLForm $this for chaining calls (since 1.20)
824 public function setHeaderText( $msg, $section = null ) {
825 if ( $section === null ) {
826 $this->mHeader
= $msg;
828 $this->mSectionHeaders
[$section] = $msg;
837 * @param string|null $section The section to get the header text for
839 * @return string HTML
841 public function getHeaderText( $section = null ) {
842 if ( $section === null ) {
843 return $this->mHeader
;
845 return $this->mSectionHeaders
[$section] ??
'';
850 * Add footer text, inside the form.
852 * @param string $msg Complete text of message to display
853 * @param string|null $section The section to add the footer text to
855 * @return HTMLForm $this for chaining calls (since 1.20)
857 public function addFooterText( $msg, $section = null ) {
858 if ( $section === null ) {
859 $this->mFooter
.= $msg;
861 if ( !isset( $this->mSectionFooters
[$section] ) ) {
862 $this->mSectionFooters
[$section] = '';
864 $this->mSectionFooters
[$section] .= $msg;
871 * Set footer text, inside the form.
874 * @param string $msg Complete text of message to display
875 * @param string|null $section The section to add the footer text to
877 * @return HTMLForm $this for chaining calls (since 1.20)
879 public function setFooterText( $msg, $section = null ) {
880 if ( $section === null ) {
881 $this->mFooter
= $msg;
883 $this->mSectionFooters
[$section] = $msg;
892 * @param string|null $section The section to get the footer text for
896 public function getFooterText( $section = null ) {
897 if ( $section === null ) {
898 return $this->mFooter
;
900 return $this->mSectionFooters
[$section] ??
'';
905 * Add text to the end of the display.
907 * @param string $msg Complete text of message to display
909 * @return HTMLForm $this for chaining calls (since 1.20)
911 public function addPostText( $msg ) {
912 $this->mPost
.= $msg;
918 * Set text at the end of the display.
920 * @param string $msg Complete text of message to display
922 * @return HTMLForm $this for chaining calls (since 1.20)
924 public function setPostText( $msg ) {
931 * Add a hidden field to the output
933 * @param string $name Field name. This will be used exactly as entered
934 * @param string $value Field value
935 * @param array $attribs
937 * @return HTMLForm $this for chaining calls (since 1.20)
939 public function addHiddenField( $name, $value, array $attribs = [] ) {
940 $attribs +
= [ 'name' => $name ];
941 $this->mHiddenFields
[] = [ $value, $attribs ];
947 * Add an array of hidden fields to the output
951 * @param array $fields Associative array of fields to add;
952 * mapping names to their values
954 * @return HTMLForm $this for chaining calls
956 public function addHiddenFields( array $fields ) {
957 foreach ( $fields as $name => $value ) {
958 $this->mHiddenFields
[] = [ $value, [ 'name' => $name ] ];
965 * Add a button to the form
967 * @since 1.27 takes an array as shown. Earlier versions accepted
968 * 'name', 'value', 'id', and 'attribs' as separate parameters in that
970 * @note Custom labels ('label', 'label-message', 'label-raw') are not
971 * supported for IE6 and IE7 due to bugs in those browsers. If detected,
972 * they will be served buttons using 'value' as the button label.
973 * @param array $data Data to define the button:
974 * - name: (string) Button name.
975 * - value: (string) Button value.
976 * - label-message: (string, optional) Button label message key to use
977 * instead of 'value'. Overrides 'label' and 'label-raw'.
978 * - label: (string, optional) Button label text to use instead of
979 * 'value'. Overrides 'label-raw'.
980 * - label-raw: (string, optional) Button label HTML to use instead of
982 * - id: (string, optional) DOM id for the button.
983 * - attribs: (array, optional) Additional HTML attributes.
984 * - flags: (string|string[], optional) OOUI flags.
985 * - framed: (boolean=true, optional) OOUI framed attribute.
986 * @return HTMLForm $this for chaining calls (since 1.20)
988 public function addButton( $data ) {
989 if ( !is_array( $data ) ) {
990 $args = func_get_args();
991 if ( count( $args ) < 2 ||
count( $args ) > 4 ) {
992 throw new InvalidArgumentException(
993 'Incorrect number of arguments for deprecated calling style'
999 'id' => $args[2] ??
null,
1000 'attribs' => $args[3] ??
null,
1003 if ( !isset( $data['name'] ) ) {
1004 throw new InvalidArgumentException( 'A name is required' );
1006 if ( !isset( $data['value'] ) ) {
1007 throw new InvalidArgumentException( 'A value is required' );
1010 $this->mButtons
[] = $data +
[
1021 * Set the salt for the edit token.
1023 * Only useful when the method is "post".
1026 * @param string|array $salt Salt to use
1027 * @return HTMLForm $this For chaining calls
1029 public function setTokenSalt( $salt ) {
1030 $this->mTokenSalt
= $salt;
1036 * Display the form (sending to the context's OutputPage object), with an
1037 * appropriate error message or stack of messages, and any validation errors, etc.
1039 * @warning You should call prepareForm() before calling this function.
1040 * Moreover, when doing method chaining this should be the very last method
1041 * call just after prepareForm().
1043 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1045 * @return void Nothing, should be last call
1047 public function displayForm( $submitResult ) {
1048 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1052 * Returns the raw HTML generated by the form
1054 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1056 * @return string HTML
1057 * @return-taint escaped
1059 public function getHTML( $submitResult ) {
1060 # For good measure (it is the default)
1061 $this->getOutput()->preventClickjacking();
1062 $this->getOutput()->addModules( 'mediawiki.htmlform' );
1063 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1066 . $this->getErrorsOrWarnings( $submitResult, 'error' )
1067 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1068 . $this->getHeaderText()
1070 . $this->getHiddenFields()
1071 . $this->getButtons()
1072 . $this->getFooterText();
1074 $html = $this->wrapForm( $html );
1076 return '' . $this->mPre
. $html . $this->mPost
;
1080 * Enable collapsible mode, and set whether the form is collapsed by default.
1083 * @param bool $collapsedByDefault Whether the form is collapsed by default (optional).
1084 * @return HTMLForm $this for chaining calls
1086 public function setCollapsibleOptions( $collapsedByDefault = false ) {
1087 $this->mCollapsible
= true;
1088 $this->mCollapsed
= $collapsedByDefault;
1093 * Get HTML attributes for the `<form>` tag.
1096 protected function getFormAttributes() {
1097 # Use multipart/form-data
1098 $encType = $this->mUseMultipart
1099 ?
'multipart/form-data'
1100 : 'application/x-www-form-urlencoded';
1103 'class' => 'mw-htmlform',
1104 'action' => $this->getAction(),
1105 'method' => $this->getMethod(),
1106 'enctype' => $encType,
1109 $attribs['id'] = $this->mId
;
1111 if ( is_string( $this->mAutocomplete
) ) {
1112 $attribs['autocomplete'] = $this->mAutocomplete
;
1114 if ( $this->mName
) {
1115 $attribs['name'] = $this->mName
;
1117 if ( $this->needsJSForHtml5FormValidation() ) {
1118 $attribs['novalidate'] = true;
1124 * Wrap the form innards in an actual "<form>" element
1126 * @param string $html HTML contents to wrap.
1128 * @return string Wrapped HTML.
1130 public function wrapForm( $html ) {
1131 # Include a <fieldset> wrapper for style, if requested.
1132 if ( $this->mWrapperLegend
!== false ) {
1133 $legend = is_string( $this->mWrapperLegend
) ?
$this->mWrapperLegend
: false;
1134 $html = Xml
::fieldset( $legend, $html, $this->mWrapperAttributes
);
1137 return Html
::rawElement(
1139 $this->getFormAttributes(),
1145 * Get the hidden fields that should go inside the form.
1146 * @return string HTML.
1148 public function getHiddenFields() {
1150 if ( $this->mFormIdentifier
!== null ) {
1151 $html .= Html
::hidden(
1153 $this->mFormIdentifier
1156 if ( $this->getMethod() === 'post' ) {
1157 $html .= Html
::hidden(
1159 $this->getUser()->getEditToken( $this->mTokenSalt
),
1160 [ 'id' => 'wpEditToken' ]
1162 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1165 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1166 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1167 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1170 foreach ( $this->mHiddenFields
as $data ) {
1171 list( $value, $attribs ) = $data;
1172 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
1179 * Get the submit and (potentially) reset buttons.
1180 * @return string HTML.
1182 public function getButtons() {
1184 $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1186 if ( $this->mShowSubmit
) {
1189 if ( isset( $this->mSubmitID
) ) {
1190 $attribs['id'] = $this->mSubmitID
;
1193 if ( isset( $this->mSubmitName
) ) {
1194 $attribs['name'] = $this->mSubmitName
;
1197 if ( isset( $this->mSubmitTooltip
) ) {
1198 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
1201 $attribs['class'] = [ 'mw-htmlform-submit' ];
1203 if ( $useMediaWikiUIEverywhere ) {
1204 foreach ( $this->mSubmitFlags
as $flag ) {
1205 $attribs['class'][] = 'mw-ui-' . $flag;
1207 $attribs['class'][] = 'mw-ui-button';
1210 $buttons .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1213 if ( $this->mShowReset
) {
1214 $buttons .= Html
::element(
1218 'value' => $this->msg( 'htmlform-reset' )->text(),
1219 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1224 if ( $this->mShowCancel
) {
1225 $target = $this->mCancelTarget ?
: Title
::newMainPage();
1226 if ( $target instanceof Title
) {
1227 $target = $target->getLocalURL();
1229 $buttons .= Html
::element(
1232 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1235 $this->msg( 'cancel' )->text()
1239 // IE<8 has bugs with <button>, so we'll need to avoid them.
1240 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
1242 foreach ( $this->mButtons
as $button ) {
1245 'name' => $button['name'],
1246 'value' => $button['value']
1249 if ( isset( $button['label-message'] ) ) {
1250 $label = $this->getMessage( $button['label-message'] )->parse();
1251 } elseif ( isset( $button['label'] ) ) {
1252 $label = htmlspecialchars( $button['label'] );
1253 } elseif ( isset( $button['label-raw'] ) ) {
1254 $label = $button['label-raw'];
1256 $label = htmlspecialchars( $button['value'] );
1259 if ( $button['attribs'] ) {
1260 $attrs +
= $button['attribs'];
1263 if ( isset( $button['id'] ) ) {
1264 $attrs['id'] = $button['id'];
1267 if ( $useMediaWikiUIEverywhere ) {
1268 $attrs['class'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : [];
1269 $attrs['class'][] = 'mw-ui-button';
1273 $buttons .= Html
::element( 'input', $attrs ) . "\n";
1275 $buttons .= Html
::rawElement( 'button', $attrs, $label ) . "\n";
1283 return Html
::rawElement( 'span',
1284 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1288 * Get the whole body of the form.
1291 public function getBody() {
1292 return $this->displaySection( $this->mFieldTree
, $this->mTableId
);
1296 * Returns a formatted list of errors or warnings from the given elements.
1298 * @param string|array|Status $elements The set of errors/warnings to process.
1299 * @param string $elementsType Should warnings or errors be returned. This is meant
1300 * for Status objects, all other valid types are always considered as errors.
1303 public function getErrorsOrWarnings( $elements, $elementsType ) {
1304 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1305 throw new DomainException( $elementsType . ' is not a valid type.' );
1307 $elementstr = false;
1308 if ( $elements instanceof Status
) {
1309 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1310 $status = $elementsType === 'error' ?
$errorStatus : $warningStatus;
1311 if ( $status->isGood() ) {
1314 $elementstr = $this->getOutput()->parseAsInterface(
1315 $status->getWikiText()
1318 } elseif ( is_array( $elements ) && $elementsType === 'error' ) {
1319 $elementstr = $this->formatErrors( $elements );
1320 } elseif ( $elementsType === 'error' ) {
1321 $elementstr = $elements;
1325 ? Html
::rawElement( 'div', [ 'class' => $elementsType ], $elementstr )
1330 * Format a stack of error messages into a single HTML string
1332 * @param array $errors Array of message keys/values
1334 * @return string HTML, a "<ul>" list of errors
1336 public function formatErrors( $errors ) {
1339 foreach ( $errors as $error ) {
1340 $errorstr .= Html
::rawElement(
1343 $this->getMessage( $error )->parse()
1347 $errorstr = Html
::rawElement( 'ul', [], $errorstr );
1353 * Set the text for the submit button
1355 * @param string $t Plaintext
1357 * @return HTMLForm $this for chaining calls (since 1.20)
1359 public function setSubmitText( $t ) {
1360 $this->mSubmitText
= $t;
1366 * Identify that the submit button in the form has a destructive action
1369 * @return HTMLForm $this for chaining calls (since 1.28)
1371 public function setSubmitDestructive() {
1372 $this->mSubmitFlags
= [ 'destructive', 'primary' ];
1378 * Set the text for the submit button to a message
1381 * @param string|Message $msg Message key or Message object
1383 * @return HTMLForm $this for chaining calls (since 1.20)
1385 public function setSubmitTextMsg( $msg ) {
1386 if ( !$msg instanceof Message
) {
1387 $msg = $this->msg( $msg );
1389 $this->setSubmitText( $msg->text() );
1395 * Get the text for the submit button, either customised or a default.
1398 public function getSubmitText() {
1399 return $this->mSubmitText ?
: $this->msg( 'htmlform-submit' )->text();
1403 * @param string $name Submit button name
1405 * @return HTMLForm $this for chaining calls (since 1.20)
1407 public function setSubmitName( $name ) {
1408 $this->mSubmitName
= $name;
1414 * @param string $name Tooltip for the submit button
1416 * @return HTMLForm $this for chaining calls (since 1.20)
1418 public function setSubmitTooltip( $name ) {
1419 $this->mSubmitTooltip
= $name;
1425 * Set the id for the submit button.
1429 * @todo FIXME: Integrity of $t is *not* validated
1430 * @return HTMLForm $this for chaining calls (since 1.20)
1432 public function setSubmitID( $t ) {
1433 $this->mSubmitID
= $t;
1439 * Set an internal identifier for this form. It will be submitted as a hidden form field, allowing
1440 * HTMLForm to determine whether the form was submitted (or merely viewed). Setting this serves
1443 * - If you use two or more forms on one page, it allows HTMLForm to identify which of the forms
1444 * was submitted, and not attempt to validate the other ones.
1445 * - If you use checkbox or multiselect fields inside a form using the GET method, it allows
1446 * HTMLForm to distinguish between the initial page view and a form submission with all
1447 * checkboxes or select options unchecked.
1450 * @param string $ident
1453 public function setFormIdentifier( $ident ) {
1454 $this->mFormIdentifier
= $ident;
1460 * Stop a default submit button being shown for this form. This implies that an
1461 * alternate submit method must be provided manually.
1465 * @param bool $suppressSubmit Set to false to re-enable the button again
1467 * @return HTMLForm $this for chaining calls
1469 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1470 $this->mShowSubmit
= !$suppressSubmit;
1476 * Show a cancel button (or prevent it). The button is not shown by default.
1478 * @return HTMLForm $this for chaining calls
1481 public function showCancel( $show = true ) {
1482 $this->mShowCancel
= $show;
1487 * Sets the target where the user is redirected to after clicking cancel.
1488 * @param Title|string $target Target as a Title object or an URL
1489 * @return HTMLForm $this for chaining calls
1492 public function setCancelTarget( $target ) {
1493 $this->mCancelTarget
= $target;
1498 * Set the id of the \<table\> or outermost \<div\> element.
1502 * @param string $id New value of the id attribute, or "" to remove
1504 * @return HTMLForm $this for chaining calls
1506 public function setTableId( $id ) {
1507 $this->mTableId
= $id;
1513 * @param string $id DOM id for the form
1515 * @return HTMLForm $this for chaining calls (since 1.20)
1517 public function setId( $id ) {
1524 * @param string $name 'name' attribute for the form
1525 * @return HTMLForm $this for chaining calls
1527 public function setName( $name ) {
1528 $this->mName
= $name;
1534 * Prompt the whole form to be wrapped in a "<fieldset>", with
1535 * this text as its "<legend>" element.
1537 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1538 * If true, a wrapper will be displayed, but no legend.
1539 * If a string, a wrapper will be displayed with that string as a legend.
1540 * The string will be escaped before being output (this doesn't support HTML).
1542 * @return HTMLForm $this for chaining calls (since 1.20)
1544 public function setWrapperLegend( $legend ) {
1545 $this->mWrapperLegend
= $legend;
1551 * For internal use only. Use is discouraged, and should only be used where
1552 * support for gadgets/user scripts is warranted.
1553 * @param array $attributes
1555 * @return HTMLForm $this for chaining calls
1557 public function setWrapperAttributes( $attributes ) {
1558 $this->mWrapperAttributes
= $attributes;
1564 * Prompt the whole form to be wrapped in a "<fieldset>", with
1565 * this message as its "<legend>" element.
1568 * @param string|Message $msg Message key or Message object
1570 * @return HTMLForm $this for chaining calls (since 1.20)
1572 public function setWrapperLegendMsg( $msg ) {
1573 if ( !$msg instanceof Message
) {
1574 $msg = $this->msg( $msg );
1576 $this->setWrapperLegend( $msg->text() );
1582 * Set the prefix for various default messages
1583 * @todo Currently only used for the "<fieldset>" legend on forms
1584 * with multiple sections; should be used elsewhere?
1588 * @return HTMLForm $this for chaining calls (since 1.20)
1590 public function setMessagePrefix( $p ) {
1591 $this->mMessagePrefix
= $p;
1597 * Set the title for form submission
1599 * @param Title $t Title of page the form is on/should be posted to
1601 * @return HTMLForm $this for chaining calls (since 1.20)
1603 public function setTitle( $t ) {
1613 public function getTitle() {
1614 return $this->mTitle
=== false
1615 ?
$this->getContext()->getTitle()
1620 * Set the method used to submit the form
1622 * @param string $method
1624 * @return HTMLForm $this for chaining calls (since 1.20)
1626 public function setMethod( $method = 'post' ) {
1627 $this->mMethod
= strtolower( $method );
1633 * @return string Always lowercase
1635 public function getMethod() {
1636 return $this->mMethod
;
1640 * Wraps the given $section into an user-visible fieldset.
1642 * @param string $legend Legend text for the fieldset
1643 * @param string $section The section content in plain Html
1644 * @param array $attributes Additional attributes for the fieldset
1645 * @param bool $isRoot Section is at the root of the tree
1646 * @return string The fieldset's Html
1648 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1649 return Xml
::fieldset( $legend, $section, $attributes ) . "\n";
1655 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
1657 * @param string $sectionName ID attribute of the "<table>" tag for this
1658 * section, ignored if empty.
1659 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
1660 * each subsection, ignored if empty.
1661 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
1662 * @throws LogicException When called on uninitialized field data, e.g. When
1663 * HTMLForm::displayForm was called without calling HTMLForm::prepareForm
1668 public function displaySection( $fields,
1670 $fieldsetIDPrefix = '',
1671 &$hasUserVisibleFields = false
1673 if ( $this->mFieldData
=== null ) {
1674 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1675 . 'You probably called displayForm() without calling prepareForm() first.' );
1678 $displayFormat = $this->getDisplayFormat();
1681 $subsectionHtml = '';
1684 // Conveniently, PHP method names are case-insensitive.
1685 // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1686 $getFieldHtmlMethod = $displayFormat === 'table' ?
'getTableRow' : ( 'get' . $displayFormat );
1688 foreach ( $fields as $key => $value ) {
1689 if ( $value instanceof HTMLFormField
) {
1690 $v = array_key_exists( $key, $this->mFieldData
)
1691 ?
$this->mFieldData
[$key]
1692 : $value->getDefault();
1694 $retval = $value->$getFieldHtmlMethod( $v );
1696 // check, if the form field should be added to
1698 if ( $value->hasVisibleOutput() ) {
1701 $labelValue = trim( $value->getLabel() );
1702 if ( $labelValue !== "\u{00A0}" && $labelValue !== ' ' && $labelValue !== '' ) {
1706 $hasUserVisibleFields = true;
1708 } elseif ( is_array( $value ) ) {
1709 $subsectionHasVisibleFields = false;
1711 $this->displaySection( $value,
1713 "$fieldsetIDPrefix$key-",
1714 $subsectionHasVisibleFields );
1717 if ( $subsectionHasVisibleFields === true ) {
1718 // Display the section with various niceties.
1719 $hasUserVisibleFields = true;
1721 $legend = $this->getLegend( $key );
1723 $section = $this->getHeaderText( $key ) .
1725 $this->getFooterText( $key );
1728 if ( $fieldsetIDPrefix ) {
1729 $attributes['id'] = Sanitizer
::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1731 $subsectionHtml .= $this->wrapFieldSetSection(
1732 $legend, $section, $attributes, $fields === $this->mFieldTree
1735 // Just return the inputs, nothing fancy.
1736 $subsectionHtml .= $section;
1741 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1743 if ( $subsectionHtml ) {
1744 if ( $this->mSubSectionBeforeFields
) {
1745 return $subsectionHtml . "\n" . $html;
1747 return $html . "\n" . $subsectionHtml;
1755 * Put a form section together from the individual fields' HTML, merging it and wrapping.
1756 * @param array $fieldsHtml
1757 * @param string $sectionName
1758 * @param bool $anyFieldHasLabel
1759 * @return string HTML
1761 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1762 if ( !$fieldsHtml ) {
1763 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
1764 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
1768 $displayFormat = $this->getDisplayFormat();
1769 $html = implode( '', $fieldsHtml );
1771 if ( $displayFormat === 'raw' ) {
1777 if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1778 $classes[] = 'mw-htmlform-nolabel';
1782 'class' => implode( ' ', $classes ),
1785 if ( $sectionName ) {
1786 $attribs['id'] = Sanitizer
::escapeIdForAttribute( $sectionName );
1789 if ( $displayFormat === 'table' ) {
1790 return Html
::rawElement( 'table',
1792 Html
::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
1793 } elseif ( $displayFormat === 'inline' ) {
1794 return Html
::rawElement( 'span', $attribs, "\n$html\n" );
1796 return Html
::rawElement( 'div', $attribs, "\n$html\n" );
1801 * Construct the form fields from the Descriptor array
1803 public function loadData() {
1806 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1807 $request = $this->getRequest();
1808 if ( $field->skipLoadData( $request ) ) {
1810 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
1811 $fieldData[$fieldname] = $field->getDefault();
1813 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1818 foreach ( $fieldData as $name => &$value ) {
1819 $field = $this->mFlatFields
[$name];
1820 $value = $field->filter( $value, $this->mFlatFields
);
1823 $this->mFieldData
= $fieldData;
1827 * Stop a reset button being shown for this form
1829 * @param bool $suppressReset Set to false to re-enable the button again
1831 * @return HTMLForm $this for chaining calls (since 1.20)
1833 public function suppressReset( $suppressReset = true ) {
1834 $this->mShowReset
= !$suppressReset;
1840 * Overload this if you want to apply special filtration routines
1841 * to the form as a whole, after it's submitted but before it's
1844 * @param array $data
1848 public function filterDataForSubmit( $data ) {
1853 * Get a string to go in the "<legend>" of a section fieldset.
1854 * Override this if you want something more complicated.
1856 * @param string $key
1858 * @return string Plain text (not HTML-escaped)
1860 public function getLegend( $key ) {
1861 return $this->msg( "{$this->mMessagePrefix}-$key" )->text();
1865 * Set the value for the action attribute of the form.
1866 * When set to false (which is the default state), the set title is used.
1870 * @param string|bool $action
1872 * @return HTMLForm $this for chaining calls (since 1.20)
1874 public function setAction( $action ) {
1875 $this->mAction
= $action;
1881 * Get the value for the action attribute of the form.
1887 public function getAction() {
1888 // If an action is alredy provided, return it
1889 if ( $this->mAction
!== false ) {
1890 return $this->mAction
;
1893 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1894 // Check whether we are in GET mode and the ArticlePath contains a "?"
1895 // meaning that getLocalURL() would return something like "index.php?title=...".
1896 // As browser remove the query string before submitting GET forms,
1897 // it means that the title would be lost. In such case use wfScript() instead
1898 // and put title in an hidden field (see getHiddenFields()).
1899 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1903 return $this->getTitle()->getLocalURL();
1907 * Set the value for the autocomplete attribute of the form. A typical value is "off".
1908 * When set to null (which is the default state), the attribute get not set.
1912 * @param string|null $autocomplete
1914 * @return HTMLForm $this for chaining calls
1916 public function setAutocomplete( $autocomplete ) {
1917 $this->mAutocomplete
= $autocomplete;
1923 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1924 * name + parameters array) into a Message.
1925 * @param mixed $value
1928 protected function getMessage( $value ) {
1929 return Message
::newFromSpecifier( $value )->setContext( $this );
1933 * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
1934 * for the client-side HTML5 form validation to work correctly. If this function returns true, a
1935 * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
1936 * agent has JavaScript support, in htmlform.js.
1941 public function needsJSForHtml5FormValidation() {
1942 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1943 if ( $field->needsJSForHtml5FormValidation() ) {