*/
class HTMLForm extends ContextSource {
// A mapping of 'type' inputs onto standard HTMLFormField subclasses
- public static $typeMappings = array(
+ public static $typeMappings = [
'api' => 'HTMLApiField',
'text' => 'HTMLTextField',
'textwithbutton' => 'HTMLTextFieldWithButton',
'url' => 'HTMLTextField',
'title' => 'HTMLTitleTextField',
'user' => 'HTMLUserTextField',
- );
+ ];
public $mFieldData;
protected $mFieldTree;
protected $mShowReset = false;
protected $mShowSubmit = true;
- protected $mSubmitFlags = array( 'constructive', 'primary' );
+ protected $mSubmitFlags = [ 'constructive', 'primary' ];
protected $mSubmitCallback;
protected $mValidationErrorMessage;
protected $mPre = '';
protected $mHeader = '';
protected $mFooter = '';
- protected $mSectionHeaders = array();
- protected $mSectionFooters = array();
+ protected $mSectionHeaders = [];
+ protected $mSectionFooters = [];
protected $mPost = '';
protected $mId;
protected $mTableId = '';
protected $mAction = false;
protected $mUseMultipart = false;
- protected $mHiddenFields = array();
- protected $mButtons = array();
+ protected $mHiddenFields = [];
+ protected $mButtons = [];
protected $mWrapperLegend = false;
* Available formats in which to display the form
* @var array
*/
- protected $availableDisplayFormats = array(
+ protected $availableDisplayFormats = [
'table',
'div',
'raw',
'inline',
- );
+ ];
/**
* Available formats in which to display the form
* @var array
*/
- protected $availableSubclassDisplayFormats = array(
+ protected $availableSubclassDisplayFormats = [
'vform',
'ooui',
- );
+ ];
/**
* Construct a HTMLForm object for given display type. May return a HTMLForm subclass.
}
// Expand out into a tree.
- $loadedDescriptor = array();
- $this->mFlatFields = array();
+ $loadedDescriptor = [];
+ $this->mFlatFields = [];
foreach ( $descriptor as $fieldname => $info ) {
$section = isset( $info['section'] )
$newName = array_shift( $sectionParts );
if ( !isset( $setSection[$newName] ) ) {
- $setSection[$newName] = array();
+ $setSection[$newName] = [];
}
$setSection =& $setSection[$newName];
return false;
}
+ /**
+ * Same as self::show with the difference, that the form will be
+ * added to the output, no matter, if the validation was good or not.
+ * @return bool|Status Whether submission was successful.
+ */
+ function showAlways() {
+ $this->prepareForm();
+
+ $result = $this->tryAuthorizedSubmit();
+
+ $this->displayForm( $result );
+
+ return $result;
+ }
+
/**
* Validate all the fields, and call the submission callback
* function if everything is kosher.
*/
function trySubmit() {
$valid = true;
- $hoistedErrors = array();
+ $hoistedErrors = [];
$hoistedErrors[] = isset( $this->mValidationErrorMessage )
? $this->mValidationErrorMessage
- : array( 'htmlform-invalid-input' );
+ : [ 'htmlform-invalid-input' ];
$this->mWasSubmitted = true;
if ( $res !== true ) {
$valid = false;
if ( $res !== false && !$field->canDisplayErrors() ) {
- $hoistedErrors[] = array( 'rawmessage', $res );
+ $hoistedErrors[] = [ 'rawmessage', $res ];
}
}
}
}
/**
- * Set the introductory message, overwriting any existing message.
+ * Set the introductory message HTML, overwriting any existing message.
* @since 1.19
*
- * @param string $msg Complete text of message to display
+ * @param string $msg Complete HTML of message to display
*
* @return HTMLForm $this for chaining calls (since 1.20)
*/
}
/**
- * Add introductory text.
+ * Add HTML to introductory message.
*
- * @param string $msg Complete text of message to display
+ * @param string $msg Complete HTML of message to display
*
* @return HTMLForm $this for chaining calls (since 1.20)
*/
}
/**
- * Add header text, inside the form.
+ * Add HTML to the header, inside the form.
*
- * @param string $msg Complete text of message to display
+ * @param string $msg Additional HTML to display in header
* @param string|null $section The section to add the header to
*
* @return HTMLForm $this for chaining calls (since 1.20)
* Set header text, inside the form.
* @since 1.19
*
- * @param string $msg Complete text of message to display
+ * @param string $msg Complete HTML of header to display
* @param string|null $section The section to add the header to
*
* @return HTMLForm $this for chaining calls (since 1.20)
*
* @param string|null $section The section to get the header text for
* @since 1.26
- * @return string
+ * @return string HTML
*/
function getHeaderText( $section = null ) {
if ( is_null( $section ) ) {
*
* @return HTMLForm $this for chaining calls (since 1.20)
*/
- public function addHiddenField( $name, $value, $attribs = array() ) {
- $attribs += array( 'name' => $name );
- $this->mHiddenFields[] = array( $value, $attribs );
+ public function addHiddenField( $name, $value, $attribs = [] ) {
+ $attribs += [ 'name' => $name ];
+ $this->mHiddenFields[] = [ $value, $attribs ];
return $this;
}
*/
public function addHiddenFields( array $fields ) {
foreach ( $fields as $name => $value ) {
- $this->mHiddenFields[] = array( $value, array( 'name' => $name ) );
+ $this->mHiddenFields[] = [ $value, [ 'name' => $name ] ];
}
return $this;
/**
* Add a button to the form
*
- * @param string $name Field name.
- * @param string $value Field value
- * @param string $id DOM id for the button (default: null)
- * @param array $attribs
- *
+ * @since 1.27 takes an array as shown. Earlier versions accepted
+ * 'name', 'value', 'id', and 'attribs' as separate parameters in that
+ * order.
+ * @note Custom labels ('label', 'label-message', 'label-raw') are not
+ * supported for IE6 and IE7 due to bugs in those browsers. If detected,
+ * they will be served buttons using 'value' as the button label.
+ * @param array $data Data to define the button:
+ * - name: (string) Button name.
+ * - value: (string) Button value.
+ * - label-message: (string, optional) Button label message key to use
+ * instead of 'value'. Overrides 'label' and 'label-raw'.
+ * - label: (string, optional) Button label text to use instead of
+ * 'value'. Overrides 'label-raw'.
+ * - label-raw: (string, optional) Button label HTML to use instead of
+ * 'value'.
+ * - id: (string, optional) DOM id for the button.
+ * - attribs: (array, optional) Additional HTML attributes.
+ * - flags: (string|string[], optional) OOUI flags.
* @return HTMLForm $this for chaining calls (since 1.20)
*/
- public function addButton( $name, $value, $id = null, $attribs = null ) {
- $this->mButtons[] = compact( 'name', 'value', 'id', 'attribs' );
+ public function addButton( $data ) {
+ if ( !is_array( $data ) ) {
+ $args = func_get_args();
+ if ( count( $args ) < 2 || count( $args ) > 4 ) {
+ throw new InvalidArgumentException(
+ 'Incorrect number of arguments for deprecated calling style'
+ );
+ }
+ $data = [
+ 'name' => $args[0],
+ 'value' => $args[1],
+ 'id' => isset( $args[2] ) ? $args[2] : null,
+ 'attribs' => isset( $args[3] ) ? $args[3] : null,
+ ];
+ } else {
+ if ( !isset( $data['name'] ) ) {
+ throw new InvalidArgumentException( 'A name is required' );
+ }
+ if ( !isset( $data['value'] ) ) {
+ throw new InvalidArgumentException( 'A value is required' );
+ }
+ }
+ $this->mButtons[] = $data + [
+ 'id' => null,
+ 'attribs' => null,
+ 'flags' => null,
+ ];
return $this;
}
*
* @param bool|string|array|Status $submitResult Output from HTMLForm::trySubmit()
*
- * @return string
+ * @return string HTML
*/
function getHTML( $submitResult ) {
# For good measure (it is the default)
? 'multipart/form-data'
: 'application/x-www-form-urlencoded';
# Attributes
- $attribs = array(
+ $attribs = [
'action' => $this->getAction(),
'method' => $this->getMethod(),
'enctype' => $encType,
- );
+ ];
if ( !empty( $this->mId ) ) {
$attribs['id'] = $this->mId;
}
return Html::rawElement(
'form',
- $this->getFormAttributes() + array( 'class' => 'visualClear' ),
+ $this->getFormAttributes() + [ 'class' => 'visualClear' ],
$html
);
}
$html .= Html::hidden(
'wpEditToken',
$this->getUser()->getEditToken( $this->mTokenSalt ),
- array( 'id' => 'wpEditToken' )
+ [ 'id' => 'wpEditToken' ]
) . "\n";
$html .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
}
$useMediaWikiUIEverywhere = $this->getConfig()->get( 'UseMediaWikiUIEverywhere' );
if ( $this->mShowSubmit ) {
- $attribs = array();
+ $attribs = [];
if ( isset( $this->mSubmitID ) ) {
$attribs['id'] = $this->mSubmitID;
$attribs += Linker::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip );
}
- $attribs['class'] = array( 'mw-htmlform-submit' );
+ $attribs['class'] = [ 'mw-htmlform-submit' ];
if ( $useMediaWikiUIEverywhere ) {
foreach ( $this->mSubmitFlags as $flag ) {
if ( $this->mShowReset ) {
$buttons .= Html::element(
'input',
- array(
+ [
'type' => 'reset',
'value' => $this->msg( 'htmlform-reset' )->text(),
'class' => ( $useMediaWikiUIEverywhere ? 'mw-ui-button' : null ),
- )
+ ]
) . "\n";
}
+ // IE<8 has bugs with <button>, so we'll need to avoid them.
+ $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
+
foreach ( $this->mButtons as $button ) {
- $attrs = array(
+ $attrs = [
'type' => 'submit',
'name' => $button['name'],
'value' => $button['value']
- );
+ ];
+
+ if ( isset( $button['label-message'] ) ) {
+ $label = $this->msg( $button['label-message'] )->parse();
+ } elseif ( isset( $button['label'] ) ) {
+ $label = htmlspecialchars( $button['label'] );
+ } elseif ( isset( $button['label-raw'] ) ) {
+ $label = $button['label-raw'];
+ } else {
+ $label = htmlspecialchars( $button['value'] );
+ }
if ( $button['attribs'] ) {
$attrs += $button['attribs'];
}
if ( $useMediaWikiUIEverywhere ) {
- $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : array();
+ $attrs['class'] = isset( $attrs['class'] ) ? (array)$attrs['class'] : [];
$attrs['class'][] = 'mw-ui-button';
}
- $buttons .= Html::element( 'input', $attrs ) . "\n";
+ if ( $isBadIE ) {
+ $buttons .= Html::element( 'input', $attrs ) . "\n";
+ } else {
+ $buttons .= Html::rawElement( 'button', $attrs, $label ) . "\n";
+ }
}
$html = Html::rawElement( 'span',
- array( 'class' => 'mw-htmlform-submit-buttons' ), "\n$buttons" ) . "\n";
+ [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
return $html;
}
}
return $errorstr
- ? Html::rawElement( 'div', array( 'class' => 'error' ), $errorstr )
+ ? Html::rawElement( 'div', [ 'class' => 'error' ], $errorstr )
: '';
}
$msg = array_shift( $error );
} else {
$msg = $error;
- $error = array();
+ $error = [];
}
$errorstr .= Html::rawElement(
'li',
- array(),
+ [],
$this->msg( $msg, $error )->parse()
);
}
- $errorstr = Html::rawElement( 'ul', array(), $errorstr );
+ $errorstr = Html::rawElement( 'ul', [], $errorstr );
return $errorstr;
}
* @since 1.24
*/
public function setSubmitDestructive() {
- $this->mSubmitFlags = array( 'destructive', 'primary' );
+ $this->mSubmitFlags = [ 'destructive', 'primary' ];
}
/**
* @since 1.25
*/
public function setSubmitProgressive() {
- $this->mSubmitFlags = array( 'progressive', 'primary' );
+ $this->mSubmitFlags = [ 'progressive', 'primary' ];
}
/**
&$hasUserVisibleFields = false ) {
$displayFormat = $this->getDisplayFormat();
- $html = array();
+ $html = [];
$subsectionHtml = '';
$hasLabel = false;
// Conveniently, PHP method names are case-insensitive.
+ // For grep: this can call getDiv, getRaw, getInline, getVForm, getOOUI
$getFieldHtmlMethod = $displayFormat == 'table' ? 'getTableRow' : ( 'get' . $displayFormat );
foreach ( $fields as $key => $value ) {
$section .
$this->getFooterText( $key );
- $attributes = array();
+ $attributes = [];
if ( $fieldsetIDPrefix ) {
$attributes['id'] = Sanitizer::escapeId( "$fieldsetIDPrefix$key" );
}
return $html;
}
- $classes = array();
+ $classes = [];
if ( !$anyFieldHasLabel ) { // Avoid strange spacing when no labels exist
$classes[] = 'mw-htmlform-nolabel';
}
- $attribs = array(
+ $attribs = [
'class' => implode( ' ', $classes ),
- );
+ ];
if ( $sectionName ) {
$attribs['id'] = Sanitizer::escapeId( $sectionName );
if ( $displayFormat === 'table' ) {
return Html::rawElement( 'table',
$attribs,
- Html::rawElement( 'tbody', array(), "\n$html\n" ) ) . "\n";
+ Html::rawElement( 'tbody', [], "\n$html\n" ) ) . "\n";
} elseif ( $displayFormat === 'inline' ) {
return Html::rawElement( 'span', $attribs, "\n$html\n" );
} else {
* Construct the form fields from the Descriptor array
*/
function loadData() {
- $fieldData = array();
+ $fieldData = [];
foreach ( $this->mFlatFields as $fieldname => $field ) {
if ( !empty( $field->mParams['nodata'] ) ) {