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;
37 if ( isset( $info['flags'] ) ) {
38 $this->mFlags
= $info['flags'];
41 if ( isset( $info['formnovalidate'] ) ) {
42 $this->mFormnovalidate
= $info['formnovalidate'];
45 # Generate the label from a message, if possible
46 if ( isset( $info['buttonlabel-message'] ) ) {
47 $this->buttonLabel
= $this->getMessage( $info['buttonlabel-message'] )->parse();
48 } elseif ( isset( $info['buttonlabel'] ) ) {
49 if ( $info['buttonlabel'] === ' ' ) {
50 // Apparently some things set   directly and in an odd format
51 $this->buttonLabel
= ' ';
53 $this->buttonLabel
= htmlspecialchars( $info['buttonlabel'] );
55 } elseif ( isset( $info['buttonlabel-raw'] ) ) {
56 $this->buttonLabel
= $info['buttonlabel-raw'];
59 $this->setShowEmptyLabel( false );
61 parent
::__construct( $info );
64 public function getInputHTML( $value ) {
66 $prefix = 'mw-htmlform-';
67 if ( $this->mParent
instanceof VFormHTMLForm ||
68 $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' )
71 // add mw-ui-button separately, so the descriptor doesn't need to set it
72 $flags .= ' ' . $prefix . 'button';
74 foreach ( $this->mFlags
as $flag ) {
75 $flags .= ' ' . $prefix . $flag;
78 'class' => 'mw-htmlform-submit ' . $this->mClass
. $flags,
80 'type' => $this->buttonType
,
81 'name' => $this->mName
,
82 'value' => $this->getDefault(),
83 'formnovalidate' => $this->mFormnovalidate
,
84 ] +
$this->getAttributes( [ 'disabled', 'tabindex' ] );
86 if ( $this->isBadIE() ) {
87 return Html
::element( 'input', $attr );
89 return Html
::rawElement( 'button', $attr,
90 $this->buttonLabel ?
: htmlspecialchars( $this->getDefault() ) );
95 * Get the OOUI widget for this field.
96 * @param string $value
97 * @return OOUI\ButtonInputWidget
99 public function getInputOOUI( $value ) {
100 return new OOUI\
ButtonInputWidget( [
101 'name' => $this->mName
,
102 'value' => $this->getDefault(),
103 'label' => !$this->isBadIE() && $this->buttonLabel
104 ?
new OOUI\
HtmlSnippet( $this->buttonLabel
)
105 : $this->getDefault(),
106 'type' => $this->buttonType
,
107 'classes' => [ 'mw-htmlform-submit', $this->mClass
],
109 'flags' => $this->mFlags
,
110 'useInputTag' => $this->isBadIE(),
111 ] + OOUI\Element
::configFromHtmlAttributes(
112 $this->getAttributes( [ 'disabled', 'tabindex' ] )
116 protected function needsLabel() {
121 * Button cannot be invalid
123 * @param string $value
124 * @param array $alldata
126 * @return bool|string|Message
128 public function validate( $value, $alldata ) {
133 * IE<8 has bugs with <button>, so we'll need to avoid them.
134 * @return bool Whether the request is from a bad version of IE
136 private function isBadIE() {
137 $request = $this->mParent
138 ?
$this->mParent
->getRequest()
139 : RequestContext
::getMain()->getRequest();
140 return (bool)preg_match( '/MSIE [1-7]\./i', $request->getHeader( 'User-Agent' ) );