13use InvalidArgumentException;
17use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
208 use ProtectedHookAccessorTrait;
212 'api' => HTMLApiField::class,
213 'text' => HTMLTextField::class,
214 'textwithbutton' => HTMLTextFieldWithButton::class,
215 'textarea' => HTMLTextAreaField::class,
216 'select' => HTMLSelectField::class,
217 'combobox' => HTMLComboboxField::class,
218 'radio' => HTMLRadioField::class,
219 'multiselect' => HTMLMultiSelectField::class,
220 'limitselect' => HTMLSelectLimitField::class,
221 'check' => HTMLCheckField::class,
222 'toggle' => HTMLCheckField::class,
223 'int' => HTMLIntField::class,
224 'file' => HTMLFileField::class,
225 'float' => HTMLFloatField::class,
226 'info' => HTMLInfoField::class,
227 'selectorother' => HTMLSelectOrOtherField::class,
228 'selectandother' => HTMLSelectAndOtherField::class,
229 'namespaceselect' => HTMLSelectNamespace::class,
230 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
231 'tagfilter' => HTMLTagFilter::class,
232 'sizefilter' => HTMLSizeFilterField::class,
233 'submit' => HTMLSubmitField::class,
234 'hidden' => HTMLHiddenField::class,
235 'edittools' => HTMLEditTools::class,
236 'checkmatrix' => HTMLCheckMatrix::class,
237 'cloner' => HTMLFormFieldCloner::class,
238 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
239 'language' => HTMLSelectLanguageField::class,
240 'date' => HTMLDateTimeField::class,
241 'time' => HTMLDateTimeField::class,
242 'datetime' => HTMLDateTimeField::class,
243 'expiry' => HTMLExpiryField::class,
244 'timezone' => HTMLTimezoneField::class,
248 'email' => HTMLTextField::class,
249 'password' => HTMLTextField::class,
250 'url' => HTMLTextField::class,
251 'title' => HTMLTitleTextField::class,
252 'user' => HTMLUserTextField::class,
253 'tagmultiselect' => HTMLTagMultiselectField::class,
254 'orderedmultiselect' => HTMLOrderedMultiselectField::class,
255 'usersmultiselect' => HTMLUsersMultiselectField::class,
256 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
257 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
434 private $hiddenTitleAddedToForm =
false;
454 return new CodexHTMLForm( $descriptor, $context, $messagePrefix );
456 return new OOUIHTMLForm( $descriptor, $context, $messagePrefix );
458 $form =
new self( $descriptor, $context, $messagePrefix );
479 $this->mMessagePrefix = $messagePrefix;
493 $loadedDescriptor = [];
495 foreach ( $descriptor as $fieldname => $info ) {
496 $section = $info[
'section'] ??
'';
498 if ( isset( $info[
'type'] ) && $info[
'type'] ===
'file' ) {
499 $this->mUseMultipart =
true;
502 $field = static::loadInputFromParameters( $fieldname, $info, $this );
504 $setSection =& $loadedDescriptor;
506 foreach ( explode(
'/', $section ) as $newName ) {
507 $setSection[$newName] ??= [];
508 $setSection =& $setSection[$newName];
512 $setSection[$fieldname] = $field;
513 $this->mFlatFields[$fieldname] = $field;
516 $this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor );
526 return isset( $this->mFlatFields[$fieldname] );
535 if ( !$this->
hasField( $fieldname ) ) {
536 throw new DomainException( __METHOD__ .
': no field named ' . $fieldname );
538 return $this->mFlatFields[$fieldname];
552 in_array( $format, $this->availableSubclassDisplayFormats,
true ) ||
553 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats,
true )
555 throw new LogicException(
'Cannot change display format after creation, ' .
556 'use HTMLForm::factory() instead' );
559 if ( !in_array( $format, $this->availableDisplayFormats,
true ) ) {
560 throw new InvalidArgumentException(
'Display format must be one of ' .
563 $this->availableDisplayFormats,
564 $this->availableSubclassDisplayFormats
570 $this->displayFormat = $format;
601 if ( isset( $descriptor[
'class'] ) ) {
602 $class = $descriptor[
'class'];
603 } elseif ( isset( $descriptor[
'type'] ) ) {
604 $class = static::$typeMappings[$descriptor[
'type']];
605 $descriptor[
'class'] = $class;
611 throw new InvalidArgumentException(
"Descriptor with no class for $fieldname: "
612 . print_r( $descriptor,
true ) );
633 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
635 $descriptor[
'fieldname'] = $fieldname;
637 $descriptor[
'parent'] = $parent;
640 'Calling HTMLForm::loadInputFromParameters without a parent was deprecated in 1.40',
645 # @todo This will throw a fatal error whenever someone try to use
646 # 'class' to feed a CSS class instead of 'cssclass'. Would be
647 # great to avoid the fatal error and show a nice error.
648 return new $class( $descriptor );
660 # Load data from the request.
662 $this->mFormIdentifier ===
null ||
663 $this->
getRequest()->getVal(
'wpFormIdentifier' ) === $this->mFormIdentifier ||
664 ( $this->mSingleForm && $this->
getMethod() ===
'get' )
668 $this->mFieldData = [];
681 $this->mWasSubmitted =
true;
695 if ( $this->mFormIdentifier === null ) {
704 } elseif ( $this->
getRequest()->wasPosted() ) {
705 $editToken = $this->
getRequest()->getVal(
'wpEditToken' );
706 if ( $this->
getUser()->isRegistered() || $editToken !==
null ) {
711 CsrfTokenSet::DEFAULT_FIELD_NAME, $this->mTokenSalt
717 return $identOkay && $tokenOkay;
728 $this->prepareForm();
730 $result = $this->tryAuthorizedSubmit();
731 if ( $result ===
true || ( $result instanceof
Status && $result->
isGood() ) ) {
735 $this->displayForm( $result );
746 $this->prepareForm();
748 $result = $this->tryAuthorizedSubmit();
750 $this->displayForm( $result );
768 $hoistedErrors = Status::newGood();
769 if ( $this->mValidationErrorMessage ) {
770 foreach ( $this->mValidationErrorMessage as $error ) {
771 $hoistedErrors->fatal( ...$error );
774 $hoistedErrors->fatal(
'htmlform-invalid-input' );
777 $this->mWasSubmitted =
true;
779 # Check for cancelled submission
780 foreach ( $this->mFlatFields as $fieldname => $field ) {
781 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
784 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
785 $this->mWasSubmitted =
false;
790 # Check for validation
791 $hasNonDefault =
false;
792 foreach ( $this->mFlatFields as $fieldname => $field ) {
793 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
796 $hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault();
797 if ( $field->isDisabled( $this->mFieldData ) ) {
800 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
801 if ( $res !==
true ) {
803 if ( $res !==
false && !$field->canDisplayErrors() ) {
804 if ( is_string( $res ) ) {
805 $hoistedErrors->fatal(
'rawmessage', $res );
807 $hoistedErrors->fatal( $res );
815 if ( !$hasNonDefault && $this->getMethod() ===
'get' &&
816 ( $this->mFormIdentifier ===
null ||
817 $this->getRequest()->getCheck(
'wpFormIdentifier' ) )
819 $this->mWasSubmitted =
false;
822 return $hoistedErrors;
825 $callback = $this->mSubmitCallback;
826 if ( !is_callable( $callback ) ) {
827 throw new LogicException(
'HTMLForm: no submit callback provided. Use ' .
828 'setSubmitCallback() to set one.' );
831 $data = $this->filterDataForSubmit( $this->mFieldData );
833 $res = $callback( $data, $this );
834 if ( $res ===
false ) {
835 $this->mWasSubmitted =
false;
838 $res = Status::wrap( $res );
856 return $this->mWasSubmitted;
870 $this->mSubmitCallback = $cb;
885 $this->mValidationErrorMessage = $msg;
913 $this->mPre .= $html;
938 if ( $section ===
null ) {
939 $this->mHeader .= $html;
941 $this->mSectionHeaders[$section] ??=
'';
942 $this->mSectionHeaders[$section] .= $html;
958 if ( $section ===
null ) {
959 $this->mHeader = $html;
961 $this->mSectionHeaders[$section] = $html;
976 return $section ? $this->mSectionHeaders[$section] ??
'' : $this->mHeader;
989 if ( $section ===
null ) {
990 $this->mFooter .= $html;
992 $this->mSectionFooters[$section] ??=
'';
993 $this->mSectionFooters[$section] .= $html;
1009 if ( $section ===
null ) {
1010 $this->mFooter = $html;
1012 $this->mSectionFooters[$section] = $html;
1026 return $section ? $this->mSectionFooters[$section] ??
'' : $this->mFooter;
1038 $this->mPost .= $html;
1052 $this->mPost = $html;
1064 return $this->mPost;
1077 if ( $this->getDisplayFormat() !==
'codex' ) {
1078 throw new \InvalidArgumentException(
1079 "Non-Codex HTMLForms do not support additional section information."
1083 $this->mSections = $sections;
1099 if ( !is_array( $value ) ) {
1101 $attribs += [
'name' => $name ];
1102 $this->mHiddenFields[] = [ $value, $attribs ];
1120 foreach ( $fields as $name => $value ) {
1121 if ( is_array( $value ) ) {
1125 $this->mHiddenFields[] = [ $value, [
'name' => $name ] ];
1155 if ( !is_array( $data ) ) {
1156 $args = func_get_args();
1157 if ( count( $args ) < 2 || count( $args ) > 4 ) {
1158 throw new InvalidArgumentException(
1159 'Incorrect number of arguments for deprecated calling style'
1164 'value' => $args[1],
1165 'id' => $args[2] ??
null,
1166 'attribs' => $args[3] ??
null,
1169 if ( !isset( $data[
'name'] ) ) {
1170 throw new InvalidArgumentException(
'A name is required' );
1172 if ( !isset( $data[
'value'] ) ) {
1173 throw new InvalidArgumentException(
'A value is required' );
1176 $this->mButtons[] = $data + [
1196 $this->mTokenSalt = $salt;
1216 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1222 private function getHiddenTitle(): string {
1223 if ( $this->hiddenTitleAddedToForm ) {
1228 if ( $this->getMethod() ===
'post' ||
1231 $html .= Html::hidden(
'title', $this->getTitle()->getPrefixedText() ) .
"\n";
1233 $this->hiddenTitleAddedToForm =
true;
1248 # For good measure (it is the default)
1249 $this->getOutput()->getMetadata()->setPreventClickjacking(
true );
1250 $this->getOutput()->addModules(
'mediawiki.htmlform' );
1251 $this->getOutput()->addModuleStyles( [
1252 'mediawiki.htmlform.styles',
1254 'mediawiki.codex.messagebox.styles'
1257 if ( $this->mCollapsible ) {
1259 $this->getOutput()->addModules(
'jquery.makeCollapsible' );
1262 $headerHtml = $this->getHeaderHtml();
1263 $footerHtml = $this->getFooterHtml();
1264 $html = $this->getErrorsOrWarnings( $submitResult,
'error' )
1265 . $this->getErrorsOrWarnings( $submitResult,
'warning' )
1267 . $this->getHiddenTitle()
1269 . $this->getHiddenFields()
1270 . $this->getButtons()
1273 return $this->mPre . $this->wrapForm( $html ) . $this->mPost;
1284 $this->mCollapsible =
true;
1285 $this->mCollapsed = $collapsedByDefault;
1295 # Use multipart/form-data
1296 $encType = $this->mUseMultipart
1297 ?
'multipart/form-data'
1298 :
'application/x-www-form-urlencoded';
1301 'class' =>
'mw-htmlform',
1302 'action' => $this->getAction(),
1303 'method' => $this->getMethod(),
1304 'enctype' => $encType,
1307 $attribs[
'id'] = $this->mId;
1309 if ( is_string( $this->mAutocomplete ) ) {
1310 $attribs[
'autocomplete'] = $this->mAutocomplete;
1312 if ( $this->mName ) {
1313 $attribs[
'name'] = $this->mName;
1315 if ( $this->needsJSForHtml5FormValidation() ) {
1316 $attribs[
'novalidate'] =
true;
1329 # Include a <fieldset> wrapper for style, if requested.
1330 if ( $this->mWrapperLegend !==
false ) {
1331 $legend = is_string( $this->mWrapperLegend ) ? $this->mWrapperLegend :
false;
1332 $html = Html::rawElement(
1334 $this->mWrapperAttributes,
1335 ( $legend ? Html::element(
'legend', [], $legend ) :
'' ) . $html
1339 return Html::rawElement(
1341 $this->getFormAttributes(),
1355 $html .= $this->getHiddenTitle();
1357 if ( $this->mFormIdentifier !==
null ) {
1358 $html .= Html::hidden(
1360 $this->mFormIdentifier
1363 if ( $this->getMethod() ===
'post' ) {
1364 $html .= Html::hidden(
1366 $this->getUser()->getEditToken( $this->mTokenSalt ),
1367 [
'id' =>
'wpEditToken' ]
1371 foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1372 $html .= Html::hidden( $attribs[
'name'], $value, $attribs ) .
"\n";
1386 if ( $this->mShowSubmit ) {
1389 if ( $this->mSubmitID !==
null ) {
1390 $attribs[
'id'] = $this->mSubmitID;
1393 if ( $this->mSubmitName !==
null ) {
1394 $attribs[
'name'] = $this->mSubmitName;
1397 if ( $this->mSubmitTooltip !==
null ) {
1398 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1401 $attribs[
'class'] = [
'mw-htmlform-submit' ];
1403 $buttons .= Html::submitButton( $this->getSubmitText(), $attribs ) .
"\n";
1406 if ( $this->mShowCancel ) {
1407 $target = $this->getCancelTargetURL();
1408 $buttons .= Html::element(
1413 $this->msg(
'cancel' )->text()
1417 foreach ( $this->mButtons as $button ) {
1420 'name' => $button[
'name'],
1421 'value' => $button[
'value']
1424 if ( isset( $button[
'label-message'] ) ) {
1425 $label = $this->getMessage( $button[
'label-message'] )->parse();
1426 } elseif ( isset( $button[
'label'] ) ) {
1427 $label = htmlspecialchars( $button[
'label'] );
1428 } elseif ( isset( $button[
'label-raw'] ) ) {
1429 $label = $button[
'label-raw'];
1431 $label = htmlspecialchars( $button[
'value'] );
1435 if ( $button[
'attribs'] ) {
1436 $attrs += $button[
'attribs'];
1439 if ( isset( $button[
'id'] ) ) {
1440 $attrs[
'id'] = $button[
'id'];
1443 $buttons .= Html::rawElement(
'button', $attrs, $label ) .
"\n";
1450 return Html::rawElement(
'span',
1451 [
'class' =>
'mw-htmlform-submit-buttons' ],
"\n$buttons" ) .
"\n";
1460 return $this->displaySection( $this->mFieldTree, $this->mTableId );
1473 if ( !in_array( $elementsType, [
'error',
'warning' ],
true ) ) {
1474 throw new DomainException( $elementsType .
' is not a valid type.' );
1476 $elementstr =
false;
1477 if ( $elements instanceof
Status ) {
1478 [ $errorStatus, $warningStatus ] = $elements->splitByErrorType();
1479 $status = $elementsType ===
'error' ? $errorStatus : $warningStatus;
1480 if ( $status->isGood() ) {
1483 $elementstr = $status
1485 ->setContext( $this )
1486 ->setInterfaceMessageFlag(
true )
1489 } elseif ( $elementsType ===
'error' ) {
1490 if ( is_array( $elements ) ) {
1491 $elementstr = $this->formatErrors( $elements );
1492 } elseif ( $elements && $elements !==
true ) {
1493 $elementstr = (string)$elements;
1497 if ( !$elementstr ) {
1499 } elseif ( $elementsType ===
'error' ) {
1500 return Html::errorBox( $elementstr );
1502 return Html::warningBox( $elementstr );
1516 foreach ( $errors as $error ) {
1517 $errorstr .= Html::rawElement(
1520 $this->getMessage( $error )->parse()
1524 return Html::rawElement(
'ul', [], $errorstr );
1536 $this->mSubmitText = $t;
1548 $this->mSubmitFlags = [
'destructive',
'primary' ];
1562 if ( !$msg instanceof
Message ) {
1563 $msg = $this->msg( $msg );
1565 $this->setSubmitText( $msg->text() );
1575 return $this->mSubmitText ?: $this->msg(
'htmlform-submit' )->text();
1584 $this->mSubmitName = $name;
1595 $this->mSubmitTooltip = $name;
1609 $this->mSubmitID = $t;
1633 $this->mFormIdentifier = $ident;
1634 $this->mSingleForm = $single;
1650 $this->mShowSubmit = !$suppressSubmit;
1662 $this->mShowCancel = $show;
1674 $target = TitleValue::castPageToLinkTarget( $target );
1677 $this->mCancelTarget = $target;
1686 if ( is_string( $this->mCancelTarget ) ) {
1687 return $this->mCancelTarget;
1690 $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1691 return $target->getLocalURL();
1705 $this->mTableId = $id;
1726 $this->mName = $name;
1744 $this->mWrapperLegend = $legend;
1757 $this->mWrapperAttributes = $attributes;
1772 if ( !$msg instanceof
Message ) {
1773 $msg = $this->msg( $msg );
1775 $this->setWrapperLegend( $msg->text() );
1790 $this->mMessagePrefix = $p;
1804 $this->mTitle = Title::castFromPageReference( $t );
1813 return $this->mTitle ?: $this->getContext()->getTitle();
1824 $this->mMethod = strtolower( $method );
1833 return $this->mMethod;
1847 return Html::rawElement(
1850 Html::element(
'legend', [], $legend ) . $section
1874 $fieldsetIDPrefix =
'',
1875 &$hasUserVisibleFields =
false
1877 if ( $this->mFieldData ===
null ) {
1878 throw new LogicException(
'HTMLForm::displaySection() called on uninitialized field data. '
1879 .
'You probably called displayForm() without calling prepareForm() first.' );
1883 $subsectionHtml =
'';
1886 foreach ( $fields as $key => $value ) {
1888 $v = array_key_exists( $key, $this->mFieldData )
1889 ? $this->mFieldData[$key]
1890 : $value->getDefault();
1892 $retval = $this->formatField( $value, $v ??
'' );
1896 if ( $value->hasVisibleOutput() ) {
1899 $labelValue = trim( $value->getLabel() );
1900 if ( $labelValue !==
"\u{00A0}" && $labelValue !==
' ' && $labelValue !==
'' ) {
1904 $hasUserVisibleFields =
true;
1906 } elseif ( is_array( $value ) ) {
1907 $subsectionHasVisibleFields =
false;
1909 $this->displaySection( $value,
1911 "$fieldsetIDPrefix$key-",
1912 $subsectionHasVisibleFields );
1914 if ( $subsectionHasVisibleFields ===
true ) {
1916 $hasUserVisibleFields =
true;
1918 $legend = $this->getLegend( $key );
1920 $headerHtml = $this->getHeaderHtml( $key );
1921 $footerHtml = $this->getFooterHtml( $key );
1922 $section = $headerHtml .
1927 if ( $fieldsetIDPrefix ) {
1928 $attributes[
'id'] = Sanitizer::escapeIdForAttribute(
"$fieldsetIDPrefix$key" );
1930 $subsectionHtml .= $this->wrapFieldSetSection(
1931 $legend, $section, $attributes, $fields === $this->mFieldTree
1935 $subsectionHtml .= $section;
1940 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1942 if ( $subsectionHtml ) {
1943 if ( $this->mSubSectionBeforeFields ) {
1944 return $subsectionHtml .
"\n" . $html;
1946 return $html .
"\n" . $subsectionHtml;
1962 $displayFormat = $this->getDisplayFormat();
1963 switch ( $displayFormat ) {
1967 return $field->
getDiv( $value );
1969 return $field->
getRaw( $value );
1973 throw new LogicException(
'Not implemented' );
1985 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
1986 if ( !$fieldsHtml ) {
1992 $displayFormat = $this->getDisplayFormat();
1993 $html = implode(
'', $fieldsHtml );
1995 if ( $displayFormat ===
'raw' ) {
2000 $attribs = $anyFieldHasLabel ? [] : [
'class' =>
'mw-htmlform-nolabel' ];
2002 if ( $sectionName ) {
2003 $attribs[
'id'] = Sanitizer::escapeIdForAttribute( $sectionName );
2006 if ( $displayFormat ===
'table' ) {
2007 return Html::rawElement(
'table',
2009 Html::rawElement(
'tbody', [],
"\n$html\n" ) ) .
"\n";
2010 } elseif ( $displayFormat ===
'inline' ) {
2011 return Html::rawElement(
'span', $attribs,
"\n$html\n" );
2013 return Html::rawElement(
'div', $attribs,
"\n$html\n" );
2021 $this->prepareForm();
2029 $request = $this->getRequest();
2031 foreach ( $this->mFlatFields as $fieldname => $field ) {
2032 if ( $field->skipLoadData( $request ) ) {
2035 if ( $field->mParams[
'disabled'] ??
false ) {
2036 $fieldData[$fieldname] = $field->getDefault();
2038 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2044 foreach ( $fieldData as $name => &$value ) {
2045 $field = $this->mFlatFields[$name];
2046 if ( $field->isDisabled( $fieldData ) ) {
2047 $value = $field->getDefault();
2052 foreach ( $fieldData as $name => &$value ) {
2053 $field = $this->mFlatFields[$name];
2054 $value = $field->filter( $value, $fieldData );
2057 $this->mFieldData = $fieldData;
2084 return $this->msg( $this->mMessagePrefix ?
"{$this->mMessagePrefix}-$key" : $key )->text();
2098 $this->mAction = $action;
2112 if ( $this->mAction !==
false ) {
2113 return $this->mAction;
2116 $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2122 if ( str_contains( $articlePath,
'?' ) && $this->getMethod() ===
'get' ) {
2123 return $this->getConfig()->get( MainConfigNames::Script );
2126 return $this->getTitle()->getLocalURL();
2140 $this->mAutocomplete = $autocomplete;
2152 return Message::newFromSpecifier( $value )->setContext( $this );
2165 foreach ( $this->mFlatFields as $field ) {
2166 if ( $field->needsJSForHtml5FormValidation() ) {
2175class_alias( HTMLForm::class,
'HTMLForm' );
wfDeprecatedMsg( $msg, $version=false, $component=false, $callerOffset=2)
Log a deprecation warning with arbitrary message text.
if(!defined('MW_SETUP_CALLBACK'))
The simplest way of implementing IContextSource is to hold a RequestContext as a member variable and ...
getCsrfTokenSet()
Get a repository to obtain and match CSRF tokens.
setContext(IContextSource $context)
Creates a text input field with a button assigned to the input field.
Implements a text input field for page titles.
Implements a text input field for user names.
A class containing constants representing the names of configuration variables.
const Script
Name constant for the Script setting, for use with Config::get()
Generic operation result class Has warning/error list, boolean status and arbitrary value.
isGood()
Returns whether the operation completed and didn't have any error or warnings.
Interface for objects which can provide a MediaWiki context on request.