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 * 'id' -- HTML id attribute
54 * 'cssclass' -- CSS class
55 * 'csshelpclass' -- CSS class used to style help text
56 * 'dir' -- Direction of the element.
57 * 'options' -- associative array mapping labels to values.
58 * Some field types support multi-level arrays.
59 * 'options-messages' -- associative array mapping message keys to values.
60 * Some field types support multi-level arrays.
61 * 'options-message' -- message key or object to be parsed to extract the list of
62 * options (like 'ipbreason-dropdown').
63 * 'label-message' -- message key or object for a message to use as the label.
64 * can be an array of msg key and then parameters to
66 * 'label' -- alternatively, a raw text message. Overridden by
68 * 'help' -- message text for a message to use as a help text.
69 * 'help-message' -- message key or object for a message to use as a help text.
70 * can be an array of msg key and then parameters to
72 * Overwrites 'help-messages' and 'help'.
73 * 'help-messages' -- array of message keys/objects. As above, each item can
74 * be an array of msg key and then parameters.
76 * 'notice' -- message text for a message to use as a notice in the field.
77 * Currently used by OOUI form fields only.
78 * 'notice-messages' -- array of message keys/objects to use for notice.
80 * 'notice-message' -- message key or object to use as a notice.
81 * 'required' -- passed through to the object, indicating that it
82 * is a required field.
83 * 'size' -- the length of text fields
84 * 'filter-callback' -- a function name to give you the chance to
85 * massage the inputted value before it's processed.
86 * @see HTMLFormField::filter()
87 * 'validation-callback' -- a function name to give you the chance
88 * to impose extra validation on the field input.
89 * @see HTMLFormField::validate()
90 * 'name' -- By default, the 'name' attribute of the input field
91 * is "wp{$fieldname}". If you want a different name
92 * (eg one without the "wp" prefix), specify it here and
93 * it will be used without modification.
94 * 'hide-if' -- expression given as an array stating when the field
95 * should be hidden. The first array value has to be the
96 * expression's logic operator. Supported expressions:
98 * [ 'NOT', array $expression ]
99 * To hide a field if a given expression is not true.
101 * [ '===', string $fieldName, string $value ]
102 * To hide a field if another field identified by
103 * $field has the value $value.
105 * [ '!==', string $fieldName, string $value ]
106 * Same as [ 'NOT', [ '===', $fieldName, $value ]
107 * 'OR', 'AND', 'NOR', 'NAND'
108 * [ 'XXX', array $expression1, ..., array $expressionN ]
109 * To hide a field if one or more (OR), all (AND),
110 * neither (NOR) or not all (NAND) given expressions
111 * are evaluated as true.
112 * The expressions will be given to a JavaScript frontend
113 * module which will continually update the field's
116 * Since 1.20, you can chain mutators to ease the form generation:
119 * $form = new HTMLForm( $someFields );
120 * $form->setMethod( 'get' )
121 * ->setWrapperLegendMsg( 'message-key' )
123 * ->displayForm( '' );
125 * Note that you will have prepareForm and displayForm at the end. Other
126 * methods call done after that would simply not be part of the form :(
128 * @todo Document 'section' / 'subsection' stuff
130 class HTMLForm
extends ContextSource
{
131 // A mapping of 'type' inputs onto standard HTMLFormField subclasses
132 public static $typeMappings = [
133 'api' => HTMLApiField
::class,
134 'text' => HTMLTextField
::class,
135 'textwithbutton' => HTMLTextFieldWithButton
::class,
136 'textarea' => HTMLTextAreaField
::class,
137 'select' => HTMLSelectField
::class,
138 'combobox' => HTMLComboboxField
::class,
139 'radio' => HTMLRadioField
::class,
140 'multiselect' => HTMLMultiSelectField
::class,
141 'limitselect' => HTMLSelectLimitField
::class,
142 'check' => HTMLCheckField
::class,
143 'toggle' => HTMLCheckField
::class,
144 'int' => HTMLIntField
::class,
145 'float' => HTMLFloatField
::class,
146 'info' => HTMLInfoField
::class,
147 'selectorother' => HTMLSelectOrOtherField
::class,
148 'selectandother' => HTMLSelectAndOtherField
::class,
149 'namespaceselect' => HTMLSelectNamespace
::class,
150 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton
::class,
151 'tagfilter' => HTMLTagFilter
::class,
152 'sizefilter' => HTMLSizeFilterField
::class,
153 'submit' => HTMLSubmitField
::class,
154 'hidden' => HTMLHiddenField
::class,
155 'edittools' => HTMLEditTools
::class,
156 'checkmatrix' => HTMLCheckMatrix
::class,
157 'cloner' => HTMLFormFieldCloner
::class,
158 'autocompleteselect' => HTMLAutoCompleteSelectField
::class,
159 'date' => HTMLDateTimeField
::class,
160 'time' => HTMLDateTimeField
::class,
161 'datetime' => HTMLDateTimeField
::class,
162 // HTMLTextField will output the correct type="" attribute automagically.
163 // There are about four zillion other HTML5 input types, like range, but
164 // we don't use those at the moment, so no point in adding all of them.
165 'email' => HTMLTextField
::class,
166 'password' => HTMLTextField
::class,
167 'url' => HTMLTextField
::class,
168 'title' => HTMLTitleTextField
::class,
169 'user' => HTMLUserTextField
::class,
170 'usersmultiselect' => HTMLUsersMultiselectField
::class,
175 protected $mMessagePrefix;
177 /** @var HTMLFormField[] */
178 protected $mFlatFields;
180 protected $mFieldTree;
181 protected $mShowReset = false;
182 protected $mShowSubmit = true;
183 protected $mSubmitFlags = [ 'primary', 'progressive' ];
184 protected $mShowCancel = false;
185 protected $mCancelTarget;
187 protected $mSubmitCallback;
188 protected $mValidationErrorMessage;
190 protected $mPre = '';
191 protected $mHeader = '';
192 protected $mFooter = '';
193 protected $mSectionHeaders = [];
194 protected $mSectionFooters = [];
195 protected $mPost = '';
198 protected $mTableId = '';
200 protected $mSubmitID;
201 protected $mSubmitName;
202 protected $mSubmitText;
203 protected $mSubmitTooltip;
205 protected $mFormIdentifier;
207 protected $mMethod = 'post';
208 protected $mWasSubmitted = false;
211 * Form action URL. false means we will use the URL to set Title
215 protected $mAction = false;
218 * Form attribute autocomplete. A typical value is "off". null does not set the attribute
222 protected $mAutocomplete = null;
224 protected $mUseMultipart = false;
225 protected $mHiddenFields = [];
226 protected $mButtons = [];
228 protected $mWrapperLegend = false;
231 * Salt for the edit token.
234 protected $mTokenSalt = '';
237 * If true, sections that contain both fields and subsections will
238 * render their subsections before their fields.
240 * Subclasses may set this to false to render subsections after fields
243 protected $mSubSectionBeforeFields = true;
246 * Format in which to display form. For viable options,
247 * @see $availableDisplayFormats
250 protected $displayFormat = 'table';
253 * Available formats in which to display the form
256 protected $availableDisplayFormats = [
264 * Available formats in which to display the form
267 protected $availableSubclassDisplayFormats = [
273 * Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
275 * @param string $displayFormat
276 * @param mixed $arguments,... Additional arguments to pass to the constructor.
279 public static function factory( $displayFormat/*, $arguments...*/ ) {
280 $arguments = func_get_args();
281 array_shift( $arguments );
283 switch ( $displayFormat ) {
285 return ObjectFactory
::constructClassInstance( VFormHTMLForm
::class, $arguments );
287 return ObjectFactory
::constructClassInstance( OOUIHTMLForm
::class, $arguments );
289 /** @var HTMLForm $form */
290 $form = ObjectFactory
::constructClassInstance( self
::class, $arguments );
291 $form->setDisplayFormat( $displayFormat );
297 * Build a new HTMLForm from an array of field attributes
299 * @param array $descriptor Array of Field constructs, as described above
300 * @param IContextSource $context Available since 1.18, will become compulsory in 1.18.
301 * Obviates the need to call $form->setTitle()
302 * @param string $messagePrefix A prefix to go in front of default messages
304 public function __construct( $descriptor, /*IContextSource*/ $context = null,
307 if ( $context instanceof IContextSource
) {
308 $this->setContext( $context );
309 $this->mTitle
= false; // We don't need them to set a title
310 $this->mMessagePrefix
= $messagePrefix;
311 } elseif ( $context === null && $messagePrefix !== '' ) {
312 $this->mMessagePrefix
= $messagePrefix;
313 } elseif ( is_string( $context ) && $messagePrefix === '' ) {
315 // it's actually $messagePrefix
316 $this->mMessagePrefix
= $context;
319 // Evil hack for mobile :(
321 !$this->getConfig()->get( 'HTMLFormAllowTableFormat' )
322 && $this->displayFormat
=== 'table'
324 $this->displayFormat
= 'div';
327 // Expand out into a tree.
328 $loadedDescriptor = [];
329 $this->mFlatFields
= [];
331 foreach ( $descriptor as $fieldname => $info ) {
332 $section = isset( $info['section'] )
336 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
337 $this->mUseMultipart
= true;
340 $field = static::loadInputFromParameters( $fieldname, $info, $this );
342 $setSection =& $loadedDescriptor;
344 $sectionParts = explode( '/', $section );
346 while ( count( $sectionParts ) ) {
347 $newName = array_shift( $sectionParts );
349 if ( !isset( $setSection[$newName] ) ) {
350 $setSection[$newName] = [];
353 $setSection =& $setSection[$newName];
357 $setSection[$fieldname] = $field;
358 $this->mFlatFields
[$fieldname] = $field;
361 $this->mFieldTree
= $loadedDescriptor;
365 * @param string $fieldname
368 public function hasField( $fieldname ) {
369 return isset( $this->mFlatFields
[$fieldname] );
373 * @param string $fieldname
374 * @return HTMLFormField
375 * @throws DomainException on invalid field name
377 public function getField( $fieldname ) {
378 if ( !$this->hasField( $fieldname ) ) {
379 throw new DomainException( __METHOD__
. ': no field named ' . $fieldname );
381 return $this->mFlatFields
[$fieldname];
385 * Set format in which to display the form
387 * @param string $format The name of the format to use, must be one of
388 * $this->availableDisplayFormats
390 * @throws MWException
392 * @return HTMLForm $this for chaining calls (since 1.20)
394 public function setDisplayFormat( $format ) {
396 in_array( $format, $this->availableSubclassDisplayFormats
, true ) ||
397 in_array( $this->displayFormat
, $this->availableSubclassDisplayFormats
, true )
399 throw new MWException( 'Cannot change display format after creation, ' .
400 'use HTMLForm::factory() instead' );
403 if ( !in_array( $format, $this->availableDisplayFormats
, true ) ) {
404 throw new MWException( 'Display format must be one of ' .
407 $this->availableDisplayFormats
,
408 $this->availableSubclassDisplayFormats
414 // Evil hack for mobile :(
415 if ( !$this->getConfig()->get( 'HTMLFormAllowTableFormat' ) && $format === 'table' ) {
419 $this->displayFormat
= $format;
425 * Getter for displayFormat
429 public function getDisplayFormat() {
430 return $this->displayFormat
;
434 * Get the HTMLFormField subclass for this descriptor.
436 * The descriptor can be passed either 'class' which is the name of
437 * a HTMLFormField subclass, or a shorter 'type' which is an alias.
438 * This makes sure the 'class' is always set, and also is returned by
439 * this function for ease.
443 * @param string $fieldname Name of the field
444 * @param array &$descriptor Input Descriptor, as described above
446 * @throws MWException
447 * @return string Name of a HTMLFormField subclass
449 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
450 if ( isset( $descriptor['class'] ) ) {
451 $class = $descriptor['class'];
452 } elseif ( isset( $descriptor['type'] ) ) {
453 $class = static::$typeMappings[$descriptor['type']];
454 $descriptor['class'] = $class;
460 throw new MWException( "Descriptor with no class for $fieldname: "
461 . print_r( $descriptor, true ) );
468 * Initialise a new Object for the field
470 * @param string $fieldname Name of the field
471 * @param array $descriptor Input Descriptor, as described above
472 * @param HTMLForm|null $parent Parent instance of HTMLForm
474 * @throws MWException
475 * @return HTMLFormField Instance of a subclass of HTMLFormField
477 public static function loadInputFromParameters( $fieldname, $descriptor,
478 HTMLForm
$parent = null
480 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
482 $descriptor['fieldname'] = $fieldname;
484 $descriptor['parent'] = $parent;
487 # @todo This will throw a fatal error whenever someone try to use
488 # 'class' to feed a CSS class instead of 'cssclass'. Would be
489 # great to avoid the fatal error and show a nice error.
490 return new $class( $descriptor );
494 * Prepare form for submission.
496 * @warning When doing method chaining, that should be the very last
497 * method call before displayForm().
499 * @throws MWException
500 * @return HTMLForm $this for chaining calls (since 1.20)
502 public function prepareForm() {
503 # Check if we have the info we need
504 if ( !$this->mTitle
instanceof Title
&& $this->mTitle
!== false ) {
505 throw new MWException( 'You must call setTitle() on an HTMLForm' );
508 # Load data from the request.
510 $this->mFormIdentifier
=== null ||
511 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
515 $this->mFieldData
= [];
522 * Try submitting, with edit token check first
523 * @return Status|bool
525 public function tryAuthorizedSubmit() {
529 if ( $this->mFormIdentifier
=== null ) {
532 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier
;
536 if ( $this->getMethod() !== 'post' ) {
537 $tokenOkay = true; // no session check needed
538 } elseif ( $this->getRequest()->wasPosted() ) {
539 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
540 if ( $this->getUser()->isLoggedIn() ||
$editToken !== null ) {
541 // Session tokens for logged-out users have no security value.
542 // However, if the user gave one, check it in order to give a nice
543 // "session expired" error instead of "permission denied" or such.
544 $tokenOkay = $this->getUser()->matchEditToken( $editToken, $this->mTokenSalt
);
550 if ( $tokenOkay && $identOkay ) {
551 $this->mWasSubmitted
= true;
552 $result = $this->trySubmit();
559 * The here's-one-I-made-earlier option: do the submission if
560 * posted, or display the form with or without funky validation
562 * @return bool|Status Whether submission was successful.
564 public function show() {
565 $this->prepareForm();
567 $result = $this->tryAuthorizedSubmit();
568 if ( $result === true ||
( $result instanceof Status
&& $result->isGood() ) ) {
572 $this->displayForm( $result );
578 * Same as self::show with the difference, that the form will be
579 * added to the output, no matter, if the validation was good or not.
580 * @return bool|Status Whether submission was successful.
582 public function showAlways() {
583 $this->prepareForm();
585 $result = $this->tryAuthorizedSubmit();
587 $this->displayForm( $result );
593 * Validate all the fields, and call the submission callback
594 * function if everything is kosher.
595 * @throws MWException
596 * @return bool|string|array|Status
597 * - Bool true or a good Status object indicates success,
598 * - Bool false indicates no submission was attempted,
599 * - Anything else indicates failure. The value may be a fatal Status
600 * object, an HTML string, or an array of arrays (message keys and
601 * params) or strings (message keys)
603 public function trySubmit() {
605 $hoistedErrors = Status
::newGood();
606 if ( $this->mValidationErrorMessage
) {
607 foreach ( (array)$this->mValidationErrorMessage
as $error ) {
608 call_user_func_array( [ $hoistedErrors, 'fatal' ], $error );
611 $hoistedErrors->fatal( 'htmlform-invalid-input' );
614 $this->mWasSubmitted
= true;
616 # Check for cancelled submission
617 foreach ( $this->mFlatFields
as $fieldname => $field ) {
618 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
621 if ( $field->cancelSubmit( $this->mFieldData
[$fieldname], $this->mFieldData
) ) {
622 $this->mWasSubmitted
= false;
627 # Check for validation
628 foreach ( $this->mFlatFields
as $fieldname => $field ) {
629 if ( !array_key_exists( $fieldname, $this->mFieldData
) ) {
632 if ( $field->isHidden( $this->mFieldData
) ) {
635 $res = $field->validate( $this->mFieldData
[$fieldname], $this->mFieldData
);
636 if ( $res !== true ) {
638 if ( $res !== false && !$field->canDisplayErrors() ) {
639 if ( is_string( $res ) ) {
640 $hoistedErrors->fatal( 'rawmessage', $res );
642 $hoistedErrors->fatal( $res );
649 return $hoistedErrors;
652 $callback = $this->mSubmitCallback
;
653 if ( !is_callable( $callback ) ) {
654 throw new MWException( 'HTMLForm: no submit callback provided. Use ' .
655 'setSubmitCallback() to set one.' );
658 $data = $this->filterDataForSubmit( $this->mFieldData
);
660 $res = call_user_func( $callback, $data, $this );
661 if ( $res === false ) {
662 $this->mWasSubmitted
= false;
669 * Test whether the form was considered to have been submitted or not, i.e.
670 * whether the last call to tryAuthorizedSubmit or trySubmit returned
673 * This will return false until HTMLForm::tryAuthorizedSubmit or
674 * HTMLForm::trySubmit is called.
679 public function wasSubmitted() {
680 return $this->mWasSubmitted
;
684 * Set a callback to a function to do something with the form
685 * once it's been successfully validated.
687 * @param callable $cb The function will be passed the output from
688 * HTMLForm::filterDataForSubmit and this HTMLForm object, and must
689 * return as documented for HTMLForm::trySubmit
691 * @return HTMLForm $this for chaining calls (since 1.20)
693 public function setSubmitCallback( $cb ) {
694 $this->mSubmitCallback
= $cb;
700 * Set a message to display on a validation error.
702 * @param string|array $msg String or Array of valid inputs to wfMessage()
703 * (so each entry can be either a String or Array)
705 * @return HTMLForm $this for chaining calls (since 1.20)
707 public function setValidationErrorMessage( $msg ) {
708 $this->mValidationErrorMessage
= $msg;
714 * Set the introductory message, overwriting any existing message.
716 * @param string $msg Complete text of message to display
718 * @return HTMLForm $this for chaining calls (since 1.20)
720 public function setIntro( $msg ) {
721 $this->setPreText( $msg );
727 * Set the introductory message HTML, overwriting any existing message.
730 * @param string $msg Complete HTML of message to display
732 * @return HTMLForm $this for chaining calls (since 1.20)
734 public function setPreText( $msg ) {
741 * Add HTML to introductory message.
743 * @param string $msg Complete HTML of message to display
745 * @return HTMLForm $this for chaining calls (since 1.20)
747 public function addPreText( $msg ) {
754 * Add HTML to the header, inside the form.
756 * @param string $msg Additional HTML to display in header
757 * @param string|null $section The section to add the header to
759 * @return HTMLForm $this for chaining calls (since 1.20)
761 public function addHeaderText( $msg, $section = null ) {
762 if ( $section === null ) {
763 $this->mHeader
.= $msg;
765 if ( !isset( $this->mSectionHeaders
[$section] ) ) {
766 $this->mSectionHeaders
[$section] = '';
768 $this->mSectionHeaders
[$section] .= $msg;
775 * Set header text, inside the form.
778 * @param string $msg Complete HTML of header to display
779 * @param string|null $section The section to add the header to
781 * @return HTMLForm $this for chaining calls (since 1.20)
783 public function setHeaderText( $msg, $section = null ) {
784 if ( $section === null ) {
785 $this->mHeader
= $msg;
787 $this->mSectionHeaders
[$section] = $msg;
796 * @param string|null $section The section to get the header text for
798 * @return string HTML
800 public function getHeaderText( $section = null ) {
801 if ( $section === null ) {
802 return $this->mHeader
;
804 return isset( $this->mSectionHeaders
[$section] ) ?
$this->mSectionHeaders
[$section] : '';
809 * Add footer text, inside the form.
811 * @param string $msg Complete text of message to display
812 * @param string|null $section The section to add the footer text to
814 * @return HTMLForm $this for chaining calls (since 1.20)
816 public function addFooterText( $msg, $section = null ) {
817 if ( $section === null ) {
818 $this->mFooter
.= $msg;
820 if ( !isset( $this->mSectionFooters
[$section] ) ) {
821 $this->mSectionFooters
[$section] = '';
823 $this->mSectionFooters
[$section] .= $msg;
830 * Set footer text, inside the form.
833 * @param string $msg Complete text of message to display
834 * @param string|null $section The section to add the footer text to
836 * @return HTMLForm $this for chaining calls (since 1.20)
838 public function setFooterText( $msg, $section = null ) {
839 if ( $section === null ) {
840 $this->mFooter
= $msg;
842 $this->mSectionFooters
[$section] = $msg;
851 * @param string|null $section The section to get the footer text for
855 public function getFooterText( $section = null ) {
856 if ( $section === null ) {
857 return $this->mFooter
;
859 return isset( $this->mSectionFooters
[$section] ) ?
$this->mSectionFooters
[$section] : '';
864 * Add text to the end of the display.
866 * @param string $msg Complete text of message to display
868 * @return HTMLForm $this for chaining calls (since 1.20)
870 public function addPostText( $msg ) {
871 $this->mPost
.= $msg;
877 * Set text at the end of the display.
879 * @param string $msg Complete text of message to display
881 * @return HTMLForm $this for chaining calls (since 1.20)
883 public function setPostText( $msg ) {
890 * Add a hidden field to the output
892 * @param string $name Field name. This will be used exactly as entered
893 * @param string $value Field value
894 * @param array $attribs
896 * @return HTMLForm $this for chaining calls (since 1.20)
898 public function addHiddenField( $name, $value, array $attribs = [] ) {
899 $attribs +
= [ 'name' => $name ];
900 $this->mHiddenFields
[] = [ $value, $attribs ];
906 * Add an array of hidden fields to the output
910 * @param array $fields Associative array of fields to add;
911 * mapping names to their values
913 * @return HTMLForm $this for chaining calls
915 public function addHiddenFields( array $fields ) {
916 foreach ( $fields as $name => $value ) {
917 $this->mHiddenFields
[] = [ $value, [ 'name' => $name ] ];
924 * Add a button to the form
926 * @since 1.27 takes an array as shown. Earlier versions accepted
927 * 'name', 'value', 'id', and 'attribs' as separate parameters in that
929 * @note Custom labels ('label', 'label-message', 'label-raw') are not
930 * supported for IE6 and IE7 due to bugs in those browsers. If detected,
931 * they will be served buttons using 'value' as the button label.
932 * @param array $data Data to define the button:
933 * - name: (string) Button name.
934 * - value: (string) Button value.
935 * - label-message: (string, optional) Button label message key to use
936 * instead of 'value'. Overrides 'label' and 'label-raw'.
937 * - label: (string, optional) Button label text to use instead of
938 * 'value'. Overrides 'label-raw'.
939 * - label-raw: (string, optional) Button label HTML to use instead of
941 * - id: (string, optional) DOM id for the button.
942 * - attribs: (array, optional) Additional HTML attributes.
943 * - flags: (string|string[], optional) OOUI flags.
944 * - framed: (boolean=true, optional) OOUI framed attribute.
945 * @return HTMLForm $this for chaining calls (since 1.20)
947 public function addButton( $data ) {
948 if ( !is_array( $data ) ) {
949 $args = func_get_args();
950 if ( count( $args ) < 2 ||
count( $args ) > 4 ) {
951 throw new InvalidArgumentException(
952 'Incorrect number of arguments for deprecated calling style'
958 'id' => isset( $args[2] ) ?
$args[2] : null,
959 'attribs' => isset( $args[3] ) ?
$args[3] : null,
962 if ( !isset( $data['name'] ) ) {
963 throw new InvalidArgumentException( 'A name is required' );
965 if ( !isset( $data['value'] ) ) {
966 throw new InvalidArgumentException( 'A value is required' );
969 $this->mButtons
[] = $data +
[
980 * Set the salt for the edit token.
982 * Only useful when the method is "post".
985 * @param string|array $salt Salt to use
986 * @return HTMLForm $this For chaining calls
988 public function setTokenSalt( $salt ) {
989 $this->mTokenSalt
= $salt;
995 * Display the form (sending to the context's OutputPage object), with an
996 * appropriate error message or stack of messages, and any validation errors, etc.
998 * @warning You should call prepareForm() before calling this function.
999 * Moreover, when doing method chaining this should be the very last method
1000 * call just after prepareForm().
1002 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1004 * @return void Nothing, should be last call
1006 public function displayForm( $submitResult ) {
1007 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1011 * Returns the raw HTML generated by the form
1013 * @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
1015 * @return string HTML
1017 public function getHTML( $submitResult ) {
1018 # For good measure (it is the default)
1019 $this->getOutput()->preventClickjacking();
1020 $this->getOutput()->addModules( 'mediawiki.htmlform' );
1021 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.styles' );
1024 . $this->getErrorsOrWarnings( $submitResult, 'error' )
1025 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1026 . $this->getHeaderText()
1028 . $this->getHiddenFields()
1029 . $this->getButtons()
1030 . $this->getFooterText();
1032 $html = $this->wrapForm( $html );
1034 return '' . $this->mPre
. $html . $this->mPost
;
1038 * Get HTML attributes for the `<form>` tag.
1041 protected function getFormAttributes() {
1042 # Use multipart/form-data
1043 $encType = $this->mUseMultipart
1044 ?
'multipart/form-data'
1045 : 'application/x-www-form-urlencoded';
1048 'class' => 'mw-htmlform',
1049 'action' => $this->getAction(),
1050 'method' => $this->getMethod(),
1051 'enctype' => $encType,
1054 $attribs['id'] = $this->mId
;
1056 if ( is_string( $this->mAutocomplete
) ) {
1057 $attribs['autocomplete'] = $this->mAutocomplete
;
1059 if ( $this->mName
) {
1060 $attribs['name'] = $this->mName
;
1062 if ( $this->needsJSForHtml5FormValidation() ) {
1063 $attribs['novalidate'] = true;
1069 * Wrap the form innards in an actual "<form>" element
1071 * @param string $html HTML contents to wrap.
1073 * @return string Wrapped HTML.
1075 public function wrapForm( $html ) {
1076 # Include a <fieldset> wrapper for style, if requested.
1077 if ( $this->mWrapperLegend
!== false ) {
1078 $legend = is_string( $this->mWrapperLegend
) ?
$this->mWrapperLegend
: false;
1079 $html = Xml
::fieldset( $legend, $html );
1082 return Html
::rawElement(
1084 $this->getFormAttributes(),
1090 * Get the hidden fields that should go inside the form.
1091 * @return string HTML.
1093 public function getHiddenFields() {
1095 if ( $this->mFormIdentifier
!== null ) {
1096 $html .= Html
::hidden(
1098 $this->mFormIdentifier
1101 if ( $this->getMethod() === 'post' ) {
1102 $html .= Html
::hidden(
1104 $this->getUser()->getEditToken( $this->mTokenSalt
),
1105 [ 'id' => 'wpEditToken' ]
1107 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1110 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1111 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1112 $html .= Html
::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1115 foreach ( $this->mHiddenFields
as $data ) {
1116 list( $value, $attribs ) = $data;
1117 $html .= Html
::hidden( $attribs['name'], $value, $attribs ) . "\n";
1124 * Get the submit and (potentially) reset buttons.
1125 * @return string HTML.
1127 public function getButtons() {
1129 $useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
1131 if ( $this->mShowSubmit
) {
1134 if ( isset( $this->mSubmitID
) ) {
1135 $attribs['id'] = $this->mSubmitID
;
1138 if ( isset( $this->mSubmitName
) ) {
1139 $attribs['name'] = $this->mSubmitName
;
1142 if ( isset( $this->mSubmitTooltip
) ) {
1143 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
1146 $attribs['class'] = [ 'mw-htmlform-submit' ];
1148 if ( $useMediaWikiUIEverywhere ) {
1149 foreach ( $this->mSubmitFlags
as $flag ) {
1150 $attribs['class'][] = 'mw-ui-' . $flag;
1152 $attribs['class'][] = 'mw-ui-button';
1155 $buttons .= Xml
::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1158 if ( $this->mShowReset
) {
1159 $buttons .= Html
::element(
1163 'value' => $this->msg( 'htmlform-reset' )->text(),
1164 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1169 if ( $this->mShowCancel
) {
1170 $target = $this->mCancelTarget ?
: Title
::newMainPage();
1171 if ( $target instanceof Title
) {
1172 $target = $target->getLocalURL();
1174 $buttons .= Html
::element(
1177 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' : null,
1180 $this->msg( 'cancel' )->text()
1184 // IE<8 has bugs with <button>, so we'll need to avoid them.
1185 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
1187 foreach ( $this->mButtons
as $button ) {
1190 'name' => $button['name'],
1191 'value' => $button['value']
1194 if ( isset( $button['label-message'] ) ) {
1195 $label = $this->getMessage( $button['label-message'] )->parse();
1196 } elseif ( isset( $button['label'] ) ) {
1197 $label = htmlspecialchars( $button['label'] );
1198 } elseif ( isset( $button['label-raw'] ) ) {
1199 $label = $button['label-raw'];
1201 $label = htmlspecialchars( $button['value'] );
1204 if ( $button['attribs'] ) {
1205 $attrs +
= $button['attribs'];
1208 if ( isset( $button['id'] ) ) {
1209 $attrs['id'] = $button['id'];
1212 if ( $useMediaWikiUIEverywhere ) {
1213 $attrs['class'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : [];
1214 $attrs['class'][] = 'mw-ui-button';
1218 $buttons .= Html
::element( 'input', $attrs ) . "\n";
1220 $buttons .= Html
::rawElement( 'button', $attrs, $label ) . "\n";
1228 return Html
::rawElement( 'span',
1229 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1233 * Get the whole body of the form.
1236 public function getBody() {
1237 return $this->displaySection( $this->mFieldTree
, $this->mTableId
);
1241 * Format and display an error message stack.
1243 * @param string|array|Status $errors
1245 * @deprecated since 1.28, use getErrorsOrWarnings() instead
1249 public function getErrors( $errors ) {
1250 wfDeprecated( __METHOD__
);
1251 return $this->getErrorsOrWarnings( $errors, 'error' );
1255 * Returns a formatted list of errors or warnings from the given elements.
1257 * @param string|array|Status $elements The set of errors/warnings to process.
1258 * @param string $elementsType Should warnings or errors be returned. This is meant
1259 * for Status objects, all other valid types are always considered as errors.
1262 public function getErrorsOrWarnings( $elements, $elementsType ) {
1263 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1264 throw new DomainException( $elementsType . ' is not a valid type.' );
1266 $elementstr = false;
1267 if ( $elements instanceof Status
) {
1268 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1269 $status = $elementsType === 'error' ?
$errorStatus : $warningStatus;
1270 if ( $status->isGood() ) {
1273 $elementstr = $this->getOutput()->parse(
1274 $status->getWikiText()
1277 } elseif ( is_array( $elements ) && $elementsType === 'error' ) {
1278 $elementstr = $this->formatErrors( $elements );
1279 } elseif ( $elementsType === 'error' ) {
1280 $elementstr = $elements;
1284 ? Html
::rawElement( 'div', [ 'class' => $elementsType ], $elementstr )
1289 * Format a stack of error messages into a single HTML string
1291 * @param array $errors Array of message keys/values
1293 * @return string HTML, a "<ul>" list of errors
1295 public function formatErrors( $errors ) {
1298 foreach ( $errors as $error ) {
1299 $errorstr .= Html
::rawElement(
1302 $this->getMessage( $error )->parse()
1306 $errorstr = Html
::rawElement( 'ul', [], $errorstr );
1312 * Set the text for the submit button
1314 * @param string $t Plaintext
1316 * @return HTMLForm $this for chaining calls (since 1.20)
1318 public function setSubmitText( $t ) {
1319 $this->mSubmitText
= $t;
1325 * Identify that the submit button in the form has a destructive action
1328 * @return HTMLForm $this for chaining calls (since 1.28)
1330 public function setSubmitDestructive() {
1331 $this->mSubmitFlags
= [ 'destructive', 'primary' ];
1337 * Identify that the submit button in the form has a progressive action
1340 * @return HTMLForm $this for chaining calls (since 1.28)
1342 public function setSubmitProgressive() {
1343 $this->mSubmitFlags
= [ 'progressive', 'primary' ];
1349 * Set the text for the submit button to a message
1352 * @param string|Message $msg Message key or Message object
1354 * @return HTMLForm $this for chaining calls (since 1.20)
1356 public function setSubmitTextMsg( $msg ) {
1357 if ( !$msg instanceof Message
) {
1358 $msg = $this->msg( $msg );
1360 $this->setSubmitText( $msg->text() );
1366 * Get the text for the submit button, either customised or a default.
1369 public function getSubmitText() {
1370 return $this->mSubmitText ?
: $this->msg( 'htmlform-submit' )->text();
1374 * @param string $name Submit button name
1376 * @return HTMLForm $this for chaining calls (since 1.20)
1378 public function setSubmitName( $name ) {
1379 $this->mSubmitName
= $name;
1385 * @param string $name Tooltip for the submit button
1387 * @return HTMLForm $this for chaining calls (since 1.20)
1389 public function setSubmitTooltip( $name ) {
1390 $this->mSubmitTooltip
= $name;
1396 * Set the id for the submit button.
1400 * @todo FIXME: Integrity of $t is *not* validated
1401 * @return HTMLForm $this for chaining calls (since 1.20)
1403 public function setSubmitID( $t ) {
1404 $this->mSubmitID
= $t;
1410 * Set an internal identifier for this form. It will be submitted as a hidden form field, allowing
1411 * HTMLForm to determine whether the form was submitted (or merely viewed). Setting this serves
1414 * - If you use two or more forms on one page, it allows HTMLForm to identify which of the forms
1415 * was submitted, and not attempt to validate the other ones.
1416 * - If you use checkbox or multiselect fields inside a form using the GET method, it allows
1417 * HTMLForm to distinguish between the initial page view and a form submission with all
1418 * checkboxes or select options unchecked.
1421 * @param string $ident
1424 public function setFormIdentifier( $ident ) {
1425 $this->mFormIdentifier
= $ident;
1431 * Stop a default submit button being shown for this form. This implies that an
1432 * alternate submit method must be provided manually.
1436 * @param bool $suppressSubmit Set to false to re-enable the button again
1438 * @return HTMLForm $this for chaining calls
1440 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1441 $this->mShowSubmit
= !$suppressSubmit;
1447 * Show a cancel button (or prevent it). The button is not shown by default.
1449 * @return HTMLForm $this for chaining calls
1452 public function showCancel( $show = true ) {
1453 $this->mShowCancel
= $show;
1458 * Sets the target where the user is redirected to after clicking cancel.
1459 * @param Title|string $target Target as a Title object or an URL
1460 * @return HTMLForm $this for chaining calls
1463 public function setCancelTarget( $target ) {
1464 $this->mCancelTarget
= $target;
1469 * Set the id of the \<table\> or outermost \<div\> element.
1473 * @param string $id New value of the id attribute, or "" to remove
1475 * @return HTMLForm $this for chaining calls
1477 public function setTableId( $id ) {
1478 $this->mTableId
= $id;
1484 * @param string $id DOM id for the form
1486 * @return HTMLForm $this for chaining calls (since 1.20)
1488 public function setId( $id ) {
1495 * @param string $name 'name' attribute for the form
1496 * @return HTMLForm $this for chaining calls
1498 public function setName( $name ) {
1499 $this->mName
= $name;
1505 * Prompt the whole form to be wrapped in a "<fieldset>", with
1506 * this text as its "<legend>" element.
1508 * @param string|bool $legend If false, no wrapper or legend will be displayed.
1509 * If true, a wrapper will be displayed, but no legend.
1510 * If a string, a wrapper will be displayed with that string as a legend.
1511 * The string will be escaped before being output (this doesn't support HTML).
1513 * @return HTMLForm $this for chaining calls (since 1.20)
1515 public function setWrapperLegend( $legend ) {
1516 $this->mWrapperLegend
= $legend;
1522 * Prompt the whole form to be wrapped in a "<fieldset>", with
1523 * this message as its "<legend>" element.
1526 * @param string|Message $msg Message key or Message object
1528 * @return HTMLForm $this for chaining calls (since 1.20)
1530 public function setWrapperLegendMsg( $msg ) {
1531 if ( !$msg instanceof Message
) {
1532 $msg = $this->msg( $msg );
1534 $this->setWrapperLegend( $msg->text() );
1540 * Set the prefix for various default messages
1541 * @todo Currently only used for the "<fieldset>" legend on forms
1542 * with multiple sections; should be used elsewhere?
1546 * @return HTMLForm $this for chaining calls (since 1.20)
1548 public function setMessagePrefix( $p ) {
1549 $this->mMessagePrefix
= $p;
1555 * Set the title for form submission
1557 * @param Title $t Title of page the form is on/should be posted to
1559 * @return HTMLForm $this for chaining calls (since 1.20)
1561 public function setTitle( $t ) {
1571 public function getTitle() {
1572 return $this->mTitle
=== false
1573 ?
$this->getContext()->getTitle()
1578 * Set the method used to submit the form
1580 * @param string $method
1582 * @return HTMLForm $this for chaining calls (since 1.20)
1584 public function setMethod( $method = 'post' ) {
1585 $this->mMethod
= strtolower( $method );
1591 * @return string Always lowercase
1593 public function getMethod() {
1594 return $this->mMethod
;
1598 * Wraps the given $section into an user-visible fieldset.
1600 * @param string $legend Legend text for the fieldset
1601 * @param string $section The section content in plain Html
1602 * @param array $attributes Additional attributes for the fieldset
1603 * @return string The fieldset's Html
1605 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
1606 return Xml
::fieldset( $legend, $section, $attributes ) . "\n";
1612 * @param array[]|HTMLFormField[] $fields Array of fields (either arrays or
1614 * @param string $sectionName ID attribute of the "<table>" tag for this
1615 * section, ignored if empty.
1616 * @param string $fieldsetIDPrefix ID prefix for the "<fieldset>" tag of
1617 * each subsection, ignored if empty.
1618 * @param bool &$hasUserVisibleFields Whether the section had user-visible fields.
1619 * @throws LogicException When called on uninitialized field data, e.g. When
1620 * HTMLForm::displayForm was called without calling HTMLForm::prepareForm
1625 public function displaySection( $fields,
1627 $fieldsetIDPrefix = '',
1628 &$hasUserVisibleFields = false
1630 if ( $this->mFieldData
=== null ) {
1631 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1632 . 'You probably called displayForm() without calling prepareForm() first.' );
1635 $displayFormat = $this->getDisplayFormat();
1638 $subsectionHtml = '';
1641 // Conveniently, PHP method names are case-insensitive.
1642 // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
1643 $getFieldHtmlMethod = $displayFormat === 'table' ?
'getTableRow' : ( 'get' . $displayFormat );
1645 foreach ( $fields as $key => $value ) {
1646 if ( $value instanceof HTMLFormField
) {
1647 $v = array_key_exists( $key, $this->mFieldData
)
1648 ?
$this->mFieldData
[$key]
1649 : $value->getDefault();
1651 $retval = $value->$getFieldHtmlMethod( $v );
1653 // check, if the form field should be added to
1655 if ( $value->hasVisibleOutput() ) {
1658 $labelValue = trim( $value->getLabel() );
1659 if ( $labelValue !== ' ' && $labelValue !== '' ) {
1663 $hasUserVisibleFields = true;
1665 } elseif ( is_array( $value ) ) {
1666 $subsectionHasVisibleFields = false;
1668 $this->displaySection( $value,
1670 "$fieldsetIDPrefix$key-",
1671 $subsectionHasVisibleFields );
1674 if ( $subsectionHasVisibleFields === true ) {
1675 // Display the section with various niceties.
1676 $hasUserVisibleFields = true;
1678 $legend = $this->getLegend( $key );
1680 $section = $this->getHeaderText( $key ) .
1682 $this->getFooterText( $key );
1685 if ( $fieldsetIDPrefix ) {
1686 $attributes['id'] = Sanitizer
::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1688 $subsectionHtml .= $this->wrapFieldSetSection( $legend, $section, $attributes );
1690 // Just return the inputs, nothing fancy.
1691 $subsectionHtml .= $section;
1696 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1698 if ( $subsectionHtml ) {
1699 if ( $this->mSubSectionBeforeFields
) {
1700 return $subsectionHtml . "\n" . $html;
1702 return $html . "\n" . $subsectionHtml;
1710 * Put a form section together from the individual fields' HTML, merging it and wrapping.
1711 * @param array $fieldsHtml
1712 * @param string $sectionName
1713 * @param bool $anyFieldHasLabel
1714 * @return string HTML
1716 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1717 if ( !$fieldsHtml ) {
1718 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
1719 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
1723 $displayFormat = $this->getDisplayFormat();
1724 $html = implode( '', $fieldsHtml );
1726 if ( $displayFormat === 'raw' ) {
1732 if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
1733 $classes[] = 'mw-htmlform-nolabel';
1737 'class' => implode( ' ', $classes ),
1740 if ( $sectionName ) {
1741 $attribs['id'] = Sanitizer
::escapeIdForAttribute( $sectionName );
1744 if ( $displayFormat === 'table' ) {
1745 return Html
::rawElement( 'table',
1747 Html
::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
1748 } elseif ( $displayFormat === 'inline' ) {
1749 return Html
::rawElement( 'span', $attribs, "\n$html\n" );
1751 return Html
::rawElement( 'div', $attribs, "\n$html\n" );
1756 * Construct the form fields from the Descriptor array
1758 public function loadData() {
1761 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1762 $request = $this->getRequest();
1763 if ( $field->skipLoadData( $request ) ) {
1765 } elseif ( !empty( $field->mParams
['disabled'] ) ) {
1766 $fieldData[$fieldname] = $field->getDefault();
1768 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1773 foreach ( $fieldData as $name => &$value ) {
1774 $field = $this->mFlatFields
[$name];
1775 $value = $field->filter( $value, $this->mFlatFields
);
1778 $this->mFieldData
= $fieldData;
1782 * Stop a reset button being shown for this form
1784 * @param bool $suppressReset Set to false to re-enable the button again
1786 * @return HTMLForm $this for chaining calls (since 1.20)
1788 public function suppressReset( $suppressReset = true ) {
1789 $this->mShowReset
= !$suppressReset;
1795 * Overload this if you want to apply special filtration routines
1796 * to the form as a whole, after it's submitted but before it's
1799 * @param array $data
1803 public function filterDataForSubmit( $data ) {
1808 * Get a string to go in the "<legend>" of a section fieldset.
1809 * Override this if you want something more complicated.
1811 * @param string $key
1815 public function getLegend( $key ) {
1816 return $this->msg( "{$this->mMessagePrefix}-$key" )->text();
1820 * Set the value for the action attribute of the form.
1821 * When set to false (which is the default state), the set title is used.
1825 * @param string|bool $action
1827 * @return HTMLForm $this for chaining calls (since 1.20)
1829 public function setAction( $action ) {
1830 $this->mAction
= $action;
1836 * Get the value for the action attribute of the form.
1842 public function getAction() {
1843 // If an action is alredy provided, return it
1844 if ( $this->mAction
!== false ) {
1845 return $this->mAction
;
1848 $articlePath = $this->getConfig()->get( 'ArticlePath' );
1849 // Check whether we are in GET mode and the ArticlePath contains a "?"
1850 // meaning that getLocalURL() would return something like "index.php?title=...".
1851 // As browser remove the query string before submitting GET forms,
1852 // it means that the title would be lost. In such case use wfScript() instead
1853 // and put title in an hidden field (see getHiddenFields()).
1854 if ( strpos( $articlePath, '?' ) !== false && $this->getMethod() === 'get' ) {
1858 return $this->getTitle()->getLocalURL();
1862 * Set the value for the autocomplete attribute of the form. A typical value is "off".
1863 * When set to null (which is the default state), the attribute get not set.
1867 * @param string|null $autocomplete
1869 * @return HTMLForm $this for chaining calls
1871 public function setAutocomplete( $autocomplete ) {
1872 $this->mAutocomplete
= $autocomplete;
1878 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1879 * name + parameters array) into a Message.
1880 * @param mixed $value
1883 protected function getMessage( $value ) {
1884 return Message
::newFromSpecifier( $value )->setContext( $this );
1888 * Whether this form, with its current fields, requires the user agent to have JavaScript enabled
1889 * for the client-side HTML5 form validation to work correctly. If this function returns true, a
1890 * 'novalidate' attribute will be added on the `<form>` element. It will be removed if the user
1891 * agent has JavaScript support, in htmlform.js.
1896 public function needsJSForHtml5FormValidation() {
1897 foreach ( $this->mFlatFields
as $fieldname => $field ) {
1898 if ( $field->needsJSForHtml5FormValidation() ) {