Jump to navigation Jump to search
This page is a translated version of the page HTMLForm and the translation is 27% complete.
Outdated translations are marked like this.
Other languages:
Deutsch • ‎English • ‎dansk • ‎español • ‎français • ‎polski • ‎português do Brasil • ‎русский • ‎中文 • ‎日本語

HTMLForm is a class for handling everything related to user interface forms. MediaWiki 1.16 and newer include HTMLForm in HTMLForm.php.

The HTMLForm tutorial shows basics of HTMLForm.


Точная спецификация для:

Параметр Тип Описание
type string Тип содержимого формы для создания. Например: text, radio, или multiselect. Они отображаются в конкретных подклассах в HtmlForm.
class string Subclass to use for this form content. This accomplishes the same thing as the type parameter, but more directly.
size integer Устанавливает длину текстовых полей
maxlength integer Устанавливает максимальную длину текстовых полей
min integer Минимальная сумма для значения
max integer Максимальная сумма для значения
invert boolean Set inputs of type toggle to checked by default
options array The options to present in a multiselect, radio, or select form element
rows array The rows to present in a checkmatrix form element or the number of rows shown for textarea form element
columns array The columns to present in a checkmatrix form element
force-options-off array Options to set unchecked and disabled in a checkmatrix form element
force-options-on array Options to set checked and enabled in a checkmatrix form element
section string Key for an i18n message to display as a section or subsection header


string Key for an i18n message to display as a label for a form input/button

label-raw, buttonlabel

string Label for a form input/button
vertical-label boolean Set to true if you want the label to appear above the options rather than to the left of the options
id string ID attribute to assign to the input
cssclass string Class attribute to assign to the input
validation-callback array Class and function to use for input validation
filter-callback array Class and function to use for input filtering
help-message string Key for an i18n message to display directly below the form element
help string Message to display directly below the form element
tooltip string Key suffix for i18n messages to use for title and/or accesskey attributes (tooltip-YOURVALUE and accesskey-YOURVALUE)
placeholder string Value to use for the HTML5 placeholder attribute
placeholder-message string Key for an i18n message to display as a placeholder
disabled boolean Disable editing and submission of the form input
readonly boolean Disable editing of the form input
required boolean If true, the input cannot be left blank
name string Override the name of the input (default is wp{$fieldname})
default string (or array) Default value(s) for the field (not to be confused with $wgDefaultUserOptions)

Пример использования


class SpecialTestForm extends SpecialPage {
	public function __construct() {
		parent::__construct( 'TestForm' );

	public function execute( $par ) {
		$this->getOutput()->setPageTitle( 'Test form' );

		$formDescriptor = [
			'myfield1' => [
				'section' => 'section1/subsection',
				'label-message' => 'testform-myfield1',
				'type' => 'text',
				'default' => 'Meep',
			'myfield2' => [
				'section' => 'section1',
				'class' => 'HTMLTextField', // HTMLTextField same as type 'text'
				'label-message' => 'testform-myfield2',
			'myfield3' => [
				'class' => 'HTMLTextField',
				'label' => 'Foo bar baz',
			'myfield4' => [
				'class' => 'HTMLCheckField',
				'label' => 'This be a pirate checkbox',
				'default' => true,
			'omgaselectbox' => [
				'class' => 'HTMLSelectField',
				'label' => 'Select an oooption',
				'options' => [
					'Pirates' => 'pirate',
					'Ninjas' => 'ninja',
					'Back to the NINJAR!' => 'ninjars',
			'omgmultiselect' => [
				'class' => 'HTMLMultiSelectField',
				'label' => 'Weapons to use',
				'options' => [
					'Cannons' => 'cannon',
					'Swords' => 'sword',
				'default' => [ 'sword' ],
			'radiolol' => [
				'class' => 'HTMLRadioField',
				'label' => 'Who do you like?',
				'options' => [
					'Pirates' => 'pirates',
					'Ninjas' => 'ninjas',
					'Both' => 'both',
				'default' => 'pirates',

		$htmlForm = new HTMLForm( $formDescriptor, $this->getContext() );
			->setSubmitText( 'Foo submit' )
			->setSubmitCallback( [ $this, 'trySubmit' ] )

	public function trySubmit( $formData ) {
		if ( $formData[ 'myfield1' ] === 'Fleep' ) {
			return true;

		return 'Fail';

$wgSpecialPages['TestForm'] = 'SpecialTestForm';

Отображаемые форматы

The default display format for HTMLForm is a table layout, with labels in the left column and inputs in the right column. It is possible to select a few different formats e.g. $htmlForm->setDisplayFormat( 'div' ); will use a div-based layout, the others more rare ones are raw and inline.

Версия MediaWiki: 1.26

To change the form to use the OOUI toolkit for HTMLform elements, starting with MediaWiki 1.26, you'll need to use 'ooui' as display format in HTMLForm::factory:

$htmlForm = HTMLForm::factory( 'ooui', $formDescriptor, $this->getContext() );

Historical notes

HTMLForm was introduced by Werdna in r48740 as a part of his preferences system rewrite. Historically MediaWikis from version 1.4.0 to version 1.11.0 included a different HTMLForm class, written by Hashar and JeLuF. This HTMLForm was removed in r29245.