134 'api' => HTMLApiField::class,
135 'text' => HTMLTextField::class,
136 'textwithbutton' => HTMLTextFieldWithButton::class,
137 'textarea' => HTMLTextAreaField::class,
138 'select' => HTMLSelectField::class,
139 'combobox' => HTMLComboboxField::class,
140 'radio' => HTMLRadioField::class,
141 'multiselect' => HTMLMultiSelectField::class,
142 'limitselect' => HTMLSelectLimitField::class,
143 'check' => HTMLCheckField::class,
144 'toggle' => HTMLCheckField::class,
145 'int' => HTMLIntField::class,
146 'float' => HTMLFloatField::class,
147 'info' => HTMLInfoField::class,
148 'selectorother' => HTMLSelectOrOtherField::class,
149 'selectandother' => HTMLSelectAndOtherField::class,
150 'namespaceselect' => HTMLSelectNamespace::class,
151 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
152 'tagfilter' => HTMLTagFilter::class,
153 'sizefilter' => HTMLSizeFilterField::class,
154 'submit' => HTMLSubmitField::class,
155 'hidden' => HTMLHiddenField::class,
156 'edittools' => HTMLEditTools::class,
157 'checkmatrix' => HTMLCheckMatrix::class,
158 'cloner' => HTMLFormFieldCloner::class,
159 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
160 'language' => HTMLSelectLanguageField::class,
161 'date' => HTMLDateTimeField::class,
162 'time' => HTMLDateTimeField::class,
163 'datetime' => HTMLDateTimeField::class,
164 'expiry' => HTMLExpiryField::class,
168 'email' => HTMLTextField::class,
169 'password' => HTMLTextField::class,
170 'url' => HTMLTextField::class,
171 'title' => HTMLTitleTextField::class,
172 'user' => HTMLUserTextField::class,
173 'usersmultiselect' => HTMLUsersMultiselectField::class,
174 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
175 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
303 public static function factory( $displayFormat, ...$arguments ) {
310 $form =
new self( ...$arguments );
330 $this->mTitle =
false;
331 $this->mMessagePrefix = $messagePrefix;
332 } elseif (
$context ===
null && $messagePrefix !==
'' ) {
333 $this->mMessagePrefix = $messagePrefix;
334 } elseif ( is_string(
$context ) && $messagePrefix ===
'' ) {
342 !$this->
getConfig()->
get(
'HTMLFormAllowTableFormat' )
343 && $this->displayFormat ===
'table'
345 $this->displayFormat =
'div';
361 $loadedDescriptor = [];
363 foreach ( $descriptor as $fieldname => $info ) {
365 $section = $info[
'section'] ??
'';
367 if ( isset( $info[
'type'] ) && $info[
'type'] ===
'file' ) {
368 $this->mUseMultipart =
true;
371 $field = static::loadInputFromParameters( $fieldname, $info, $this );
373 $setSection =& $loadedDescriptor;
375 foreach ( explode(
'/', $section ) as $newName ) {
376 if ( !isset( $setSection[$newName] ) ) {
377 $setSection[$newName] = [];
380 $setSection =& $setSection[$newName];
384 $setSection[$fieldname] = $field;
385 $this->mFlatFields[$fieldname] = $field;
388 $this->mFieldTree = array_merge( $this->mFieldTree, $loadedDescriptor );
398 return isset( $this->mFlatFields[$fieldname] );
407 if ( !$this->
hasField( $fieldname ) ) {
408 throw new DomainException( __METHOD__ .
': no field named ' . $fieldname );
410 return $this->mFlatFields[$fieldname];
425 in_array( $format, $this->availableSubclassDisplayFormats,
true ) ||
426 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats,
true )
428 throw new MWException(
'Cannot change display format after creation, ' .
429 'use HTMLForm::factory() instead' );
432 if ( !in_array( $format, $this->availableDisplayFormats,
true ) ) {
433 throw new MWException(
'Display format must be one of ' .
436 $this->availableDisplayFormats,
437 $this->availableSubclassDisplayFormats
444 if ( !$this->
getConfig()->
get(
'HTMLFormAllowTableFormat' ) && $format ===
'table' ) {
448 $this->displayFormat = $format;
459 return $this->displayFormat;
480 if ( isset( $descriptor[
'class'] ) ) {
481 $class = $descriptor[
'class'];
482 } elseif ( isset( $descriptor[
'type'] ) ) {
483 $class = static::$typeMappings[$descriptor[
'type']];
484 $descriptor[
'class'] = $class;
490 throw new MWException(
"Descriptor with no class for $fieldname: "
491 . print_r( $descriptor,
true ) );
511 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
513 $descriptor[
'fieldname'] = $fieldname;
515 $descriptor[
'parent'] = $parent;
518 # @todo This will throw a fatal error whenever someone try to use
519 # 'class' to feed a CSS class instead of 'cssclass'. Would be
520 # great to avoid the fatal error and show a nice error.
521 return new $class( $descriptor );
534 # Check if we have the info we need
535 if ( !$this->mTitle instanceof
Title && $this->mTitle !==
false ) {
536 throw new MWException(
'You must call setTitle() on an HTMLForm' );
539 # Load data from the request.
541 $this->mFormIdentifier ===
null ||
542 $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier
546 $this->mFieldData = [];
559 if ( $this->mFormIdentifier ===
null ) {
562 $identOkay = $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier;
568 } elseif ( $this->
getRequest()->wasPosted() ) {
569 $editToken = $this->
getRequest()->getVal(
'wpEditToken' );
570 if ( $this->
getUser()->isLoggedIn() || $editToken !==
null ) {
574 $tokenOkay = $this->
getUser()->matchEditToken( $editToken, $this->mTokenSalt );
580 if ( $tokenOkay && $identOkay ) {
581 $this->mWasSubmitted =
true;
598 if ( $result ===
true || ( $result instanceof
Status && $result->
isGood() ) ) {
635 $hoistedErrors = Status::newGood();
636 if ( $this->mValidationErrorMessage ) {
637 foreach ( $this->mValidationErrorMessage as $error ) {
638 $hoistedErrors->fatal( ...$error );
641 $hoistedErrors->fatal(
'htmlform-invalid-input' );
644 $this->mWasSubmitted =
true;
646 # Check for cancelled submission
647 foreach ( $this->mFlatFields as $fieldname => $field ) {
648 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
651 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
652 $this->mWasSubmitted =
false;
657 # Check for validation
658 foreach ( $this->mFlatFields as $fieldname => $field ) {
659 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
662 if ( $field->isHidden( $this->mFieldData ) ) {
665 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
666 if (
$res !==
true ) {
668 if (
$res !==
false && !$field->canDisplayErrors() ) {
669 if ( is_string(
$res ) ) {
670 $hoistedErrors->fatal(
'rawmessage',
$res );
672 $hoistedErrors->fatal(
$res );
679 return $hoistedErrors;
682 $callback = $this->mSubmitCallback;
683 if ( !is_callable( $callback ) ) {
684 throw new MWException(
'HTMLForm: no submit callback provided. Use ' .
685 'setSubmitCallback() to set one.' );
690 $res = call_user_func( $callback, $data, $this );
691 if (
$res ===
false ) {
692 $this->mWasSubmitted =
false;
710 return $this->mWasSubmitted;
724 $this->mSubmitCallback = $cb;
738 $this->mValidationErrorMessage = $msg;
803 if ( $section ===
null ) {
804 $this->mHeader .= $msg;
806 if ( !isset( $this->mSectionHeaders[$section] ) ) {
807 $this->mSectionHeaders[$section] =
'';
809 $this->mSectionHeaders[$section] .= $msg;
825 if ( $section ===
null ) {
826 $this->mHeader = $msg;
828 $this->mSectionHeaders[$section] = $msg;
842 if ( $section ===
null ) {
843 return $this->mHeader;
845 return $this->mSectionHeaders[$section] ??
'';
858 if ( $section ===
null ) {
859 $this->mFooter .= $msg;
861 if ( !isset( $this->mSectionFooters[$section] ) ) {
862 $this->mSectionFooters[$section] =
'';
864 $this->mSectionFooters[$section] .= $msg;
880 if ( $section ===
null ) {
881 $this->mFooter = $msg;
883 $this->mSectionFooters[$section] = $msg;
897 if ( $section ===
null ) {
898 return $this->mFooter;
900 return $this->mSectionFooters[$section] ??
'';
912 $this->mPost .= $msg;
940 $attribs += [
'name' => $name ];
941 $this->mHiddenFields[] = [ $value, $attribs ];
957 foreach ( $fields as $name => $value ) {
958 $this->mHiddenFields[] = [ $value, [
'name' => $name ] ];
992 if ( !is_array( $data ) ) {
993 $args = func_get_args();
994 if ( count(
$args ) < 2 || count(
$args ) > 4 ) {
995 throw new InvalidArgumentException(
996 'Incorrect number of arguments for deprecated calling style'
1001 'value' =>
$args[1],
1002 'id' =>
$args[2] ??
null,
1003 'attribs' =>
$args[3] ??
null,
1006 if ( !isset( $data[
'name'] ) ) {
1007 throw new InvalidArgumentException(
'A name is required' );
1009 if ( !isset( $data[
'value'] ) ) {
1010 throw new InvalidArgumentException(
'A value is required' );
1013 $this->mButtons[] = $data + [
1033 $this->mTokenSalt = $salt;
1063 # For good measure (it is the default)
1064 $this->
getOutput()->preventClickjacking();
1065 $this->
getOutput()->addModules(
'mediawiki.htmlform' );
1066 $this->
getOutput()->addModuleStyles(
'mediawiki.htmlform.styles' );
1079 return '' . $this->mPre . $html . $this->mPost;
1090 $this->mCollapsible =
true;
1091 $this->mCollapsed = $collapsedByDefault;
1100 # Use multipart/form-data
1101 $encType = $this->mUseMultipart
1102 ?
'multipart/form-data'
1103 :
'application/x-www-form-urlencoded';
1106 'class' =>
'mw-htmlform',
1109 'enctype' => $encType,
1112 $attribs[
'id'] = $this->mId;
1114 if ( is_string( $this->mAutocomplete ) ) {
1115 $attribs[
'autocomplete'] = $this->mAutocomplete;
1117 if ( $this->mName ) {
1118 $attribs[
'name'] = $this->mName;
1121 $attribs[
'novalidate'] =
true;
1134 # Include a <fieldset> wrapper for style, if requested.
1135 if ( $this->mWrapperLegend !==
false ) {
1136 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend :
false;
1137 $html = Xml::fieldset( $legend, $html, $this->mWrapperAttributes );
1140 return Html::rawElement(
1153 if ( $this->mFormIdentifier !==
null ) {
1154 $html .= Html::hidden(
1156 $this->mFormIdentifier
1160 $html .= Html::hidden(
1162 $this->
getUser()->getEditToken( $this->mTokenSalt ),
1163 [
'id' =>
'wpEditToken' ]
1165 $html .= Html::hidden(
'title', $this->
getTitle()->getPrefixedText() ) .
"\n";
1168 $articlePath = $this->
getConfig()->get(
'ArticlePath' );
1169 if ( strpos( $articlePath,
'?' ) !==
false && $this->
getMethod() ===
'get' ) {
1170 $html .= Html::hidden(
'title', $this->
getTitle()->getPrefixedText() ) .
"\n";
1173 foreach ( $this->mHiddenFields as $data ) {
1174 list( $value, $attribs ) = $data;
1175 $html .= Html::hidden( $attribs[
'name'], $value, $attribs ) .
"\n";
1187 $useMediaWikiUIEverywhere = $this->
getConfig()->get(
'UseMediaWikiUIEverywhere' );
1189 if ( $this->mShowSubmit ) {
1192 if ( isset( $this->mSubmitID ) ) {
1193 $attribs[
'id'] = $this->mSubmitID;
1196 if ( isset( $this->mSubmitName ) ) {
1197 $attribs[
'name'] = $this->mSubmitName;
1200 if ( isset( $this->mSubmitTooltip ) ) {
1204 $attribs[
'class'] = [
'mw-htmlform-submit' ];
1206 if ( $useMediaWikiUIEverywhere ) {
1207 foreach ( $this->mSubmitFlags as $flag ) {
1208 $attribs[
'class'][] =
'mw-ui-' . $flag;
1210 $attribs[
'class'][] =
'mw-ui-button';
1213 $buttons .= Xml::submitButton( $this->
getSubmitText(), $attribs ) .
"\n";
1216 if ( $this->mShowReset ) {
1217 $buttons .= Html::element(
1221 'value' => $this->
msg(
'htmlform-reset' )->text(),
1222 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1227 if ( $this->mShowCancel ) {
1228 $target = $this->mCancelTarget ?: Title::newMainPage();
1229 if ( $target instanceof
Title ) {
1230 $target = $target->getLocalURL();
1232 $buttons .= Html::element(
1235 'class' => $useMediaWikiUIEverywhere ?
'mw-ui-button' :
null,
1238 $this->
msg(
'cancel' )->text()
1243 $isBadIE = preg_match(
'/MSIE [1-7]\./i', $this->
getRequest()->getHeader(
'User-Agent' ) );
1245 foreach ( $this->mButtons as $button ) {
1248 'name' => $button[
'name'],
1249 'value' => $button[
'value']
1252 if ( isset( $button[
'label-message'] ) ) {
1253 $label = $this->
getMessage( $button[
'label-message'] )->parse();
1254 } elseif ( isset( $button[
'label'] ) ) {
1255 $label = htmlspecialchars( $button[
'label'] );
1256 } elseif ( isset( $button[
'label-raw'] ) ) {
1257 $label = $button[
'label-raw'];
1259 $label = htmlspecialchars( $button[
'value'] );
1262 if ( $button[
'attribs'] ) {
1263 $attrs += $button[
'attribs'];
1266 if ( isset( $button[
'id'] ) ) {
1267 $attrs[
'id'] = $button[
'id'];
1270 if ( $useMediaWikiUIEverywhere ) {
1271 $attrs[
'class'] = isset( $attrs[
'class'] ) ? (array)$attrs[
'class'] : [];
1272 $attrs[
'class'][] =
'mw-ui-button';
1276 $buttons .= Html::element(
'input', $attrs ) .
"\n";
1278 $buttons .= Html::rawElement(
'button', $attrs, $label ) .
"\n";
1286 return Html::rawElement(
'span',
1287 [
'class' =>
'mw-htmlform-submit-buttons' ],
"\n$buttons" ) .
"\n";
1295 return $this->
displaySection( $this->mFieldTree, $this->mTableId );
1307 if ( !in_array( $elementsType, [
'error',
'warning' ],
true ) ) {
1308 throw new DomainException( $elementsType .
' is not a valid type.' );
1310 $elementstr =
false;
1311 if ( $elements instanceof
Status ) {
1312 list( $errorStatus, $warningStatus ) = $elements->splitByErrorType();
1313 $status = $elementsType ===
'error' ? $errorStatus : $warningStatus;
1314 if ( $status->isGood() ) {
1317 $elementstr = $this->
getOutput()->parseAsInterface(
1318 $status->getWikiText()
1321 } elseif ( is_array( $elements ) && $elementsType ===
'error' ) {
1323 } elseif ( $elementsType ===
'error' ) {
1324 $elementstr = $elements;
1328 ? Html::rawElement(
'div', [
'class' => $elementsType .
'box' ], $elementstr )
1342 foreach ( $errors as $error ) {
1343 $errorstr .= Html::rawElement(
1350 $errorstr = Html::rawElement(
'ul', [], $errorstr );
1363 $this->mSubmitText =
$t;
1375 $this->mSubmitFlags = [
'destructive',
'primary' ];
1389 if ( !$msg instanceof
Message ) {
1390 $msg = $this->
msg( $msg );
1402 return $this->mSubmitText ?: $this->
msg(
'htmlform-submit' )->text();
1411 $this->mSubmitName = $name;
1422 $this->mSubmitTooltip = $name;
1436 $this->mSubmitID =
$t;
1457 $this->mFormIdentifier = $ident;
1473 $this->mShowSubmit = !$suppressSubmit;
1485 $this->mShowCancel = $show;
1496 $this->mCancelTarget = $target;
1510 $this->mTableId = $id;
1531 $this->mName = $name;
1548 $this->mWrapperLegend = $legend;
1561 $this->mWrapperAttributes = $attributes;
1576 if ( !$msg instanceof
Message ) {
1577 $msg = $this->
msg( $msg );
1594 $this->mMessagePrefix = $p;
1617 return $this->mTitle ===
false
1630 $this->mMethod = strtolower( $method );
1639 return $this->mMethod;
1652 return Xml::fieldset( $legend, $section, $attributes ) .
"\n";
1673 $fieldsetIDPrefix =
'',
1674 &$hasUserVisibleFields =
false
1676 if ( $this->mFieldData ===
null ) {
1677 throw new LogicException(
'HTMLForm::displaySection() called on uninitialized field data. '
1678 .
'You probably called displayForm() without calling prepareForm() first.' );
1684 $subsectionHtml =
'';
1691 foreach ( $fields as $key => $value ) {
1693 $v = array_key_exists( $key, $this->mFieldData )
1694 ? $this->mFieldData[$key]
1695 : $value->getDefault();
1697 $retval = $value->$getFieldHtmlMethod( $v );
1701 if ( $value->hasVisibleOutput() ) {
1704 $labelValue = trim( $value->getLabel() );
1705 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1709 $hasUserVisibleFields =
true;
1711 } elseif ( is_array( $value ) ) {
1712 $subsectionHasVisibleFields =
false;
1716 "$fieldsetIDPrefix$key-",
1717 $subsectionHasVisibleFields );
1720 if ( $subsectionHasVisibleFields ===
true ) {
1722 $hasUserVisibleFields =
true;
1731 if ( $fieldsetIDPrefix ) {
1732 $attributes[
'id'] = Sanitizer::escapeIdForAttribute(
"$fieldsetIDPrefix$key" );
1735 $legend, $section, $attributes, $fields === $this->mFieldTree
1739 $subsectionHtml .= $section;
1744 $html = $this->
formatSection( $html, $sectionName, $hasLabel );
1746 if ( $subsectionHtml ) {
1747 if ( $this->mSubSectionBeforeFields ) {
1748 return $subsectionHtml .
"\n" . $html;
1750 return $html .
"\n" . $subsectionHtml;
1764 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1765 if ( !$fieldsHtml ) {
1772 $html = implode(
'', $fieldsHtml );
1780 if ( !$anyFieldHasLabel ) {
1781 $classes[] =
'mw-htmlform-nolabel';
1785 'class' => implode(
' ', $classes ),
1788 if ( $sectionName ) {
1789 $attribs[
'id'] = Sanitizer::escapeIdForAttribute( $sectionName );
1793 return Html::rawElement(
'table',
1795 Html::rawElement(
'tbody', [],
"\n$html\n" ) ) .
"\n";
1797 return Html::rawElement(
'span', $attribs,
"\n$html\n" );
1799 return Html::rawElement(
'div', $attribs,
"\n$html\n" );
1809 foreach ( $this->mFlatFields as $fieldname => $field ) {
1811 if ( $field->skipLoadData( $request ) ) {
1813 } elseif ( !empty( $field->mParams[
'disabled'] ) ) {
1814 $fieldData[$fieldname] = $field->getDefault();
1816 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
1821 foreach ( $fieldData as $name => &$value ) {
1822 $field = $this->mFlatFields[$name];
1823 $value = $field->filter( $value, $this->mFlatFields );
1826 $this->mFieldData = $fieldData;
1837 $this->mShowReset = !$suppressReset;
1864 return $this->
msg(
"{$this->mMessagePrefix}-$key" )->text();
1878 $this->mAction = $action;
1892 if ( $this->mAction !==
false ) {
1893 return $this->mAction;
1896 $articlePath = $this->
getConfig()->get(
'ArticlePath' );
1902 if ( strpos( $articlePath,
'?' ) !==
false && $this->
getMethod() ===
'get' ) {
1906 return $this->
getTitle()->getLocalURL();
1920 $this->mAutocomplete = $autocomplete;
1945 foreach ( $this->mFlatFields as $fieldname => $field ) {
1946 if ( $field->needsJSForHtml5FormValidation() ) {
wfScript( $script='index')
Get the path to a specified script file, respecting file extensions; this is a wrapper around $wgScri...
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
setContext(IContextSource $context)
static tooltipAndAccesskeyAttribs( $name, array $msgParams=[], $options=null)
Returns the attributes for the tooltip and access key.
The Message class provides methods which fulfil two basic services:
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Represents a title within MediaWiki.
Interface for objects which can provide a MediaWiki context on request.