4 * Adds a generic button inline to the form. Does not do anything, you must add
5 * click handling code in JavaScript. Use a HTMLSubmitField if you merely
6 * wish to add a submit button to a form.
8 * Additional recognized configuration parameters include:
9 * - flags: OOUI flags for the button, see OOUI\FlaggedElement
10 * - buttonlabel-message: Message to use for the button display text, instead
11 * of the value from 'default'. Overrides 'buttonlabel' and 'buttonlabel-raw'.
12 * - buttonlabel: Text to display for the button display text, instead
13 * of the value from 'default'. Overrides 'buttonlabel-raw'.
14 * - buttonlabel-raw: HTMLto display for the button display text, instead
15 * of the value from 'default'.
16 * - formnovalidate: Set to true if clicking this button should suppress
17 * client-side form validation. Used in HTMLFormFieldCloner for add/remove
20 * Note that the buttonlabel parameters are not supported on IE6 and IE7 due to
21 * bugs in those browsers. If detected, they will be served buttons using the
22 * value of 'default' as the button label.
26 class HTMLButtonField
extends HTMLFormField
{
27 protected $buttonType = 'button';
28 protected $buttonLabel = null;
30 /** @var array $mFlags Flags to add to OOUI Button widget */
31 protected $mFlags = [];
33 protected $mFormnovalidate = false;
35 public function __construct( $info ) {
36 $info['nodata'] = true;
38 $this->setShowEmptyLabel( false );
40 parent
::__construct( $info );
42 if ( isset( $info['flags'] ) ) {
43 $this->mFlags
= $info['flags'];
46 if ( isset( $info['formnovalidate'] ) ) {
47 $this->mFormnovalidate
= $info['formnovalidate'];
50 # Generate the label from a message, if possible
51 if ( isset( $info['buttonlabel-message'] ) ) {
52 $this->buttonLabel
= $this->getMessage( $info['buttonlabel-message'] )->parse();
53 } elseif ( isset( $info['buttonlabel'] ) ) {
54 if ( $info['buttonlabel'] === ' ' ||
$info['buttonlabel'] === "\u{00A0}" ) {
55 // Apparently some things set   directly and in an odd format
56 $this->buttonLabel
= "\u{00A0}";
58 $this->buttonLabel
= htmlspecialchars( $info['buttonlabel'] );
60 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
61 $this->buttonLabel
= $info['buttonlabel-raw'];
65 public function getInputHTML( $value ) {
67 $prefix = 'mw-htmlform-';
68 if ( $this->mParent
instanceof VFormHTMLForm ||
69 $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' )
72 // add mw-ui-button separately, so the descriptor doesn't need to set it
73 $flags .= ' ' . $prefix . 'button';
75 foreach ( $this->mFlags
as $flag ) {
76 $flags .= ' ' . $prefix . $flag;
79 'class' => 'mw-htmlform-submit ' . $this->mClass
. $flags,
81 'type' => $this->buttonType
,
82 'name' => $this->mName
,
83 'value' => $this->getDefault(),
84 'formnovalidate' => $this->mFormnovalidate
,
85 ] +
$this->getAttributes( [ 'disabled', 'tabindex' ] );
87 if ( $this->isBadIE() ) {
88 return Html
::element( 'input', $attr );
90 return Html
::rawElement( 'button', $attr,
91 $this->buttonLabel ?
: htmlspecialchars( $this->getDefault() ) );
96 * Get the OOUI widget for this field.
97 * @param string $value
98 * @return OOUI\ButtonInputWidget
100 public function getInputOOUI( $value ) {
101 return new OOUI\
ButtonInputWidget( [
102 'name' => $this->mName
,
103 'value' => $this->getDefault(),
104 'label' => !$this->isBadIE() && $this->buttonLabel
105 ?
new OOUI\
HtmlSnippet( $this->buttonLabel
)
106 : $this->getDefault(),
107 'type' => $this->buttonType
,
108 'classes' => [ 'mw-htmlform-submit', $this->mClass
],
110 'flags' => $this->mFlags
,
111 'useInputTag' => $this->isBadIE(),
112 ] + OOUI\Element
::configFromHtmlAttributes(
113 $this->getAttributes( [ 'disabled', 'tabindex' ] )
117 protected function needsLabel() {
122 * Button cannot be invalid
124 * @param string $value
125 * @param array $alldata
127 * @return bool|string|Message
129 public function validate( $value, $alldata ) {
134 * IE<8 has bugs with <button>, so we'll need to avoid them.
135 * @return bool Whether the request is from a bad version of IE
137 private function isBadIE() {
138 $request = $this->mParent
139 ?
$this->mParent
->getRequest()
140 : RequestContext
::getMain()->getRequest();
141 return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );