HTML-Formular

From mediawiki.org
This page is a translated version of the page HTMLForm and the translation is 63% complete.
Outdated translations are marked like this.

HTML-Formular ist eine Klasse (class) für die Bearbeitung aller mit Formularen für Benutzeroberflächen zusammenhängenden Aufgaben. MediaWiki 1.16 und neuer enthalten HTML-Formular in der Form $html.

Das HTML-Formular-Tutorial erläutert die Grundlagen von HTML-Formular.

Parameter

Präzise Spezifikation findest Du für:

Parameter Art Beschreibung
type string Art des zu erstellenden Formularinhalts. Zum Beispiel: text, radio oder multiselect. Diese werden in HTML-Formularen bestimmten Unterklassen zugeordnet.

This roughly translates into the ‎<select> type attribute. If 'class' is not specified, this is used as a map through HTMLForm::$typeMappings to get the class name.

class string Unterklasse, die für diesen Formularinhalt verwendet werden soll. Dies bewirkt dasselbe wie der Typparameter, jedoch direkter. This is NOT the CSS class!
size integer Legt die Länge der Textfelder fest
maxlength integer Legt die maximale Länge von Textfeldern fest
min integer Der Mindestbetrag für den Wert
max integer Der Höchstbetrag für den Wert
invert boolean Setze die Option "Eingaben vom Typ umschalten" standardmäßig auf "aktiviert"
options array Die Optionen, die in einem Multiselect-, Radio- oder Select-Formularelement angezeigt werden sollen Maps raw text labels to values. Some field types support multi-level arrays. Overwrites 'options-message'.
rows array Die Zeilen, die in einem Checkmatrix-Formularelement angezeigt werden sollen, oder die Anzahl der Zeilen, die für das Textbereichs-Formularelement angezeigt werden
columns array Die Spalten, die in einem Checkmatrix-Formularelement dargestellt werden sollen
force-options-off array Optionen, die ein checkmatrix-Formularelement als nicht ausgewählt und deaktiviert anzeigen sollen
force-options-on array Optionen, die ein checkmatrix-Formularelement als ausgewählt und aktiviert anzeigen sollen
section string Taste für eine i18n-Nachricht, die als Abschnitts- oder Unterabschnittsüberschrift angezeigt wird Subsections should make use of '/' character e.g. foo/bar/baz creates a preference 'baz' inside the section 'bar' which is inside the section 'foo'
label-message,

buttonlabel-message

string Taste für eine i18n-Nachricht, die als Bezeichnung für eine Formulareingabe / Schaltfläche angezeigt wird Message key or object for a message to use as the label. Can be an array of msg key and then parameters to the message.
label,

label-raw, buttonlabel

string Beschriftung für eine Formulareingabe / Schaltfläche Overridden by label-message.
vertical-label boolean Setze diesen Wert auf "true", wenn die Beschriftung über den Optionen und nicht links von den Optionen angezeigt werden soll
id string ID-Attribut, das der Eingabe zugewiesen werden soll
cssclass string Klassenattribut, das der Eingabe zugewiesen werden soll
csshelpclass string CSS class used to style help text
validation-callback array Klasse und Funktion zur Eingabeüberprüfung See HTMLFormField::validate()
filter-callback array Klasse und Funktion für die Eingabefilterung It gives you the chance to massage the inputted value before it's processed. See HTMLFormField::filter()
help-message string Taste für eine i18n-Nachricht, die direkt unter dem Formularelement angezeigt wird Overwrites 'help-messages' and 'help'. Can be an array of msg key and then parameters to the message.
help string Taste für eine i18n-Nachricht, die direkt unter dem Formularelement angezeigt wird
tooltip string Schlüsselsuffix für i18n-Nachrichten zur Verwendung für Titel- und / oder Zugriffsschlüsselattribute (tooltip-YOURVALUE und accesskey-YOURVALUE)
placeholder string Für das HTML5-Platzhalterattribut zu verwendender Wert
placeholder-message string Taste für eine i18n-Nachricht, die als Platzhalter angezeigt wird
disabled boolean Deaktiviere die Bearbeitung und Übermittlung der Formulareingabe
readonly boolean Deaktiviere die Bearbeitung der Formulareingabe
required boolean Wenn die Einstellung "true" ist, kann das Eingabefeld nicht leer gelassen werden It is passed through to the object, indicating that it is a required field.
name string Überschreibe den Namen der Eingabe (Standard ist wp {$fieldname})

If you want a different name (eg one without the "wp" prefix), specify it here and it will be used without modification.

dir string Direction of the element.
'options-messages' array associative array mapping message keys to values. Some field types support multi-level arrays.

Overwrites 'options' and 'options-message'.

'options-message' array message key or object to be parsed to extract the list of options (like 'ipbreason-dropdown').
'help-messages' array array of message keys/objects. As above, each item can be an array of msg key and then parameters. Overwrites 'help'.
'help-inline' array Whether help text (defined using options above) will be shown inline after the input field, rather than in a popup.

Defaults to true. Only used by OOUI form fields.

'hide-if' array expression given as an array stating when the field should be hidden. The first array value has to be the expression's logic operator.

Suppo

[ 'NOT', array $expression ] To hide a field if a given expression is not true.

'==='

[ '===', string $fieldName, string $value ]

To hide a field if another field identified by $field has the value $value.

'!==' [ '!==', string $fieldName, string $value ]

Same as [ 'NOT', [ '===', $fieldName, $value ]

'OR', 'AND', 'NOR', 'NAND'

[ 'XXX', array $expression1, ..., array $expressionN ]

To hide a field if one or more (OR), all (AND), neither (NOR) or not all (NAND) given expressions are evaluated as true.


The expressions will be given to a JavaScript frontend module which will continually update the field's visibility.

nodata boolean if set (to any value, which casts to true), the data for this field will not be loaded from the actual request. Instead, always the default data is set as the value of this field.
default string (or array) Standardwerte für das Feld (nicht zu verwechseln mit $wgDefaultUserOptions ) Note extensions/skins adding preferences via hook must make use of UserGetDefaultOptions to set default value.

Anwendungsbeispiel

class SpecialTestForm

<?php

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() );
		$htmlForm
			->setSubmitText( 'Foo submit' )
			->setSubmitCallback( [ $this, 'trySubmit' ] )
			->show();
	}

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

		return 'Fail';
	}
}

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

i18n/en.json

{
	"@metadata": {
		"authors": [
			"MW_Kappa"
		]
	},
	"testform-desc": "some informative text for page Special:Version",
	"section1": "display text for section1",
	"subsection": "display text for subsection"
}

Anzeigeformate

Das Standardanzeigeformat für HTML-Formular ist ein table -Layout mit Beschriftungen in der linken Spalte und Eingaben in der rechten Spalte. Es ist möglich, einige andere Formate auszuwählen, z. B. $htmlForm->setDisplayFormat( 'div' ); verwendet ein div-basiertes Layout, die anderen, seltener verwendeten, sind raw und inline.

MediaWiki Version:
1.26

Um das Formular für die Verwendung des OOUI Toolkits für HTML-Formular-Elemente zu ändern, musst Du ab MediaWiki 1.26 'ooui' als Anzeigeformat in HTMLForm::factory verwenden:

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

Historische Anmerkungen

HTML Formular wurde von Werdna in r48740 als Teil seiner Überarbeitung der möglichen individuellen Einstellungen eingeführt. In der Vergangenheit enthielten MediaWikis von Version 1.4.0 bis Version 1.11.0 eine andere HTML-Formular-Klasse, die von Hashar und JeLuF geschrieben war. Dieses HTML-Formular wurde in r29245 entfernt.