MediaWiki master
HTMLForm.php
Go to the documentation of this file.
1<?php
2
9
10namespace MediaWiki\HTMLForm;
11
12use DomainException;
13use InvalidArgumentException;
14use LogicException;
15use MediaWiki\Context\ContextSource;
16use MediaWiki\Context\IContextSource;
17use MediaWiki\HookContainer\ProtectedHookAccessorTrait;
18use MediaWiki\Html\Html;
19use MediaWiki\HTMLForm\Field\HTMLApiField;
20use MediaWiki\HTMLForm\Field\HTMLAutoCompleteSelectField;
21use MediaWiki\HTMLForm\Field\HTMLButtonField;
22use MediaWiki\HTMLForm\Field\HTMLCheckField;
23use MediaWiki\HTMLForm\Field\HTMLCheckMatrix;
24use MediaWiki\HTMLForm\Field\HTMLComboboxField;
25use MediaWiki\HTMLForm\Field\HTMLDateTimeField;
26use MediaWiki\HTMLForm\Field\HTMLEditTools;
27use MediaWiki\HTMLForm\Field\HTMLExpiryField;
28use MediaWiki\HTMLForm\Field\HTMLFileField;
29use MediaWiki\HTMLForm\Field\HTMLFloatField;
30use MediaWiki\HTMLForm\Field\HTMLFormFieldCloner;
31use MediaWiki\HTMLForm\Field\HTMLHiddenField;
32use MediaWiki\HTMLForm\Field\HTMLInfoField;
33use MediaWiki\HTMLForm\Field\HTMLIntField;
34use MediaWiki\HTMLForm\Field\HTMLMultiSelectField;
35use MediaWiki\HTMLForm\Field\HTMLNamespacesMultiselectField;
36use MediaWiki\HTMLForm\Field\HTMLOrderedMultiselectField;
37use MediaWiki\HTMLForm\Field\HTMLRadioField;
38use MediaWiki\HTMLForm\Field\HTMLSelectAndOtherField;
39use MediaWiki\HTMLForm\Field\HTMLSelectField;
40use MediaWiki\HTMLForm\Field\HTMLSelectLanguageField;
41use MediaWiki\HTMLForm\Field\HTMLSelectLimitField;
42use MediaWiki\HTMLForm\Field\HTMLSelectNamespace;
43use MediaWiki\HTMLForm\Field\HTMLSelectNamespaceWithButton;
44use MediaWiki\HTMLForm\Field\HTMLSelectOrOtherField;
45use MediaWiki\HTMLForm\Field\HTMLSizeFilterField;
46use MediaWiki\HTMLForm\Field\HTMLSubmitField;
47use MediaWiki\HTMLForm\Field\HTMLTagFilter;
48use MediaWiki\HTMLForm\Field\HTMLTagMultiselectField;
49use MediaWiki\HTMLForm\Field\HTMLTextAreaField;
50use MediaWiki\HTMLForm\Field\HTMLTextField;
51use MediaWiki\HTMLForm\Field\HTMLTextFieldWithButton;
52use MediaWiki\HTMLForm\Field\HTMLTimezoneField;
53use MediaWiki\HTMLForm\Field\HTMLTitlesMultiselectField;
54use MediaWiki\HTMLForm\Field\HTMLTitleTextField;
55use MediaWiki\HTMLForm\Field\HTMLUsersMultiselectField;
56use MediaWiki\HTMLForm\Field\HTMLUserTextField;
57use MediaWiki\Linker\Linker;
58use MediaWiki\Linker\LinkTarget;
59use MediaWiki\MainConfigNames;
60use MediaWiki\Message\Message;
61use MediaWiki\Page\PageReference;
62use MediaWiki\Parser\Sanitizer;
63use MediaWiki\Session\CsrfTokenSet;
64use MediaWiki\Status\Status;
65use MediaWiki\Title\Title;
66use MediaWiki\Title\TitleValue;
67use StatusValue;
68use Stringable;
69use Wikimedia\Message\MessageParam;
70use Wikimedia\Message\MessageSpecifier;
71
214class HTMLForm extends ContextSource {
215 use ProtectedHookAccessorTrait;
216
218 public static $typeMappings = [
219 'api' => HTMLApiField::class,
220 'text' => HTMLTextField::class,
221 'textwithbutton' => HTMLTextFieldWithButton::class,
222 'textarea' => HTMLTextAreaField::class,
223 'select' => HTMLSelectField::class,
224 'combobox' => HTMLComboboxField::class,
225 'radio' => HTMLRadioField::class,
226 'multiselect' => HTMLMultiSelectField::class,
227 'limitselect' => HTMLSelectLimitField::class,
228 'check' => HTMLCheckField::class,
229 'toggle' => HTMLCheckField::class,
230 'int' => HTMLIntField::class,
231 'file' => HTMLFileField::class,
232 'float' => HTMLFloatField::class,
233 'info' => HTMLInfoField::class,
234 'selectorother' => HTMLSelectOrOtherField::class,
235 'selectandother' => HTMLSelectAndOtherField::class,
236 'namespaceselect' => HTMLSelectNamespace::class,
237 'namespaceselectwithbutton' => HTMLSelectNamespaceWithButton::class,
238 'tagfilter' => HTMLTagFilter::class,
239 'sizefilter' => HTMLSizeFilterField::class,
240 'button' => HTMLButtonField::class,
241 'submit' => HTMLSubmitField::class,
242 'hidden' => HTMLHiddenField::class,
243 'edittools' => HTMLEditTools::class,
244 'checkmatrix' => HTMLCheckMatrix::class,
245 'cloner' => HTMLFormFieldCloner::class,
246 'autocompleteselect' => HTMLAutoCompleteSelectField::class,
247 'language' => HTMLSelectLanguageField::class,
248 'date' => HTMLDateTimeField::class,
249 'time' => HTMLDateTimeField::class,
250 'datetime' => HTMLDateTimeField::class,
251 'expiry' => HTMLExpiryField::class,
252 'timezone' => HTMLTimezoneField::class,
253 // HTMLTextField will output the correct type="" attribute automagically.
254 // There are about four zillion other HTML5 input types, like range, but
255 // we don't use those at the moment, so no point in adding all of them.
256 'email' => HTMLTextField::class,
257 'password' => HTMLTextField::class,
258 'url' => HTMLTextField::class,
259 'title' => HTMLTitleTextField::class,
260 'user' => HTMLUserTextField::class,
261 'tagmultiselect' => HTMLTagMultiselectField::class,
262 'orderedmultiselect' => HTMLOrderedMultiselectField::class,
263 'usersmultiselect' => HTMLUsersMultiselectField::class,
264 'titlesmultiselect' => HTMLTitlesMultiselectField::class,
265 'namespacesmultiselect' => HTMLNamespacesMultiselectField::class,
266 ];
267
270
273
275 protected $mFlatFields = [];
277 protected $mFieldTree = [];
279 protected $mShowSubmit = true;
281 protected $mSubmitFlags = [ 'primary', 'progressive' ];
283 protected $mShowCancel = false;
285 protected $mCancelTarget;
286
294
296 protected $mPre = '';
298 protected $mHeader = '';
300 protected $mFooter = '';
302 protected $mSectionHeaders = [];
304 protected $mSectionFooters = [];
306 protected $mPost = '';
308 protected $mId;
310 protected $mName;
312 protected $mTableId = '';
313
315 protected $mSubmitID;
317 protected $mSubmitName;
319 protected $mSubmitText;
323 protected bool $mSubmitLockIcon = false;
324
328 protected $mSingleForm = false;
329
331 protected $mTitle;
333 protected $mMethod = 'post';
335 protected $mWasSubmitted = false;
336
342 protected $mAction = false;
343
349 protected $mCollapsible = false;
350
356 protected $mCollapsed = false;
357
363 protected $mAutocomplete = null;
364
366 protected $mUseMultipart = false;
371 protected $mHiddenFields = [];
376 protected $mButtons = [];
377
379 protected $mWrapperLegend = false;
381 protected $mWrapperAttributes = [];
382
387 protected $mTokenSalt = '';
388
401 protected $mSections = [];
402
411 protected $mSubSectionBeforeFields = true;
412
418 protected $displayFormat = 'table';
419
425 'table',
426 'div',
427 'raw',
428 'inline',
429 ];
430
436 'codex',
437 'ooui',
438 ];
439
444 private $hiddenTitleAddedToForm = false;
445
459 public static function factory(
460 $displayFormat, $descriptor, IContextSource $context, $messagePrefix = ''
461 ) {
462 switch ( $displayFormat ) {
463 case 'codex':
464 return new CodexHTMLForm( $descriptor, $context, $messagePrefix );
465 case 'ooui':
466 return new OOUIHTMLForm( $descriptor, $context, $messagePrefix );
467 default:
468 $form = new self( $descriptor, $context, $messagePrefix );
469 $form->setDisplayFormat( $displayFormat );
470 return $form;
471 }
472 }
473
485 public function __construct(
486 $descriptor, IContextSource $context, $messagePrefix = ''
487 ) {
488 $this->setContext( $context );
489 $this->mMessagePrefix = $messagePrefix;
490 $this->addFields( $descriptor );
491 }
492
502 public function addFields( $descriptor ) {
503 $loadedDescriptor = [];
504
505 foreach ( $descriptor as $fieldname => $info ) {
506 $section = $info['section'] ?? '';
507
508 if ( isset( $info['type'] ) && $info['type'] === 'file' ) {
509 $this->mUseMultipart = true;
510 }
511
512 $field = static::loadInputFromParameters( $fieldname, $info, $this );
513
514 $setSection =& $loadedDescriptor;
515 if ( $section ) {
516 foreach ( explode( '/', $section ) as $newName ) {
517 $setSection[$newName] ??= [];
518 $setSection =& $setSection[$newName];
519 }
520 }
521
522 $setSection[$fieldname] = $field;
523 $this->mFlatFields[$fieldname] = $field;
524 }
525
526 $this->mFieldTree = array_merge_recursive( $this->mFieldTree, $loadedDescriptor );
527
528 return $this;
529 }
530
535 public function hasField( $fieldname ) {
536 return isset( $this->mFlatFields[$fieldname] );
537 }
538
544 public function getField( $fieldname ) {
545 if ( !$this->hasField( $fieldname ) ) {
546 throw new DomainException( __METHOD__ . ': no field named ' . $fieldname );
547 }
548 return $this->mFlatFields[$fieldname];
549 }
550
560 public function setDisplayFormat( $format ) {
561 if (
562 in_array( $format, $this->availableSubclassDisplayFormats, true ) ||
563 in_array( $this->displayFormat, $this->availableSubclassDisplayFormats, true )
564 ) {
565 throw new LogicException( 'Cannot change display format after creation, ' .
566 'use HTMLForm::factory() instead' );
567 }
568
569 if ( !in_array( $format, $this->availableDisplayFormats, true ) ) {
570 throw new InvalidArgumentException( 'Display format must be one of ' .
571 print_r(
572 array_merge(
573 $this->availableDisplayFormats,
574 $this->availableSubclassDisplayFormats
575 ),
576 true
577 ) );
578 }
579
580 $this->displayFormat = $format;
581
582 return $this;
583 }
584
590 public function getDisplayFormat() {
592 }
593
610 public static function getClassFromDescriptor( $fieldname, &$descriptor ) {
611 if ( isset( $descriptor['class'] ) ) {
612 $class = $descriptor['class'];
613 } elseif ( isset( $descriptor['type'] ) ) {
614 $class = static::$typeMappings[$descriptor['type']];
615 $descriptor['class'] = $class;
616 } else {
617 $class = null;
618 }
619
620 if ( !$class ) {
621 throw new InvalidArgumentException( "Descriptor with no class for $fieldname: "
622 . print_r( $descriptor, true ) );
623 }
624
625 return $class;
626 }
627
639 public static function loadInputFromParameters( $fieldname, $descriptor, self $parent ) {
640 $class = static::getClassFromDescriptor( $fieldname, $descriptor );
641
642 $descriptor['fieldname'] = $fieldname;
643 $descriptor['parent'] = $parent;
644
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 );
649 }
650
659 public function prepareForm() {
660 # Load data from the request.
661 if (
662 $this->mFormIdentifier === null ||
663 $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier ||
664 ( $this->mSingleForm && $this->getMethod() === 'get' )
665 ) {
666 $this->loadFieldData();
667 } else {
668 $this->mFieldData = [];
669 }
670
671 return $this;
672 }
673
678 public function tryAuthorizedSubmit() {
679 $result = false;
680 if ( $this->requestIsAuthorized() ) {
681 $this->mWasSubmitted = true;
682 $result = $this->trySubmit();
683 }
684
685 return $result;
686 }
687
694 public function requestIsAuthorized(): bool {
695 if ( $this->mFormIdentifier === null ) {
696 $identOkay = true;
697 } else {
698 $identOkay = $this->getRequest()->getVal( 'wpFormIdentifier' ) === $this->mFormIdentifier;
699 }
700
701 $tokenOkay = false;
702 if ( $this->getMethod() !== 'post' ) {
703 $tokenOkay = true; // no session check needed
704 } elseif ( $this->getRequest()->wasPosted() ) {
705 $editToken = $this->getRequest()->getVal( 'wpEditToken' );
706 if ( $this->getUser()->isRegistered() || $editToken !== null ) {
707 // Session tokens for logged-out users have no security value.
708 // However, if the user gave one, check it in order to give a nice
709 // "session expired" error instead of "permission denied" or such.
710 $tokenOkay = $this->getCsrfTokenSet()->matchTokenField(
711 CsrfTokenSet::DEFAULT_FIELD_NAME, $this->mTokenSalt
712 );
713 } else {
714 $tokenOkay = true;
715 }
716 }
717 return $identOkay && $tokenOkay;
718 }
719
727 public function show() {
728 $this->prepareForm();
729
730 $result = $this->tryAuthorizedSubmit();
731 if ( $result === true || ( $result instanceof Status && $result->isGood() ) ) {
732 return $result;
733 }
734
735 $this->displayForm( $result );
736
737 return false;
738 }
739
745 public function showAlways() {
746 $this->prepareForm();
747
748 $result = $this->tryAuthorizedSubmit();
749
750 $this->displayForm( $result );
751
752 return $result;
753 }
754
766 public function trySubmit() {
767 $valid = true;
768 $hoistedErrors = Status::newGood();
769 if ( $this->mValidationErrorMessage ) {
770 foreach ( $this->mValidationErrorMessage as $error ) {
771 $hoistedErrors->fatal( ...$error );
772 }
773 } else {
774 $hoistedErrors->fatal( 'htmlform-invalid-input' );
775 }
776
777 $this->mWasSubmitted = true;
778
779 # Check for cancelled submission
780 foreach ( $this->mFlatFields as $fieldname => $field ) {
781 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
782 continue;
783 }
784 if ( $field->cancelSubmit( $this->mFieldData[$fieldname], $this->mFieldData ) ) {
785 $this->mWasSubmitted = false;
786 return false;
787 }
788 }
789
790 # Check for validation
791 $hasNonDefault = false;
792 foreach ( $this->mFlatFields as $fieldname => $field ) {
793 if ( !array_key_exists( $fieldname, $this->mFieldData ) ) {
794 continue;
795 }
796 $hasNonDefault = $hasNonDefault || $this->mFieldData[$fieldname] !== $field->getDefault();
797 if ( $field->isDisabled( $this->mFieldData ) ) {
798 continue;
799 }
800 $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData );
801 if ( $res !== true ) {
802 $valid = false;
803 if ( $res !== false && !$field->canDisplayErrors() ) {
804 if ( is_string( $res ) ) {
805 $hoistedErrors->fatal( 'rawmessage', $res );
806 } else {
807 $hoistedErrors->fatal( $res );
808 }
809 }
810 }
811 }
812
813 if ( !$valid ) {
814 // Treat as not submitted if got nothing from the user on GET forms.
815 if ( !$hasNonDefault && $this->getMethod() === 'get' &&
816 ( $this->mFormIdentifier === null ||
817 $this->getRequest()->getCheck( 'wpFormIdentifier' ) )
818 ) {
819 $this->mWasSubmitted = false;
820 return false;
821 }
822 return $hoistedErrors;
823 }
824
825 $callback = $this->mSubmitCallback;
826 if ( !is_callable( $callback ) ) {
827 throw new LogicException( 'HTMLForm: no submit callback provided. Use ' .
828 'setSubmitCallback() to set one.' );
829 }
830
831 $data = $this->filterDataForSubmit( $this->mFieldData );
832
833 $res = $callback( $data, $this );
834 if ( $res === false ) {
835 $this->mWasSubmitted = false;
836 } elseif ( $res instanceof StatusValue ) {
837 // DWIM - callbacks are not supposed to return a StatusValue but it's easy to mix up.
838 $res = Status::wrap( $res );
839 }
840
841 return $res;
842 }
843
855 public function wasSubmitted() {
856 return $this->mWasSubmitted;
857 }
858
869 public function setSubmitCallback( $cb ) {
870 $this->mSubmitCallback = $cb;
871
872 return $this;
873 }
874
884 public function setValidationErrorMessage( $msg ) {
885 $this->mValidationErrorMessage = $msg;
886
887 return $this;
888 }
889
898 public function setPreHtml( $html ) {
899 $this->mPre = $html;
900
901 return $this;
902 }
903
912 public function addPreHtml( $html ) {
913 $this->mPre .= $html;
914
915 return $this;
916 }
917
924 public function getPreHtml() {
925 return $this->mPre;
926 }
927
937 public function addHeaderHtml( $html, $section = null ) {
938 if ( $section === null ) {
939 $this->mHeader .= $html;
940 } else {
941 $this->mSectionHeaders[$section] ??= '';
942 $this->mSectionHeaders[$section] .= $html;
943 }
944
945 return $this;
946 }
947
957 public function setHeaderHtml( $html, $section = null ) {
958 if ( $section === null ) {
959 $this->mHeader = $html;
960 } else {
961 $this->mSectionHeaders[$section] = $html;
962 }
963
964 return $this;
965 }
966
975 public function getHeaderHtml( $section = null ) {
976 return $section ? $this->mSectionHeaders[$section] ?? '' : $this->mHeader;
977 }
978
988 public function addFooterHtml( $html, $section = null ) {
989 if ( $section === null ) {
990 $this->mFooter .= $html;
991 } else {
992 $this->mSectionFooters[$section] ??= '';
993 $this->mSectionFooters[$section] .= $html;
994 }
995
996 return $this;
997 }
998
1008 public function setFooterHtml( $html, $section = null ) {
1009 if ( $section === null ) {
1010 $this->mFooter = $html;
1011 } else {
1012 $this->mSectionFooters[$section] = $html;
1013 }
1014
1015 return $this;
1016 }
1017
1025 public function getFooterHtml( $section = null ) {
1026 return $section ? $this->mSectionFooters[$section] ?? '' : $this->mFooter;
1027 }
1028
1037 public function addPostHtml( $html ) {
1038 $this->mPost .= $html;
1039
1040 return $this;
1041 }
1042
1051 public function setPostHtml( $html ) {
1052 $this->mPost = $html;
1053
1054 return $this;
1055 }
1056
1063 public function getPostHtml() {
1064 return $this->mPost;
1065 }
1066
1076 public function setSections( $sections ) {
1077 if ( $this->getDisplayFormat() !== 'codex' ) {
1078 throw new \InvalidArgumentException(
1079 "Non-Codex HTMLForms do not support additional section information."
1080 );
1081 }
1082
1083 $this->mSections = $sections;
1084
1085 return $this;
1086 }
1087
1098 public function addHiddenField( $name, $value, array $attribs = [] ) {
1099 if ( !is_array( $value ) ) {
1100 // Per WebRequest::getVal: Array values are discarded for security reasons.
1101 $attribs += [ 'name' => $name ];
1102 $this->mHiddenFields[] = [ $value, $attribs ];
1103 }
1104
1105 return $this;
1106 }
1107
1119 public function addHiddenFields( array $fields ) {
1120 foreach ( $fields as $name => $value ) {
1121 if ( is_array( $value ) ) {
1122 // Per WebRequest::getVal: Array values are discarded for security reasons.
1123 continue;
1124 }
1125 $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
1126 }
1127
1128 return $this;
1129 }
1130
1154 public function addButton( $data ) {
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'
1160 );
1161 }
1162 $data = [
1163 'name' => $args[0],
1164 'value' => $args[1],
1165 'id' => $args[2] ?? null,
1166 'attribs' => $args[3] ?? null,
1167 ];
1168 } else {
1169 if ( !isset( $data['name'] ) ) {
1170 throw new InvalidArgumentException( 'A name is required' );
1171 }
1172 if ( !isset( $data['value'] ) ) {
1173 throw new InvalidArgumentException( 'A value is required' );
1174 }
1175 }
1176 $this->mButtons[] = $data + [
1177 'id' => null,
1178 'attribs' => null,
1179 'flags' => null,
1180 'framed' => true,
1181 ];
1182
1183 return $this;
1184 }
1185
1195 public function setTokenSalt( $salt ) {
1196 $this->mTokenSalt = $salt;
1197
1198 return $this;
1199 }
1200
1215 public function displayForm( $submitResult ) {
1216 $this->getOutput()->addHTML( $this->getHTML( $submitResult ) );
1217 }
1218
1222 private function getHiddenTitle(): string {
1223 if ( $this->hiddenTitleAddedToForm ) {
1224 return '';
1225 }
1226
1227 $html = '';
1228 if ( $this->getMethod() === 'post' ||
1229 $this->getAction() === $this->getConfig()->get( MainConfigNames::Script )
1230 ) {
1231 $html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
1232 }
1233 $this->hiddenTitleAddedToForm = true;
1234 return $html;
1235 }
1236
1247 public function getHTML( $submitResult ) {
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',
1253 // Html::errorBox and Html::warningBox used by HtmlFormField and HtmlForm::getErrorsOrWarnings
1254 'mediawiki.codex.messagebox.styles'
1255 ] );
1256
1257 if ( $this->mCollapsible ) {
1258 // Preload jquery.makeCollapsible for mediawiki.htmlform
1259 $this->getOutput()->addModules( 'jquery.makeCollapsible' );
1260 }
1261
1262 $headerHtml = $this->getHeaderHtml();
1263 $footerHtml = $this->getFooterHtml();
1264 $html = $this->getErrorsOrWarnings( $submitResult, 'error' )
1265 . $this->getErrorsOrWarnings( $submitResult, 'warning' )
1266 . $headerHtml
1267 . $this->getHiddenTitle()
1268 . $this->getBody()
1269 . $this->getHiddenFields()
1270 . $this->getButtons()
1271 . $footerHtml;
1272
1273 return $this->mPre . $this->wrapForm( $html ) . $this->mPost;
1274 }
1275
1283 public function setCollapsibleOptions( $collapsedByDefault = false ) {
1284 $this->mCollapsible = true;
1285 $this->mCollapsed = $collapsedByDefault;
1286 return $this;
1287 }
1288
1294 protected function getFormAttributes() {
1295 # Use multipart/form-data
1296 $encType = $this->mUseMultipart
1297 ? 'multipart/form-data'
1298 : 'application/x-www-form-urlencoded';
1299 # Attributes
1300 $attribs = [
1301 'class' => 'mw-htmlform',
1302 'action' => $this->getAction(),
1303 'method' => $this->getMethod(),
1304 'enctype' => $encType,
1305 ];
1306 if ( $this->mId ) {
1307 $attribs['id'] = $this->mId;
1308 }
1309 if ( is_string( $this->mAutocomplete ) ) {
1310 $attribs['autocomplete'] = $this->mAutocomplete;
1311 }
1312 if ( $this->mName ) {
1313 $attribs['name'] = $this->mName;
1314 }
1315 if ( $this->needsJSForHtml5FormValidation() ) {
1316 $attribs['novalidate'] = true;
1317 }
1318 return $attribs;
1319 }
1320
1328 public function wrapForm( $html ) {
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(
1333 'fieldset',
1334 $this->mWrapperAttributes,
1335 ( $legend ? Html::element( 'legend', [], $legend ) : '' ) . $html
1336 );
1337 }
1338
1339 return Html::rawElement(
1340 'form',
1341 $this->getFormAttributes(),
1342 $html
1343 );
1344 }
1345
1350 public function getHiddenFields() {
1351 $html = '';
1352
1353 // add the title as a hidden file if it hasn't been added yet and if it is necessary
1354 // added for backward compatibility with the previous version of this public method
1355 $html .= $this->getHiddenTitle();
1356
1357 if ( $this->mFormIdentifier !== null ) {
1358 $html .= Html::hidden(
1359 'wpFormIdentifier',
1360 $this->mFormIdentifier
1361 ) . "\n";
1362 }
1363 if ( $this->getMethod() === 'post' ) {
1364 $html .= Html::hidden(
1365 'wpEditToken',
1366 $this->getUser()->getEditToken( $this->mTokenSalt ),
1367 [ 'id' => 'wpEditToken' ]
1368 ) . "\n";
1369 }
1370
1371 foreach ( $this->mHiddenFields as [ $value, $attribs ] ) {
1372 $html .= Html::hidden( $attribs['name'], $value, $attribs ) . "\n";
1373 }
1374
1375 return $html;
1376 }
1377
1383 public function getButtons() {
1384 $buttons = '';
1385
1386 if ( $this->mShowSubmit ) {
1387 $attribs = [];
1388
1389 if ( $this->mSubmitID !== null ) {
1390 $attribs['id'] = $this->mSubmitID;
1391 }
1392
1393 if ( $this->mSubmitName !== null ) {
1394 $attribs['name'] = $this->mSubmitName;
1395 }
1396
1397 if ( $this->mSubmitTooltip !== null ) {
1398 $attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
1399 }
1400
1401 $attribs['class'] = [ 'mw-htmlform-submit' ];
1402
1403 $buttons .= Html::submitButton( $this->getSubmitText(), $attribs ) . "\n";
1404 }
1405
1406 if ( $this->mShowCancel ) {
1407 $target = $this->getCancelTargetURL();
1408 $buttons .= Html::element(
1409 'a',
1410 [
1411 'href' => $target,
1412 ],
1413 $this->msg( 'cancel' )->text()
1414 ) . "\n";
1415 }
1416
1417 foreach ( $this->mButtons as $button ) {
1418 $attrs = [
1419 'type' => 'submit',
1420 'name' => $button['name'],
1421 'value' => $button['value']
1422 ];
1423
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'];
1430 } else {
1431 $label = htmlspecialchars( $button['value'] );
1432 }
1433
1434 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset Always set in self::addButton
1435 if ( $button['attribs'] ) {
1436 $attrs += $button['attribs'];
1437 }
1438
1439 if ( isset( $button['id'] ) ) {
1440 $attrs['id'] = $button['id'];
1441 }
1442
1443 $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
1444 }
1445
1446 if ( !$buttons ) {
1447 return '';
1448 }
1449
1450 return Html::rawElement( 'span',
1451 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
1452 }
1453
1459 public function getBody() {
1460 return $this->displaySection( $this->mFieldTree, $this->mTableId );
1461 }
1462
1472 public function getErrorsOrWarnings( $elements, $elementsType ) {
1473 if ( !in_array( $elementsType, [ 'error', 'warning' ], true ) ) {
1474 throw new DomainException( $elementsType . ' is not a valid type.' );
1475 }
1476 $elementstr = false;
1477 if ( $elements instanceof Status ) {
1478 [ $errorStatus, $warningStatus ] = $elements->splitByErrorType();
1479 $status = $elementsType === 'error' ? $errorStatus : $warningStatus;
1480 if ( $status->isGood() ) {
1481 $elementstr = '';
1482 } else {
1483 $elementstr = $status
1484 ->getMessage()
1485 ->setContext( $this )
1486 ->setInterfaceMessageFlag( true )
1487 ->parse();
1488 }
1489 } elseif ( $elementsType === 'error' ) {
1490 if ( is_array( $elements ) ) {
1491 $elementstr = $this->formatErrors( $elements );
1492 } elseif ( $elements && $elements !== true ) {
1493 $elementstr = (string)$elements;
1494 }
1495 }
1496
1497 if ( !$elementstr ) {
1498 return '';
1499 } elseif ( $elementsType === 'error' ) {
1500 return Html::errorBox( $elementstr );
1501 } else { // $elementsType can only be 'warning'
1502 return Html::warningBox( $elementstr );
1503 }
1504 }
1505
1513 public function formatErrors( $errors ) {
1514 $errorstr = '';
1515
1516 foreach ( $errors as $error ) {
1517 $errorstr .= Html::rawElement(
1518 'li',
1519 [],
1520 $this->getMessage( $error )->parse()
1521 );
1522 }
1523
1524 return Html::rawElement( 'ul', [], $errorstr );
1525 }
1526
1535 public function setSubmitText( $t ) {
1536 $this->mSubmitText = $t;
1537
1538 return $this;
1539 }
1540
1547 public function setSubmitDestructive() {
1548 $this->mSubmitFlags = [ 'destructive', 'primary' ];
1549
1550 return $this;
1551 }
1552
1561 public function setSubmitTextMsg( $msg ) {
1562 if ( !$msg instanceof Message ) {
1563 $msg = $this->msg( $msg );
1564 }
1565 $this->setSubmitText( $msg->text() );
1566
1567 return $this;
1568 }
1569
1574 public function getSubmitText() {
1575 return $this->mSubmitText ?: $this->msg( 'htmlform-submit' )->text();
1576 }
1577
1583 public function setSubmitName( $name ) {
1584 $this->mSubmitName = $name;
1585
1586 return $this;
1587 }
1588
1594 public function setSubmitTooltip( $name ) {
1595 $this->mSubmitTooltip = $name;
1596
1597 return $this;
1598 }
1599
1608 public function setSubmitID( $t ) {
1609 $this->mSubmitID = $t;
1610
1611 return $this;
1612 }
1613
1626 public function setSubmitLockIcon() {
1627 $this->mSubmitLockIcon = true;
1628 $this->getOutput()->addModuleStyles( 'oojs-ui.styles.icons-moderation' );
1629
1630 return $this;
1631 }
1632
1651 public function setFormIdentifier( string $ident, bool $single = false ) {
1652 $this->mFormIdentifier = $ident;
1653 $this->mSingleForm = $single;
1654
1655 return $this;
1656 }
1657
1668 public function suppressDefaultSubmit( $suppressSubmit = true ) {
1669 $this->mShowSubmit = !$suppressSubmit;
1670
1671 return $this;
1672 }
1673
1680 public function showCancel( $show = true ) {
1681 $this->mShowCancel = $show;
1682 return $this;
1683 }
1684
1691 public function setCancelTarget( $target ) {
1692 if ( $target instanceof PageReference ) {
1693 $target = TitleValue::castPageToLinkTarget( $target );
1694 }
1695
1696 $this->mCancelTarget = $target;
1697 return $this;
1698 }
1699
1704 protected function getCancelTargetURL() {
1705 if ( is_string( $this->mCancelTarget ) ) {
1706 return $this->mCancelTarget;
1707 } else {
1708 // TODO: use a service to get the local URL for a LinkTarget, see T282283
1709 $target = Title::castFromLinkTarget( $this->mCancelTarget ) ?: Title::newMainPage();
1710 return $target->getLocalURL();
1711 }
1712 }
1713
1723 public function setTableId( $id ) {
1724 $this->mTableId = $id;
1725
1726 return $this;
1727 }
1728
1734 public function setId( $id ) {
1735 $this->mId = $id;
1736
1737 return $this;
1738 }
1739
1744 public function setName( $name ) {
1745 $this->mName = $name;
1746
1747 return $this;
1748 }
1749
1762 public function setWrapperLegend( $legend ) {
1763 $this->mWrapperLegend = $legend;
1764
1765 return $this;
1766 }
1767
1775 public function setWrapperAttributes( $attributes ) {
1776 $this->mWrapperAttributes = $attributes;
1777
1778 return $this;
1779 }
1780
1790 public function setWrapperLegendMsg( $msg ) {
1791 if ( !$msg instanceof Message ) {
1792 $msg = $this->msg( $msg );
1793 }
1794 $this->setWrapperLegend( $msg->text() );
1795
1796 return $this;
1797 }
1798
1808 public function setMessagePrefix( $p ) {
1809 $this->mMessagePrefix = $p;
1810
1811 return $this;
1812 }
1813
1821 public function setTitle( $t ) {
1822 // TODO: make mTitle a PageReference when we have a better way to get URLs, see T282283.
1823 $this->mTitle = Title::castFromPageReference( $t );
1824
1825 return $this;
1826 }
1827
1831 public function getTitle() {
1832 return $this->mTitle ?: $this->getContext()->getTitle();
1833 }
1834
1842 public function setMethod( $method = 'post' ) {
1843 $this->mMethod = strtolower( $method );
1844
1845 return $this;
1846 }
1847
1851 public function getMethod() {
1852 return $this->mMethod;
1853 }
1854
1865 protected function wrapFieldSetSection( $legend, $section, $attributes, $isRoot ) {
1866 return Html::rawElement(
1867 'fieldset',
1868 $attributes,
1869 Html::element( 'legend', [], $legend ) . $section
1870 ) . "\n";
1871 }
1872
1891 public function displaySection( $fields,
1892 $sectionName = '',
1893 $fieldsetIDPrefix = '',
1894 &$hasUserVisibleFields = false
1895 ) {
1896 if ( $this->mFieldData === null ) {
1897 throw new LogicException( 'HTMLForm::displaySection() called on uninitialized field data. '
1898 . 'You probably called displayForm() without calling prepareForm() first.' );
1899 }
1900
1901 $html = [];
1902 $subsectionHtml = '';
1903 $hasLabel = false;
1904
1905 foreach ( $fields as $key => $value ) {
1906 if ( $value instanceof HTMLFormField ) {
1907 $v = array_key_exists( $key, $this->mFieldData )
1908 ? $this->mFieldData[$key]
1909 : $value->getDefault();
1910
1911 $retval = $this->formatField( $value, $v ?? '' );
1912
1913 // check, if the form field should be added to
1914 // the output.
1915 if ( $value->hasVisibleOutput() ) {
1916 $html[] = $retval;
1917
1918 $labelValue = trim( $value->getLabel() );
1919 if ( $labelValue !== "\u{00A0}" && $labelValue !== '&#160;' && $labelValue !== '' ) {
1920 $hasLabel = true;
1921 }
1922
1923 $hasUserVisibleFields = true;
1924 }
1925 } elseif ( is_array( $value ) ) {
1926 $subsectionHasVisibleFields = false;
1927 $section =
1928 $this->displaySection( $value,
1929 "mw-htmlform-$key",
1930 "$fieldsetIDPrefix$key-",
1931 $subsectionHasVisibleFields );
1932
1933 if ( $subsectionHasVisibleFields === true ) {
1934 // Display the section with various niceties.
1935 $hasUserVisibleFields = true;
1936
1937 $legend = $this->getLegend( $key );
1938
1939 $headerHtml = $this->getHeaderHtml( $key );
1940 $footerHtml = $this->getFooterHtml( $key );
1941 $section = $headerHtml .
1942 $section .
1943 $footerHtml;
1944
1945 $attributes = [];
1946 if ( $fieldsetIDPrefix ) {
1947 $attributes['id'] = Sanitizer::escapeIdForAttribute( "$fieldsetIDPrefix$key" );
1948 }
1949 $subsectionHtml .= $this->wrapFieldSetSection(
1950 $legend, $section, $attributes, $fields === $this->mFieldTree
1951 );
1952 } else {
1953 // Just return the inputs, nothing fancy.
1954 $subsectionHtml .= $section;
1955 }
1956 }
1957 }
1958
1959 $html = $this->formatSection( $html, $sectionName, $hasLabel );
1960
1961 if ( $subsectionHtml ) {
1962 if ( $this->mSubSectionBeforeFields ) {
1963 return $subsectionHtml . "\n" . $html;
1964 } else {
1965 return $html . "\n" . $subsectionHtml;
1966 }
1967 } else {
1968 return $html;
1969 }
1970 }
1971
1980 protected function formatField( HTMLFormField $field, $value ) {
1982 switch ( $displayFormat ) {
1983 case 'table':
1984 return $field->getTableRow( $value );
1985 case 'div':
1986 return $field->getDiv( $value );
1987 case 'raw':
1988 return $field->getRaw( $value );
1989 case 'inline':
1990 return $field->getInline( $value );
1991 default:
1992 throw new LogicException( 'Not implemented' );
1993 }
1994 }
1995
2004 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
2005 if ( !$fieldsHtml ) {
2006 // Do not generate any wrappers for empty sections. Sections may be empty if they only have
2007 // subsections, but no fields. A legend will still be added in wrapFieldSetSection().
2008 return '';
2009 }
2010
2012 $html = implode( '', $fieldsHtml );
2013
2014 if ( $displayFormat === 'raw' ) {
2015 return $html;
2016 }
2017
2018 // Avoid strange spacing when no labels exist
2019 $attribs = $anyFieldHasLabel ? [] : [ 'class' => 'mw-htmlform-nolabel' ];
2020
2021 if ( $sectionName ) {
2022 $attribs['id'] = Sanitizer::escapeIdForAttribute( $sectionName );
2023 }
2024
2025 if ( $displayFormat === 'table' ) {
2026 return Html::rawElement( 'table',
2027 $attribs,
2028 Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
2029 } elseif ( $displayFormat === 'inline' ) {
2030 return Html::rawElement( 'span', $attribs, "\n$html\n" );
2031 } else {
2032 return Html::rawElement( 'div', $attribs, "\n$html\n" );
2033 }
2034 }
2035
2039 public function loadData() {
2040 $this->prepareForm();
2041 }
2042
2046 protected function loadFieldData() {
2047 $fieldData = [];
2048 $request = $this->getRequest();
2049
2050 foreach ( $this->mFlatFields as $fieldname => $field ) {
2051 if ( $field->skipLoadData( $request ) ) {
2052 continue;
2053 }
2054 if ( $field->mParams['disabled'] ?? false ) {
2055 $fieldData[$fieldname] = $field->getDefault();
2056 } else {
2057 $fieldData[$fieldname] = $field->loadDataFromRequest( $request );
2058 }
2059 }
2060
2061 // Reset to default for fields that are supposed to be disabled.
2062 // FIXME: Handle dependency chains, fields that a field checks on may need a reset too.
2063 foreach ( $fieldData as $name => &$value ) {
2064 $field = $this->mFlatFields[$name];
2065 if ( $field->isDisabled( $fieldData ) ) {
2066 $value = $field->getDefault();
2067 }
2068 }
2069
2070 # Filter data.
2071 foreach ( $fieldData as $name => &$value ) {
2072 $field = $this->mFlatFields[$name];
2073 $value = $field->filter( $value, $fieldData );
2074 }
2075
2076 $this->mFieldData = $fieldData;
2077 }
2078
2089 public function filterDataForSubmit( $data ) {
2090 return $data;
2091 }
2092
2102 public function getLegend( $key ) {
2103 return $this->msg( $this->mMessagePrefix ? "{$this->mMessagePrefix}-$key" : $key )->text();
2104 }
2105
2116 public function setAction( $action ) {
2117 $this->mAction = $action;
2118
2119 return $this;
2120 }
2121
2129 public function getAction() {
2130 // If an action is already provided, return it
2131 if ( $this->mAction !== false ) {
2132 return $this->mAction;
2133 }
2134
2135 $articlePath = $this->getConfig()->get( MainConfigNames::ArticlePath );
2136 // Check whether we are in GET mode and the ArticlePath contains a "?"
2137 // meaning that getLocalURL() would return something like "index.php?title=...".
2138 // As browser remove the query string before submitting GET forms,
2139 // it means that the title would be lost. In such case use script path instead
2140 // and put title in a hidden field (see getHiddenFields()).
2141 if ( str_contains( $articlePath, '?' ) && $this->getMethod() === 'get' ) {
2142 return $this->getConfig()->get( MainConfigNames::Script );
2143 }
2144
2145 return $this->getTitle()->getLocalURL();
2146 }
2147
2158 public function setAutocomplete( $autocomplete ) {
2159 $this->mAutocomplete = $autocomplete;
2160
2161 return $this;
2162 }
2163
2170 protected function getMessage( $value ) {
2171 return Message::newFromSpecifier( $value )->setContext( $this );
2172 }
2173
2184 foreach ( $this->mFlatFields as $field ) {
2185 if ( $field->needsJSForHtml5FormValidation() ) {
2186 return true;
2187 }
2188 }
2189 return false;
2190 }
2191}
2192
2194class_alias( HTMLForm::class, 'HTMLForm' );
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:72
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)
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage().
getContext()
Get the base IContextSource object.
The parent class to generate form fields.
getDiv( $value)
Get the complete div for the input, including help text, labels, and whatever.
getTableRow( $value)
Get the complete table row for the input, including help text, labels, and whatever.
getRaw( $value)
Get the complete raw fields for the input, including help text, labels, and whatever.
getInline( $value)
Get the complete field as an inline element.
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:214
wrapForm( $html)
Wrap the form innards in an actual "<form>" element.
displayForm( $submitResult)
Display the form (sending to the context's OutputPage object), with an appropriate error message or s...
setHeaderHtml( $html, $section=null)
Set header HTML, inside the form.
Definition HTMLForm.php:957
needsJSForHtml5FormValidation()
Whether this form, with its current fields, requires the user agent to have JavaScript enabled for th...
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
showCancel( $show=true)
Show a cancel button (or prevent it).
setMessagePrefix( $p)
Set the prefix for various default messages.
array[] $mSections
Additional information about form sections.
Definition HTMLForm.php:401
wrapFieldSetSection( $legend, $section, $attributes, $isRoot)
Wraps the given $section into a user-visible fieldset.
callable null $mSubmitCallback
Definition HTMLForm.php:288
getHeaderHtml( $section=null)
Get header HTML.
Definition HTMLForm.php:975
bool $mCollapsed
Whether the form is collapsed by default.
Definition HTMLForm.php:356
setFormIdentifier(string $ident, bool $single=false)
Set an internal identifier for this form.
trySubmit()
Validate all the fields, and call the submission callback function if everything is kosher.
Definition HTMLForm.php:766
array $availableSubclassDisplayFormats
Available formats in which to display the form.
Definition HTMLForm.php:435
setAction( $action)
Set the value for the action attribute of the form.
setPreHtml( $html)
Set the introductory message HTML, overwriting any existing message.
Definition HTMLForm.php:898
setTokenSalt( $salt)
Set the salt for the edit token.
bool bool $mSubmitLockIcon
Whether a lock icon should be attached to the submit button.
Definition HTMLForm.php:323
setFooterHtml( $html, $section=null)
Set footer HTML, inside the form.
suppressDefaultSubmit( $suppressSubmit=true)
Stop a default submit button being shown for this form.
requestIsAuthorized()
Return true if the http request passes identity and csrf checks.
Definition HTMLForm.php:694
loadFieldData()
Load data of form fields from the request.
tryAuthorizedSubmit()
Try submitting, with edit token check first.
Definition HTMLForm.php:678
wasSubmitted()
Test whether the form was considered to have been submitted or not, i.e.
Definition HTMLForm.php:855
getFormAttributes()
Get HTML attributes for the <form> tag.
static getClassFromDescriptor( $fieldname, &$descriptor)
Get the HTMLFormField subclass for this descriptor.
Definition HTMLForm.php:610
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
prepareForm()
Prepare form for submission.
Definition HTMLForm.php:659
LinkTarget string null $mCancelTarget
Definition HTMLForm.php:285
bool $mCollapsible
Whether the form can be collapsed.
Definition HTMLForm.php:349
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output Array values are discarded for security reasons (per WebRequest\getV...
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setAutocomplete( $autocomplete)
Set the value for the autocomplete attribute of the form.
getBody()
Get the whole body of the form.
bool $mSubSectionBeforeFields
If true, sections that contain both fields and subsections will render their subsections before their...
Definition HTMLForm.php:411
string $displayFormat
Format in which to display form.
Definition HTMLForm.php:418
static loadInputFromParameters( $fieldname, $descriptor, self $parent)
Initialise a new Object for the field.
Definition HTMLForm.php:639
setWrapperAttributes( $attributes)
For internal use only.
string null $mAutocomplete
Form attribute autocomplete.
Definition HTMLForm.php:363
getDisplayFormat()
Getter for displayFormat.
Definition HTMLForm.php:590
formatSection(array $fieldsHtml, $sectionName, $anyFieldHasLabel)
Put a form section together from the individual fields' HTML, merging it and wrapping.
setDisplayFormat( $format)
Set format in which to display the form.
Definition HTMLForm.php:560
string array $mTokenSalt
Salt for the edit token.
Definition HTMLForm.php:387
getHTML( $submitResult)
Returns the raw HTML generated by the form.
addButton( $data)
Add a button to the form.
getHiddenFields()
Get the hidden fields that should go inside the form.
setTableId( $id)
Set the id of the <table> or outermost <div> element.
setWrapperLegend( $legend)
Prompt the whole form to be wrapped in a "<fieldset>", with this text as its "<legend>" element.
addFooterHtml( $html, $section=null)
Add footer HTML, inside the form.
Definition HTMLForm.php:988
showAlways()
Same as self\show with the difference, that the form will be added to the output, no matter,...
Definition HTMLForm.php:745
displaySection( $fields, $sectionName='', $fieldsetIDPrefix='', &$hasUserVisibleFields=false)
addFields( $descriptor)
Add fields to the form.
Definition HTMLForm.php:502
setSubmitText( $t)
Set the text for the submit button.
setPostHtml( $html)
Set HTML at the end of the display.
getFooterHtml( $section=null)
Get footer HTML.
setTitle( $t)
Set the title for form submission.
setSubmitCallback( $cb)
Set a callback to a function to do something with the form once it's been successfully validated.
Definition HTMLForm.php:869
getSubmitText()
Get the text for the submit button, either customised or a default.
addHiddenFields(array $fields)
Add an array of hidden fields to the output Array values are discarded for security reasons (per WebR...
show()
The here's-one-I-made-earlier option: do the submission if posted, or display the form with or withou...
Definition HTMLForm.php:727
formatField(HTMLFormField $field, $value)
Generate the HTML for an individual field in the current display format.
setSections( $sections)
Set an array of information about sections.
formatErrors( $errors)
Format a stack of error messages into a single HTML string.
setValidationErrorMessage( $msg)
Set a message to display on a validation error.
Definition HTMLForm.php:884
HTMLFormField[] $mFlatFields
Definition HTMLForm.php:275
setSubmitDestructive()
Identify that the submit button in the form has a destructive action.
getErrorsOrWarnings( $elements, $elementsType)
Returns a formatted list of errors or warnings from the given elements.
addPreHtml( $html)
Add HTML to introductory message.
Definition HTMLForm.php:912
setSubmitID( $t)
Set the id for the submit button.
array $availableDisplayFormats
Available formats in which to display the form.
Definition HTMLForm.php:424
getPostHtml()
Get HTML at the end of the display.
getLegend( $key)
Get a string to go in the "<legend>" of a section fieldset.
setCancelTarget( $target)
Sets the target where the user is redirected to after clicking cancel.
setSubmitLockIcon()
Attach a "lock" OOUI icon to the submit button.
addHeaderHtml( $html, $section=null)
Add HTML to the header, inside the form.
Definition HTMLForm.php:937
static factory( $displayFormat, $descriptor, IContextSource $context, $messagePrefix='')
Construct a HTMLForm object for given display type.
Definition HTMLForm.php:459
setCollapsibleOptions( $collapsedByDefault=false)
Enable collapsible mode, and set whether the form is collapsed by default.
getAction()
Get the value for the action attribute of the form.
setMethod( $method='post')
Set the method used to submit the form.
getPreHtml()
Get the introductory message HTML.
Definition HTMLForm.php:924
getButtons()
Get the submit and (potentially) reset buttons.
static string[] $typeMappings
A mapping of 'type' inputs onto standard HTMLFormField subclasses.
Definition HTMLForm.php:218
filterDataForSubmit( $data)
Overload this if you want to apply special filtration routines to the form as a whole,...
__construct( $descriptor, IContextSource $context, $messagePrefix='')
Build a new HTMLForm from an array of field attributes.
Definition HTMLForm.php:485
string false $mAction
Form action URL.
Definition HTMLForm.php:342
addPostHtml( $html)
Add HTML to the end of the display.
Compact stacked vertical format for forms, implemented using OOUI widgets.
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition Html.php:842
const ArticlePath
Name constant for the ArticlePath setting, for use with Config\get().
const Script
Name constant for the Script setting, for use with Config\get().
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
static newFromSpecifier( $value)
Transform a MessageSpecifier or a primitive value used interchangeably with specifiers (a message key...
Definition Message.php:492
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:44
Represents a title within MediaWiki.
Definition Title.php:69
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.
Represents the target of a wiki link.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
element(SerializerNode $parent, SerializerNode $node, $contents)