4 * HTML form generation and submission handling, OOUI style.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * http://www.gnu.org/copyleft/gpl.html
25 * Compact stacked vertical format for forms, implemented using OOUI widgets.
27 class OOUIHTMLForm
extends HTMLForm
{
30 public function __construct( $descriptor, $context = null, $messagePrefix = '' ) {
31 parent
::__construct( $descriptor, $context, $messagePrefix );
32 $this->getOutput()->enableOOUI();
33 $this->getOutput()->addModuleStyles( 'mediawiki.htmlform.ooui.styles' );
37 * Symbolic display format name.
40 protected $displayFormat = 'ooui';
42 public static function loadInputFromParameters( $fieldname, $descriptor,
43 HTMLForm
$parent = null
45 $field = parent
::loadInputFromParameters( $fieldname, $descriptor, $parent );
46 $field->setShowEmptyLabel( false );
50 function getButtons() {
53 // IE<8 has bugs with <button>, so we'll need to avoid them.
54 $isBadIE = preg_match( '/MSIE [1-7]\./i', $this->getRequest()->getHeader( 'User-Agent' ) );
56 if ( $this->mShowSubmit
) {
57 $attribs = [ 'infusable' => true ];
59 if ( isset( $this->mSubmitID
) ) {
60 $attribs['id'] = $this->mSubmitID
;
63 if ( isset( $this->mSubmitName
) ) {
64 $attribs['name'] = $this->mSubmitName
;
67 if ( isset( $this->mSubmitTooltip
) ) {
68 $attribs +
= Linker
::tooltipAndAccesskeyAttribs( $this->mSubmitTooltip
);
71 $attribs['classes'] = [ 'mw-htmlform-submit' ];
72 $attribs['type'] = 'submit';
73 $attribs['label'] = $this->getSubmitText();
74 $attribs['value'] = $this->getSubmitText();
75 $attribs['flags'] = $this->mSubmitFlags
;
76 $attribs['useInputTag'] = $isBadIE;
78 $buttons .= new OOUI\
ButtonInputWidget( $attribs );
81 if ( $this->mShowReset
) {
82 $buttons .= new OOUI\
ButtonInputWidget( [
84 'label' => $this->msg( 'htmlform-reset' )->text(),
85 'useInputTag' => $isBadIE,
89 foreach ( $this->mButtons
as $button ) {
92 if ( $button['attribs'] ) {
93 $attrs +
= $button['attribs'];
96 if ( isset( $button['id'] ) ) {
97 $attrs['id'] = $button['id'];
101 $label = $button['value'];
102 } elseif ( isset( $button['label-message'] ) ) {
103 $label = new OOUI\
HtmlSnippet( $this->getMessage( $button['label-message'] )->parse() );
104 } elseif ( isset( $button['label'] ) ) {
105 $label = $button['label'];
106 } elseif ( isset( $button['label-raw'] ) ) {
107 $label = new OOUI\
HtmlSnippet( $button['label-raw'] );
109 $label = $button['value'];
112 $attrs['classes'] = isset( $attrs['class'] ) ?
(array)$attrs['class'] : [];
114 $buttons .= new OOUI\
ButtonInputWidget( [
116 'name' => $button['name'],
117 'value' => $button['value'],
119 'flags' => $button['flags'],
120 'useInputTag' => $isBadIE,
128 return Html
::rawElement( 'div',
129 [ 'class' => 'mw-htmlform-submit-buttons' ], "\n$buttons" ) . "\n";
132 protected function wrapFieldSetSection( $legend, $section, $attributes ) {
133 // to get a user visible effect, wrap the fieldset into a framed panel layout
134 $layout = new OOUI\
PanelLayout( [
138 'infusable' => false,
141 $layout->appendContent(
142 new OOUI\
FieldsetLayout( [
144 'infusable' => false,
147 'content' => new OOUI\
HtmlSnippet( $section )
156 * Put a form section together from the individual fields' HTML, merging it and wrapping.
157 * @param OOUI\FieldLayout[] $fieldsHtml
158 * @param string $sectionName
159 * @param bool $anyFieldHasLabel Unused
160 * @return string HTML
162 protected function formatSection( array $fieldsHtml, $sectionName, $anyFieldHasLabel ) {
164 'items' => $fieldsHtml,
166 if ( $sectionName ) {
167 $config['id'] = Sanitizer
::escapeId( $sectionName );
169 if ( is_string( $this->mWrapperLegend
) ) {
170 $config['label'] = $this->mWrapperLegend
;
172 return new OOUI\
FieldsetLayout( $config );
176 * @param string|array|Status $err
179 function getErrors( $err ) {
182 } elseif ( $err instanceof Status
) {
183 if ( $err->isOK() ) {
186 $errors = $err->getErrorsByType( 'error' );
187 foreach ( $errors as &$error ) {
188 // Input: array( 'message' => 'foo', 'errors' => array( 'a', 'b', 'c' ) )
189 // Output: array( 'foo', 'a', 'b', 'c' )
190 $error = array_merge( [ $error['message'] ], $error['params'] );
195 if ( !is_array( $errors ) ) {
196 $errors = [ $errors ];
200 foreach ( $errors as &$error ) {
201 $error = $this->getMessage( $error )->parse();
202 $error = new OOUI\
HtmlSnippet( $error );
206 $this->oouiErrors
= $errors;
210 function getHeaderText( $section = null ) {
211 if ( is_null( $section ) ) {
212 // We handle $this->mHeader elsewhere, in getBody()
215 return parent
::getHeaderText( $section );
220 $fieldset = parent
::getBody();
221 // FIXME This only works for forms with no subsections
222 if ( $fieldset instanceof OOUI\FieldsetLayout
) {
223 $classes = [ 'mw-htmlform-ooui-header' ];
224 if ( !$this->mHeader
) {
225 $classes[] = 'mw-htmlform-ooui-header-empty';
227 if ( $this->oouiErrors
) {
228 $classes[] = 'mw-htmlform-ooui-header-errors';
230 $fieldset->addItems( [
231 new OOUI\
FieldLayout(
232 new OOUI\
LabelWidget( [ 'label' => new OOUI\
HtmlSnippet( $this->mHeader
) ] ),
235 'errors' => $this->oouiErrors
,
236 'classes' => $classes,
244 function wrapForm( $html ) {
245 $form = new OOUI\
FormLayout( $this->getFormAttributes() +
[
246 'classes' => [ 'mw-htmlform-ooui' ],
247 'content' => new OOUI\
HtmlSnippet( $html ),
250 // Include a wrapper for style, if requested.
251 $form = new OOUI\
PanelLayout( [
252 'classes' => [ 'mw-htmlform-ooui-wrapper' ],
254 'padded' => $this->mWrapperLegend
!== false,
255 'framed' => $this->mWrapperLegend
!== false,