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
24 use Wikimedia\ObjectFactory
;
27 * Object handling generic submission, CSRF protection, layout and
28 * other logic for UI forms. in a reusable manner.
30 * In order to generate the form, the HTMLForm object takes an array
31 * structure detailing the form fields available. Each element of the
32 * array is a basic property-list, including the type of field, the
33 * label it is to be given in the form, callbacks for validation and
34 * 'filtering', and other pertinent information.
36 * Field types are implemented as subclasses of the generic HTMLFormField
37 * object, and typically implement at least getInputHTML, which generates
38 * the HTML for the input field to be placed in the table.
40 * You can find extensive documentation on the www.mediawiki.org wiki:
41 * - https://www.mediawiki.org/wiki/HTMLForm
42 * - https://www.mediawiki.org/wiki/HTMLForm/tutorial
44 * The constructor input is an associative array of $fieldname => $info,
45 * where $info is an Associative Array with any of the following:
47 * 'class' -- the subclass of HTMLFormField that will be used
48 * to create the object. *NOT* the CSS class!
49 * 'type' -- roughly translates into the <select> type attribute.
50 * if 'class' is not specified, this is used as a map
51 * through HTMLForm::$typeMappings to get the class name.
52 * 'default' -- default value when the form is displayed
53 * 'nodata' -- if set (to any value, which casts to true), the data
54 * for this field will not be loaded from the actual request. Instead,
55 * always the default data is set as the value of this field.
56 * 'id' -- HTML id attribute
57 * 'cssclass' -- CSS class
58 * 'csshelpclass' -- CSS class used to style help text
59 * 'dir' -- Direction of the element.
60 * 'options' -- associative array mapping labels to values.
61 * Some field types support multi-level arrays.
62 * 'options-messages' -- associative array mapping message keys to values.
63 * Some field types support multi-level arrays.
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 * 'notice' -- message text for a message to use as a notice in the field.
80 * Currently used by OOUI form fields only.
81 * 'notice-messages' -- array of message keys/objects to use for notice.
83 * 'notice-message' -- message key or object to use as a notice.
84 * 'required' -- passed through to the object, indicating that it
85 * is a required field.
86 * 'size' -- the length of text fields
87 * 'filter-callback' -- a function name to give you the chance to
88 * massage the inputted value before it's processed.
89 * @see HTMLFormField::filter()
90 * 'validation-callback' -- a function name to give you the chance
91 * to impose extra validation on the field input.
92 * @see HTMLFormField::validate()
93 * 'name' -- By default, the 'name' attribute of the input field
94 * is "wp{$fieldname}". If you want a different name
95 * (eg one without the "wp" prefix), specify it here and
96 * it will be used without modification.
97 * 'hide-if' -- expression given as an array stating when the field
98 * should be hidden. The first array value has to be the
99 * expression's logic operator. Supported expressions:
101 * [ 'NOT', array $expression ]
102 * To hide a field if a given expression is not true.
104 * [ '===', string $fieldName, string $value ]
105 * To hide a field if another field identified by
106 * $field has the value $value.
108 * [ '!==', string $fieldName, string $value ]
109 * Same as [ 'NOT', [ '===', $fieldName, $value ]
110 * 'OR', 'AND', 'NOR', 'NAND'
111 * [ 'XXX', array $expression1, ..., array $expressionN ]
112 * To hide a field if one or more (OR), all (AND),
113 * neither (NOR) or not all (NAND) given expressions
114 * are evaluated as true.
115 * The expressions will be given to a JavaScript frontend
116 * module which will continually update the field's
119 * Since 1.20, you can chain mutators to ease the form generation:
122 * $form = new HTMLForm( $someFields );
123 * $form->setMethod( 'get' )
124 * ->setWrapperLegendMsg( 'message-key' )
126 * ->displayForm( '' );
128 * Note that you will have prepareForm and displayForm at the end. Other
129 * methods call done after that would simply not be part of the form :(
131 * @todo Document 'section' / 'subsection' stuff
133 class HTMLForm
extends ContextSource
{
134 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
135 public static $typeMappings = [
136 'api' => HTMLApiField
::class,
137 'text' => HTMLTextField
::class,
138 'textwithbutton' => HTMLTextFieldWithButton
::class,
139 'textarea' => HTMLTextAreaField
::class,
140 'select' => HTMLSelectField
::class,
141 'combobox' => HTMLComboboxField
::class,
142 'radio' => HTMLRadioField
::class,
143 'multiselect' => HTMLMultiSelectField
::class,
144 'limitselect' => HTMLSelectLimitField
::class,
145 'check' => HTMLCheckField
::class,
146 'toggle' => HTMLCheckField
::class,
147 'int' => HTMLIntField
::class,
148 'float' => HTMLFloatField
::class,
149 'info' => HTMLInfoField
::class,
150 'selectorother' => HTMLSelectOrOtherField
::class,
151 'selectandother' => HTMLSelectAndOtherField
::class,
152 'namespaceselect' => HTMLSelectNamespace
::class,
153 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton
::class,
154 'tagfilter' => HTMLTagFilter
::class,
155 'sizefilter' => HTMLSizeFilterField
::class,
156 'submit' => HTMLSubmitField
::class,
157 'hidden' => HTMLHiddenField
::class,
158 'edittools' => HTMLEditTools
::class,
159 'checkmatrix' => HTMLCheckMatrix
::class,
160 'cloner' => HTMLFormFieldCloner
::class,
161 'autocompleteselect' => HTMLAutoCompleteSelectField
::class,
162 'date' => HTMLDateTimeField
::class,
163 'time' => HTMLDateTimeField
::class,
164 'datetime' => HTMLDateTimeField
::class,
165 'expiry' => HTMLExpiryField
::class,
166 // HTMLTextField will output the correct type="" attribute automagically.
167 // There are about four zillion other HTML5 input types, like range, but
168 // we don't use those at the moment, so no point in adding all of them.
169 'email' => HTMLTextField
::class,
170 'password' => HTMLTextField
::class,
171 'url' => HTMLTextField
::class,
172 'title' => HTMLTitleTextField
::class,
173 'user' => HTMLUserTextField
::class,
174 'usersmultiselect' => HTMLUsersMultiselectField
::class,
179 protected $mMessagePrefix;
181 /** @var HTMLFormField[] */
182 protected $mFlatFields;
184 protected $mFieldTree;
185 protected $mShowReset = false;
186 protected $mShowSubmit = true;
187 protected $mSubmitFlags = [ 'primary', 'progressive' ];
188 protected $mShowCancel = false;
189 protected $mCancelTarget;
191 protected $mSubmitCallback;
192 protected $mValidationErrorMessage;
194 protected $mPre = '';
195 protected $mHeader = '';
196 protected $mFooter = '';
197 protected $mSectionHeaders = [];
198 protected $mSectionFooters = [];
199 protected $mPost = '';
202 protected $mTableId = '';
204 protected $mSubmitID;
205 protected $mSubmitName;
206 protected $mSubmitText;
207 protected $mSubmitTooltip;
209 protected $mFormIdentifier;
211 protected $mMethod = 'post';
212 protected $mWasSubmitted = false;
215 * Form action URL. false means we will use the URL to set Title
219 protected $mAction = false;
222 * Form attribute autocomplete. A typical value is "off". null does not set the attribute
226 protected $mAutocomplete = null;
228 protected $mUseMultipart = false;
229 protected $mHiddenFields = [];
230 protected $mButtons = [];
232 protected $mWrapperLegend = false;
235 * Salt for the edit token.
238 protected $mTokenSalt = '';
241 * If true, sections that contain both fields and subsections will
242 * render their subsections before their fields.
244 * Subclasses may set this to false to render subsections after fields
247 protected $mSubSectionBeforeFields = true;
250 * Format in which to display form. For viable options,
251 * @see $availableDisplayFormats
254 protected $displayFormat = 'table';
257 * Available formats in which to display the form
260 protected $availableDisplayFormats = [
268 * Available formats in which to display the form
271 protected $availableSubclassDisplayFormats = [
277 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
279 * @param string $displayFormat
280 * @param mixed $arguments,... Additional arguments to pass to the constructor.
283 public static function factory( $displayFormat/*, $arguments...*/ ) {
284 $arguments = func_get_args();
285 array_shift( $arguments );
287 switch ( $displayFormat ) {
289 return ObjectFactory
::constructClassInstance( VFormHTMLForm
::class, $arguments );
291 return ObjectFactory
::constructClassInstance( OOUIHTMLForm
::class, $arguments );
293 /** @var HTMLForm $form */
294 $form = ObjectFactory
::constructClassInstance( self
::class, $arguments );
295 $form->setDisplayFormat( $displayFormat );
301 * Build a new HTMLForm from an array of field attributes
303 * @param array $descriptor Array of Field constructs, as described above
304 * @param IContextSource $context Available since 1.18, will become compulsory in 1.18.
305 * Obviates the need to call $form->setTitle()
306 * @param string $messagePrefix A prefix to go in front of default messages
308 public function __construct( $descriptor, /*IContextSource*/ $context = null,
311 if ( $context instanceof IContextSource
) {
312 $this->setContext( $context );
313 $this->mTitle
= false; // We don't need them to set a title
314 $this->mMessagePrefix
= $messagePrefix;
315 } elseif ( $context === null && $messagePrefix !== '' ) {
316 $this->mMessagePrefix
= $messagePrefix;
317 } elseif ( is_string( $context ) && $messagePrefix === '' ) {
319 // it's actually $messagePrefix
320 $this->mMessagePrefix
= $context;
323 // Evil hack for mobile :(
325 !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
326 && $this->displayFormat
=== 'table'
328 $this->displayFormat
= 'div';
331 // Expand out into a tree.
332 $loadedDescriptor = [];
333 $this->mFlatFields
= [];
335 foreach ( $descriptor as $fieldname => $info ) {
336 $section = $info['section'] ??
'';
338 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
339 $this->mUseMultipart
= true;
342 $field = static::loadInputFromParameters( $fieldname, $info, $this );
344 $setSection =& $loadedDescriptor;
346 $sectionParts = explode( '/', $section );
348 while ( count( $sectionParts ) ) {
349 $newName = array_shift( $sectionParts );
351 if ( !isset( $setSection[$newName] ) ) {
352 $setSection[$newName] = [];
355 $setSection =& $setSection[$newName];
359 $setSection[$fieldname] = $field;
360 $this->mFlatFields
[$fieldname] = $field;
363 $this->mFieldTree
= $loadedDescriptor;
367 * @param string $fieldname
370 public function hasField( $fieldname ) {
371 return isset( $this->mFlatFields
[$fieldname] );
375 * @param string $fieldname
376 * @return HTMLFormField
377 * @throws DomainException on invalid field name
379 public function getField( $fieldname ) {
380 if ( !$this->hasField( $fieldname ) ) {
381 throw new DomainException( __METHOD__
. ': no field named ' . $fieldname );
383 return $this->mFlatFields
[$fieldname];
387 * Set format in which to display the form
389 * @param string $format The name of the format to use, must be one of
390 * $this->availableDisplayFormats
392 * @throws MWException
394 * @return HTMLForm $this for chaining calls (since 1.20)
396 public function setDisplayFormat( $format ) {
398 in_array( $format, $this->availableSubclassDisplayFormats
, true ) ||
399 in_array( $this->displayFormat
, $this->availableSubclassDisplayFormats
, true )
401 throw new MWException( 'Cannot change display format after creation, ' .
402 'use HTMLForm::factory() instead' );
405 if ( !in_array( $format, $this->availableDisplayFormats
, true ) ) {
406 throw new MWException( 'Display format must be one of ' .
409 $this->availableDisplayFormats
,
410 $this->availableSubclassDisplayFormats
416 // Evil hack for mobile :(
417 if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
421 $this->displayFormat
= $format;
427 * Getter for displayFormat
431 public function getDisplayFormat() {
432 return $this->displayFormat
;
436 * Get the HTMLFormField subclass for this descriptor.
438 * The descriptor can be passed either 'class' which is the name of
439 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
440 * This makes sure the 'class' is always set, and also is returned by
441 * this function for ease.
445 * @param string $fieldname Name of the field
446 * @param array &$descriptor Input Descriptor, as described above
448 * @throws MWException
449 * @return string Name of a HTMLFormField subclass
451 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
452 if ( isset( $descriptor['class'] ) ) {
453 $class = $descriptor['class'];
454 } elseif ( isset( $descriptor['type'] ) ) {
455 $class = static::$typeMappings[$descriptor['type']];
456 $descriptor['class'] = $class;
462 throw new MWException( "Descriptor with no class for $fieldname: "
463 . print_r( $descriptor, true ) );
470 * Initialise a new Object for the field
472 * @param string $fieldname Name of the field
473 * @param array $descriptor Input Descriptor, as described above
474 * @param HTMLForm|null $parent Parent instance of HTMLForm
476 * @throws MWException
477 * @return HTMLFormField Instance of a subclass of HTMLFormField
479 public static function loadInputFromParameters( $fieldname, $descriptor,
480 HTMLForm
$parent = null
482 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
484 $descriptor['fieldname'] = $fieldname;
486 $descriptor['parent'] = $parent;
489 # @todo This will throw a fatal error whenever someone try to use
490 # 'class' to feed a CSS class instead of 'cssclass'. Would be
491 # great to avoid the fatal error and show a nice error.
492 return new $class( $descriptor );
496 * Prepare form for submission.
498 * @warning When doing method chaining, that should be the very last
499 * method call before displayForm().
501 * @throws MWException
502 * @return HTMLForm $this for chaining calls (since 1.20)
504 public function prepareForm() {
505 # Check if we have the info we need
506 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
507 throw new MWException( 'You must call setTitle() on an HTMLForm' );
510 # Load data from the request.
512 $this->mFormIdentifier
=== null ||
513 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
517 $this->mFieldData
= [];
524 * Try submitting, with edit token check first
525 * @return Status|bool
527 public function tryAuthorizedSubmit() {
531 if ( $this->mFormIdentifier
=== null ) {
534 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
;
538 if ( $this->getMethod() !== 'post' ) {
539 $tokenOkay = true; // no session check needed
540 } elseif ( $this->getRequest()->wasPosted() ) {
541 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
542 if ( $this->getUser()->isLoggedIn() ||
$editToken !== null ) {
543 // Session tokens for logged-out users have no security value.
544 // However, if the user gave one, check it in order to give a nice
545 // "session expired" error instead of "permission denied" or such.
546 $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt
);
552 if ( $tokenOkay && $identOkay ) {
553 $this->mWasSubmitted
= true;
554 $result = $this->trySubmit();
561 * The here's-one-I-made-earlier option: do the submission if
562 * posted, or display the form with or without funky validation
564 * @return bool|Status Whether submission was successful.
566 public function show() {
567 $this->prepareForm();
569 $result = $this->tryAuthorizedSubmit();
570 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
574 $this->displayForm( $result );
580 * Same as self::show with the difference, that the form will be
581 * added to the output, no matter, if the validation was good or not.
582 * @return bool|Status Whether submission was successful.
584 public function showAlways() {
585 $this->prepareForm();
587 $result = $this->tryAuthorizedSubmit();
589 $this->displayForm( $result );
595 * Validate all the fields, and call the submission callback
596 * function if everything is kosher.
597 * @throws MWException
598 * @return bool|string|array|Status
599 * - Bool true or a good Status object indicates success,
600 * - Bool false indicates no submission was attempted,
601 * - Anything else indicates failure. The value may be a fatal Status
602 * object, an HTML string, or an array of arrays (message keys and
603 * params) or strings (message keys)
605 public function trySubmit() {
607 $hoistedErrors = Status
::newGood();
608 if ( $this->mValidationErrorMessage
) {
609 foreach ( (array)$this->mValidationErrorMessage
as $error ) {
610 call_user_func_array( [ $hoistedErrors, 'fatal' ], $error );
613 $hoistedErrors->fatal( 'htmlform-invalid-input' );
616 $this->mWasSubmitted
= true;
618 # Check for cancelled submission
619 foreach ( $this->mFlatFields
as $fieldname => $field ) {
620 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
623 if ( $field->cancelSubmit( $this->mFieldData
[$fieldname], $this->mFieldData
) ) {
624 $this->mWasSubmitted
= false;
629 # Check for validation
630 foreach ( $this->mFlatFields
as $fieldname => $field ) {
631 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
634 if ( $field->isHidden( $this->mFieldData
) ) {
637 $res = $field->validate( $this->mFieldData
[$fieldname], $this->mFieldData
);
638 if ( $res !== true ) {
640 if ( $res !== false && !$field->canDisplayErrors() ) {
641 if ( is_string( $res ) ) {
642 $hoistedErrors->fatal( 'rawmessage', $res );
644 $hoistedErrors->fatal( $res );
651 return $hoistedErrors;
654 $callback = $this->mSubmitCallback
;
655 if ( !is_callable( $callback ) ) {
656 throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
657 'setSubmitCallback() to set one.' );
660 $data = $this->filterDataForSubmit( $this->mFieldData
);
662 $res = call_user_func( $callback, $data, $this );
663 if ( $res === false ) {
664 $this->mWasSubmitted
= false;
671 * Test whether the form was considered to have been submitted or not, i.e.
672 * whether the last call to tryAuthorizedSubmit or trySubmit returned
675 * This will return false until HTMLForm::tryAuthorizedSubmit or
676 * HTMLForm::trySubmit is called.
681 public function wasSubmitted() {
682 return $this->mWasSubmitted
;
686 * Set a callback to a function to do something with the form
687 * once it's been successfully validated.
689 * @param callable $cb The function will be passed the output from
690 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
691 * return as documented for HTMLForm::trySubmit
693 * @return HTMLForm $this for chaining calls (since 1.20)
695 public function setSubmitCallback( $cb ) {
696 $this->mSubmitCallback
= $cb;
702 * Set a message to display on a validation error.
704 * @param string|array $msg String or Array of valid inputs to wfMessage()
705 * (so each entry can be either a String or Array)
707 * @return HTMLForm $this for chaining calls (since 1.20)
709 public function setValidationErrorMessage( $msg ) {
710 $this->mValidationErrorMessage
= $msg;
716 * Set the introductory message, overwriting any existing message.
718 * @param string $msg Complete text of message to display
720 * @return HTMLForm $this for chaining calls (since 1.20)
722 public function setIntro( $msg ) {
723 $this->setPreText( $msg );
729 * Set the introductory message HTML, overwriting any existing message.
732 * @param string $msg Complete HTML of message to display
734 * @return HTMLForm $this for chaining calls (since 1.20)
736 public function setPreText( $msg ) {
743 * Add HTML to introductory message.
745 * @param string $msg Complete HTML of message to display
747 * @return HTMLForm $this for chaining calls (since 1.20)
749 public function addPreText( $msg ) {
756 * Add HTML to the header, inside the form.
758 * @param string $msg Additional HTML to display in header
759 * @param string|null $section The section to add the header to
761 * @return HTMLForm $this for chaining calls (since 1.20)
763 public function addHeaderText( $msg, $section = null ) {
764 if ( $section === null ) {
765 $this->mHeader
.= $msg;
767 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
768 $this->mSectionHeaders
[$section] = '';
770 $this->mSectionHeaders
[$section] .= $msg;
777 * Set header text, inside the form.
780 * @param string $msg Complete HTML of header to display
781 * @param string|null $section The section to add the header to
783 * @return HTMLForm $this for chaining calls (since 1.20)
785 public function setHeaderText( $msg, $section = null ) {
786 if ( $section === null ) {
787 $this->mHeader
= $msg;
789 $this->mSectionHeaders
[$section] = $msg;
798 * @param string|null $section The section to get the header text for
800 * @return string HTML
802 public function getHeaderText( $section = null ) {
803 if ( $section === null ) {
804 return $this->mHeader
;
806 return $this->mSectionHeaders
[$section] ??
'';
811 * Add footer text, inside the form.
813 * @param string $msg Complete text of message to display
814 * @param string|null $section The section to add the footer text to
816 * @return HTMLForm $this for chaining calls (since 1.20)
818 public function addFooterText( $msg, $section = null ) {
819 if ( $section === null ) {
820 $this->mFooter
.= $msg;
822 if ( !isset( $this->mSectionFooters
[$section] ) ) {
823 $this->mSectionFooters
[$section] = '';
825 $this->mSectionFooters
[$section] .= $msg;
832 * Set footer text, inside the form.
835 * @param string $msg Complete text of message to display
836 * @param string|null $section The section to add the footer text to
838 * @return HTMLForm $this for chaining calls (since 1.20)
840 public function setFooterText( $msg, $section = null ) {
841 if ( $section === null ) {
842 $this->mFooter
= $msg;
844 $this->mSectionFooters
[$section] = $msg;
853 * @param string|null $section The section to get the footer text for
857 public function getFooterText( $section = null ) {
858 if ( $section === null ) {
859 return $this->mFooter
;
861 return $this->mSectionFooters
[$section] ??
'';
866 * Add text to the end of the display.
868 * @param string $msg Complete text of message to display
870 * @return HTMLForm $this for chaining calls (since 1.20)
872 public function addPostText( $msg ) {
873 $this->mPost
.= $msg;
879 * Set text at the end of the display.
881 * @param string $msg Complete text of message to display
883 * @return HTMLForm $this for chaining calls (since 1.20)
885 public function setPostText( $msg ) {
892 * Add a hidden field to the output
894 * @param string $name Field name. This will be used exactly as entered
895 * @param string $value Field value
896 * @param array $attribs
898 * @return HTMLForm $this for chaining calls (since 1.20)
900 public function addHiddenField( $name, $value, array $attribs = [] ) {
901 $attribs +
= [ 'name' => $name ];
902 $this->mHiddenFields
[] = [ $value, $attribs ];
908 * Add an array of hidden fields to the output
912 * @param array $fields Associative array of fields to add;
913 * mapping names to their values
915 * @return HTMLForm $this for chaining calls
917 public function addHiddenFields( array $fields ) {
918 foreach ( $fields as $name => $value ) {
919 $this->mHiddenFields
[] = [ $value, [ 'name' => $name ] ];
926 * Add a button to the form
928 * @since 1.27 takes an array as shown. Earlier versions accepted
929 * 'name', 'value', 'id', and 'attribs' as separate parameters in that
931 * @note Custom labels ('label', 'label-message', 'label-raw') are not
932 * supported for IE6 and IE7 due to bugs in those browsers. If detected,
933 * they will be served buttons using 'value' as the button label.
934 * @param array $data Data to define the button:
935 * - name: (string) Button name.
936 * - value: (string) Button value.
937 * - label-message: (string, optional) Button label message key to use
938 * instead of 'value'. Overrides 'label' and 'label-raw'.
939 * - label: (string, optional) Button label text to use instead of
940 * 'value'. Overrides 'label-raw'.
941 * - label-raw: (string, optional) Button label HTML to use instead of
943 * - id: (string, optional) DOM id for the button.
944 * - attribs: (array, optional) Additional HTML attributes.
945 * - flags: (string|string[], optional) OOUI flags.
946 * - framed: (boolean=true, optional) OOUI framed attribute.
947 * @return HTMLForm $this for chaining calls (since 1.20)
949 public function addButton( $data ) {
950 if ( !is_array( $data ) ) {
951 $args = func_get_args();
952 if ( count( $args ) < 2 ||
count( $args ) > 4 ) {
953 throw new InvalidArgumentException(
954 'Incorrect number of arguments for deprecated calling style'
960 'id' => $args[2] ??
null,
961 'attribs' => $args[3] ??
null,
964 if ( !isset( $data['name'] ) ) {
965 throw new InvalidArgumentException( 'A name is required' );
967 if ( !isset( $data['value'] ) ) {
968 throw new InvalidArgumentException( 'A value is required' );
971 $this->mButtons
[] = $data +
[
982 * Set the salt for the edit token.
984 * Only useful when the method is "post".
987 * @param string|array $salt Salt to use
988 * @return HTMLForm $this For chaining calls
990 public function setTokenSalt( $salt ) {
991 $this->mTokenSalt
= $salt;
997 * Display the form (sending to the context's OutputPage object), with an
998 * appropriate error message or stack of messages, and any validation errors, etc.
1000 * @warning You should call prepareForm() before calling this function.
1001 * Moreover, when doing method chaining this should be the very last method
1002 * call just after prepareForm().
1004 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1006 * @return void Nothing, should be last call
1008 public function displayForm( $submitResult ) {
1009 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1013 * Returns the raw HTML generated by the form
1015 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1017 * @return string HTML
1019 public function getHTML( $submitResult ) {
1020 # For good measure (it is the default)
1021 $this->getOutput()->preventClickjacking();
1022 $this->getOutput()->addModules( 'mediawiki.htmlform' );
1023 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1026 . $this->getErrorsOrWarnings( $submitResult, 'error' )
1027 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1028 . $this->getHeaderText()
1030 . $this->getHiddenFields()
1031 . $this->getButtons()
1032 . $this->getFooterText();
1034 $html = $this->wrapForm( $html );
1036 return '' . $this->mPre
. $html . $this->mPost
;
1040 * Get HTML attributes for the `<form>` tag.
1043 protected function getFormAttributes() {
1044 # Use multipart/form-data
1045 $encType = $this->mUseMultipart
1046 ?
'multipart/form-data'
1047 : 'application/x-www-form-urlencoded';
1050 'class' => 'mw-htmlform',
1051 'action' => $this->getAction(),
1052 'method' => $this->getMethod(),
1053 'enctype' => $encType,
1056 $attribs['id'] = $this->mId
;
1058 if ( is_string( $this->mAutocomplete
) ) {
1059 $attribs['autocomplete'] = $this->mAutocomplete
;
1061 if ( $this->mName
) {
1062 $attribs['name'] = $this->mName
;
1064 if ( $this->needsJSForHtml5FormValidation() ) {
1065 $attribs['novalidate'] = true;
1071 * Wrap the form innards in an actual "<form>" element
1073 * @param string $html HTML contents to wrap.
1075 * @return string Wrapped HTML.
1077 public function wrapForm( $html ) {
1078 # Include a <fieldset> wrapper for style, if requested.
1079 if ( $this->mWrapperLegend
!== false ) {
1080 $legend = is_string( $this->mWrapperLegend
) ?
$this->mWrapperLegend
: false;
1081 $html = Xml
::fieldset( $legend, $html );
1084 return Html
::rawElement(
1086 $this->getFormAttributes(),
1092 * Get the hidden fields that should go inside the form.
1093 * @return string HTML.
1095 public function getHiddenFields() {
1097 if ( $this->mFormIdentifier
!== null ) {
1098 $html .= Html
::hidden(
1100 $this->mFormIdentifier
1103 if ( $this->getMethod() === 'post' ) {
1104 $html .= Html
::hidden(
1106 $this->getUser()->getEditToken( $this->mTokenSalt
),
1107 [ 'id' => 'wpEditToken' ]
1109 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1112 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1113 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1114 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1117 foreach ( $this->mHiddenFields
as $data ) {
1118 list( $value, $attribs ) = $data;
1119 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
1126 * Get the submit and (potentially) reset buttons.
1127 * @return string HTML.
1129 public function getButtons() {
1131 $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1133 if ( $this->mShowSubmit
) {
1136 if ( isset( $this->mSubmitID
) ) {
1137 $attribs['id'] = $this->mSubmitID
;
1140 if ( isset( $this->mSubmitName
) ) {
1141 $attribs['name'] = $this->mSubmitName
;
1144 if ( isset( $this->mSubmitTooltip
) ) {
1145 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
1148 $attribs['class'] = [ 'mw-htmlform-submit' ];
1150 if ( $useMediaWikiUIEverywhere ) {
1151 foreach ( $this->mSubmitFlags
as $flag ) {
1152 $attribs['class'][] = 'mw-ui-' . $flag;
1154 $attribs['class'][] = 'mw-ui-button';
1157 $buttons .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1160 if ( $this->mShowReset
) {
1161 $buttons .= Html
::element(
1165 'value' => $this->msg( 'htmlform-reset' )->text(),
1166 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1171 if ( $this->mShowCancel
) {
1172 $target = $this->mCancelTarget ?
: Title
::newMainPage();
1173 if ( $target instanceof Title
) {
1174 $target = $target->getLocalURL();
1176 $buttons .= Html
::element(
1179 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1182 $this->msg( 'cancel' )->text()
1186 // IE<8 has bugs with <button>, so we'll need to avoid them.
1187 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
1189 foreach ( $this->mButtons
as $button ) {
1192 'name' => $button['name'],
1193 'value' => $button['value']
1196 if ( isset( $button['label-message'] ) ) {
1197 $label = $this->getMessage( $button['label-message'] )->parse();
1198 } elseif ( isset( $button['label'] ) ) {
1199 $label = htmlspecialchars( $button['label'] );
1200 } elseif ( isset( $button['label-raw'] ) ) {
1201 $label = $button['label-raw'];
1203 $label = htmlspecialchars( $button['value'] );
1206 if ( $button['attribs'] ) {
1207 $attrs +
= $button['attribs'];
1210 if ( isset( $button['id'] ) ) {
1211 $attrs['id'] = $button['id'];
1214 if ( $useMediaWikiUIEverywhere ) {
1215 $attrs['class'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : [];
1216 $attrs['class'][] = 'mw-ui-button';
1220 $buttons .= Html
::element( 'input', $attrs ) . "\n";
1222 $buttons .= Html
::rawElement( 'button', $attrs, $label ) . "\n";
1230 return Html
::rawElement( 'span',
1231 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1235 * Get the whole body of the form.
1238 public function getBody() {
1239 return $this->displaySection( $this->mFieldTree
, $this->mTableId
);
1243 * Format and display an error message stack.
1245 * @param string|array|Status $errors
1247 * @deprecated since 1.28, use getErrorsOrWarnings() instead
1251 public function getErrors( $errors ) {
1252 wfDeprecated( __METHOD__
);
1253 return $this->getErrorsOrWarnings( $errors, 'error' );
1257 * Returns a formatted list of errors or warnings from the given elements.
1259 * @param string|array|Status $elements The set of errors/warnings to process.
1260 * @param string $elementsType Should warnings or errors be returned. This is meant
1261 * for Status objects, all other valid types are always considered as errors.
1264 public function getErrorsOrWarnings( $elements, $elementsType ) {
1265 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1266 throw new DomainException( $elementsType . ' is not a valid type.' );
1268 $elementstr = false;
1269 if ( $elements instanceof Status
) {
1270 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1271 $status = $elementsType === 'error' ?
$errorStatus : $warningStatus;
1272 if ( $status->isGood() ) {
1275 $elementstr = $this->getOutput()->parse(
1276 $status->getWikiText()
1279 } elseif ( is_array( $elements ) && $elementsType === 'error' ) {
1280 $elementstr = $this->formatErrors( $elements );
1281 } elseif ( $elementsType === 'error' ) {
1282 $elementstr = $elements;
1286 ? Html
::rawElement( 'div', [ 'class' => $elementsType ], $elementstr )
1291 * Format a stack of error messages into a single HTML string
1293 * @param array $errors Array of message keys/values
1295 * @return string HTML, a "<ul>" list of errors
1297 public function formatErrors( $errors ) {
1300 foreach ( $errors as $error ) {
1301 $errorstr .= Html
::rawElement(
1304 $this->getMessage( $error )->parse()
1308 $errorstr = Html
::rawElement( 'ul', [], $errorstr );
1314 * Set the text for the submit button
1316 * @param string $t Plaintext
1318 * @return HTMLForm $this for chaining calls (since 1.20)
1320 public function setSubmitText( $t ) {
1321 $this->mSubmitText
= $t;
1327 * Identify that the submit button in the form has a destructive action
1330 * @return HTMLForm $this for chaining calls (since 1.28)
1332 public function setSubmitDestructive() {
1333 $this->mSubmitFlags
= [ 'destructive', 'primary' ];
1339 * Identify that the submit button in the form has a progressive action
1341 * @deprecated since 1.32, No need to call. Submit button already
1342 * has a progressive action form.
1344 * @return HTMLForm $this for chaining calls (since 1.28)
1346 public function setSubmitProgressive() {
1347 wfDeprecated( __METHOD__
, '1.32' );
1348 $this->mSubmitFlags
= [ 'progressive', 'primary' ];
1354 * Set the text for the submit button to a message
1357 * @param string|Message $msg Message key or Message object
1359 * @return HTMLForm $this for chaining calls (since 1.20)
1361 public function setSubmitTextMsg( $msg ) {
1362 if ( !$msg instanceof Message
) {
1363 $msg = $this->msg( $msg );
1365 $this->setSubmitText( $msg->text() );
1371 * Get the text for the submit button, either customised or a default.
1374 public function getSubmitText() {
1375 return $this->mSubmitText ?
: $this->msg( 'htmlform-submit' )->text();
1379 * @param string $name Submit button name
1381 * @return HTMLForm $this for chaining calls (since 1.20)
1383 public function setSubmitName( $name ) {
1384 $this->mSubmitName
= $name;
1390 * @param string $name Tooltip for the submit button
1392 * @return HTMLForm $this for chaining calls (since 1.20)
1394 public function setSubmitTooltip( $name ) {
1395 $this->mSubmitTooltip
= $name;
1401 * Set the id for the submit button.
1405 * @todo FIXME: Integrity of $t is *not* validated
1406 * @return HTMLForm $this for chaining calls (since 1.20)
1408 public function setSubmitID( $t ) {
1409 $this->mSubmitID
= $t;
1415 * Set an internal identifier for this form. It will be submitted as a hidden form field, allowing
1416 * HTMLForm to determine whether the form was submitted (or merely viewed). Setting this serves
1419 * - If you use two or more forms on one page, it allows HTMLForm to identify which of the forms
1420 * was submitted, and not attempt to validate the other ones.
1421 * - If you use checkbox or multiselect fields inside a form using the GET method, it allows
1422 * HTMLForm to distinguish between the initial page view and a form submission with all
1423 * checkboxes or select options unchecked.
1426 * @param string $ident
1429 public function setFormIdentifier( $ident ) {
1430 $this->mFormIdentifier
= $ident;
1436 * Stop a default submit button being shown for this form. This implies that an
1437 * alternate submit method must be provided manually.
1441 * @param bool $suppressSubmit Set to false to re-enable the button again
1443 * @return HTMLForm $this for chaining calls
1445 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1446 $this->mShowSubmit
= !$suppressSubmit;
1452 * Show a cancel button (or prevent it). The button is not shown by default.
1454 * @return HTMLForm $this for chaining calls
1457 public function showCancel( $show = true ) {
1458 $this->mShowCancel
= $show;
1463 * Sets the target where the user is redirected to after clicking cancel.
1464 * @param Title|string $target Target as a Title object or an URL
1465 * @return HTMLForm $this for chaining calls
1468 public function setCancelTarget( $target ) {
1469 $this->mCancelTarget
= $target;
1474 * Set the id of the \<table\> or outermost \<div\> element.
1478 * @param string $id New value of the id attribute, or "" to remove
1480 * @return HTMLForm $this for chaining calls
1482 public function setTableId( $id ) {
1483 $this->mTableId
= $id;
1489 * @param string $id DOM id for the form
1491 * @return HTMLForm $this for chaining calls (since 1.20)
1493 public function setId( $id ) {
1500 * @param string $name 'name' attribute for the form
1501 * @return HTMLForm $this for chaining calls
1503 public function setName( $name ) {
1504 $this->mName
= $name;
1510 * Prompt the whole form to be wrapped in a "<fieldset>", with
1511 * this text as its "<legend>" element.
1513 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1514 * If true, a wrapper will be displayed, but no legend.
1515 * If a string, a wrapper will be displayed with that string as a legend.
1516 * The string will be escaped before being output (this doesn't support HTML).
1518 * @return HTMLForm $this for chaining calls (since 1.20)
1520 public function setWrapperLegend( $legend ) {
1521 $this->mWrapperLegend
= $legend;
1527 * Prompt the whole form to be wrapped in a "<fieldset>", with
1528 * this message as its "<legend>" element.
1531 * @param string|Message $msg Message key or Message object
1533 * @return HTMLForm $this for chaining calls (since 1.20)
1535 public function setWrapperLegendMsg( $msg ) {
1536 if ( !$msg instanceof Message
) {
1537 $msg = $this->msg( $msg );
1539 $this->setWrapperLegend( $msg->text() );
1545 * Set the prefix for various default messages
1546 * @todo Currently only used for the "<fieldset>" legend on forms
1547 * with multiple sections; should be used elsewhere?
1551 * @return HTMLForm $this for chaining calls (since 1.20)
1553 public function setMessagePrefix( $p ) {
1554 $this->mMessagePrefix
= $p;
1560 * Set the title for form submission
1562 * @param Title $t Title of page the form is on/should be posted to
1564 * @return HTMLForm $this for chaining calls (since 1.20)
1566 public function setTitle( $t ) {
1576 public function getTitle() {
1577 return $this->mTitle
=== false
1578 ?
$this->getContext()->getTitle()
1583 * Set the method used to submit the form
1585 * @param string $method
1587 * @return HTMLForm $this for chaining calls (since 1.20)
1589 public function setMethod( $method = 'post' ) {
1590 $this->mMethod
= strtolower( $method );
1596 * @return string Always lowercase
1598 public function getMethod() {
1599 return $this->mMethod
;
1603 * Wraps the given $section into an user-visible fieldset.
1605 * @param string $legend Legend text for the fieldset
1606 * @param string $section The section content in plain Html
1607 * @param array $attributes Additional attributes for the fieldset
1608 * @param bool $isRoot Section is at the root of the tree
1609 * @return string The fieldset's Html
1611 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1612 return Xml
::fieldset( $legend, $section, $attributes ) . "\n";
1618 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
1620 * @param string $sectionName ID attribute of the "<table>" tag for this
1621 * section, ignored if empty.
1622 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
1623 * each subsection, ignored if empty.
1624 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
1625 * @throws LogicException When called on uninitialized field data, e.g. When
1626 * HTMLForm::displayForm was called without calling HTMLForm::prepareForm
1631 public function displaySection( $fields,
1633 $fieldsetIDPrefix = '',
1634 &$hasUserVisibleFields = false
1636 if ( $this->mFieldData
=== null ) {
1637 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1638 . 'You probably called displayForm() without calling prepareForm() first.' );
1641 $displayFormat = $this->getDisplayFormat();
1644 $subsectionHtml = '';
1647 // Conveniently, PHP method names are case-insensitive.
1648 // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1649 $getFieldHtmlMethod = $displayFormat === 'table' ?
'getTableRow' : ( 'get' . $displayFormat );
1651 foreach ( $fields as $key => $value ) {
1652 if ( $value instanceof HTMLFormField
) {
1653 $v = array_key_exists( $key, $this->mFieldData
)
1654 ?
$this->mFieldData
[$key]
1655 : $value->getDefault();
1657 $retval = $value->$getFieldHtmlMethod( $v );
1659 // check, if the form field should be added to
1661 if ( $value->hasVisibleOutput() ) {
1664 $labelValue = trim( $value->getLabel() );
1665 if ( $labelValue !== ' ' && $labelValue !== '' ) {
1669 $hasUserVisibleFields = true;
1671 } elseif ( is_array( $value ) ) {
1672 $subsectionHasVisibleFields = false;
1674 $this->displaySection( $value,
1676 "$fieldsetIDPrefix$key-",
1677 $subsectionHasVisibleFields );
1680 if ( $subsectionHasVisibleFields === true ) {
1681 // Display the section with various niceties.
1682 $hasUserVisibleFields = true;
1684 $legend = $this->getLegend( $key );
1686 $section = $this->getHeaderText( $key ) .
1688 $this->getFooterText( $key );
1691 if ( $fieldsetIDPrefix ) {
1692 $attributes['id'] = Sanitizer
::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1694 $subsectionHtml .= $this->wrapFieldSetSection(
1695 $legend, $section, $attributes, $fields === $this->mFieldTree
1698 // Just return the inputs, nothing fancy.
1699 $subsectionHtml .= $section;
1704 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1706 if ( $subsectionHtml ) {
1707 if ( $this->mSubSectionBeforeFields
) {
1708 return $subsectionHtml . "\n" . $html;
1710 return $html . "\n" . $subsectionHtml;
1718 * Put a form section together from the individual fields' HTML, merging it and wrapping.
1719 * @param array $fieldsHtml
1720 * @param string $sectionName
1721 * @param bool $anyFieldHasLabel
1722 * @return string HTML
1724 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1725 if ( !$fieldsHtml ) {
1726 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
1727 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
1731 $displayFormat = $this->getDisplayFormat();
1732 $html = implode( '', $fieldsHtml );
1734 if ( $displayFormat === 'raw' ) {
1740 if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1741 $classes[] = 'mw-htmlform-nolabel';
1745 'class' => implode( ' ', $classes ),
1748 if ( $sectionName ) {
1749 $attribs['id'] = Sanitizer
::escapeIdForAttribute( $sectionName );
1752 if ( $displayFormat === 'table' ) {
1753 return Html
::rawElement( 'table',
1755 Html
::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
1756 } elseif ( $displayFormat === 'inline' ) {
1757 return Html
::rawElement( 'span', $attribs, "\n$html\n" );
1759 return Html
::rawElement( 'div', $attribs, "\n$html\n" );
1764 * Construct the form fields from the Descriptor array
1766 public function loadData() {
1769 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1770 $request = $this->getRequest();
1771 if ( $field->skipLoadData( $request ) ) {
1773 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
1774 $fieldData[$fieldname] = $field->getDefault();
1776 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1781 foreach ( $fieldData as $name => &$value ) {
1782 $field = $this->mFlatFields
[$name];
1783 $value = $field->filter( $value, $this->mFlatFields
);
1786 $this->mFieldData
= $fieldData;
1790 * Stop a reset button being shown for this form
1792 * @param bool $suppressReset Set to false to re-enable the button again
1794 * @return HTMLForm $this for chaining calls (since 1.20)
1796 public function suppressReset( $suppressReset = true ) {
1797 $this->mShowReset
= !$suppressReset;
1803 * Overload this if you want to apply special filtration routines
1804 * to the form as a whole, after it's submitted but before it's
1807 * @param array $data
1811 public function filterDataForSubmit( $data ) {
1816 * Get a string to go in the "<legend>" of a section fieldset.
1817 * Override this if you want something more complicated.
1819 * @param string $key
1823 public function getLegend( $key ) {
1824 return $this->msg( "{$this->mMessagePrefix}-$key" )->text();
1828 * Set the value for the action attribute of the form.
1829 * When set to false (which is the default state), the set title is used.
1833 * @param string|bool $action
1835 * @return HTMLForm $this for chaining calls (since 1.20)
1837 public function setAction( $action ) {
1838 $this->mAction
= $action;
1844 * Get the value for the action attribute of the form.
1850 public function getAction() {
1851 // If an action is alredy provided, return it
1852 if ( $this->mAction
!== false ) {
1853 return $this->mAction
;
1856 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1857 // Check whether we are in GET mode and the ArticlePath contains a "?"
1858 // meaning that getLocalURL() would return something like "index.php?title=...".
1859 // As browser remove the query string before submitting GET forms,
1860 // it means that the title would be lost. In such case use wfScript() instead
1861 // and put title in an hidden field (see getHiddenFields()).
1862 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1866 return $this->getTitle()->getLocalURL();
1870 * Set the value for the autocomplete attribute of the form. A typical value is "off".
1871 * When set to null (which is the default state), the attribute get not set.
1875 * @param string|null $autocomplete
1877 * @return HTMLForm $this for chaining calls
1879 public function setAutocomplete( $autocomplete ) {
1880 $this->mAutocomplete
= $autocomplete;
1886 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1887 * name + parameters array) into a Message.
1888 * @param mixed $value
1891 protected function getMessage( $value ) {
1892 return Message
::newFromSpecifier( $value )->setContext( $this );
1896 * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
1897 * for the client-side HTML5 form validation to work correctly. If this function returns true, a
1898 * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
1899 * agent has JavaScript support, in htmlform.js.
1904 public function needsJSForHtml5FormValidation() {
1905 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1906 if ( $field->needsJSForHtml5FormValidation() ) {