Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / htmlform / HTMLFormField.php
blob487d6f647bbf4ebc2cc185a0a9e23d790f195ea5
1 <?php
3 /**
4 * The parent class to generate form fields. Any field type should
5 * be a subclass of this.
6 */
7 abstract class HTMLFormField {
8 public $mParams;
10 protected $mValidationCallback;
11 protected $mFilterCallback;
12 protected $mName;
13 protected $mDir;
14 protected $mLabel; # String label, as HTML. Set on construction.
15 protected $mID;
16 protected $mClass = '';
17 protected $mVFormClass = '';
18 protected $mHelpClass = false;
19 protected $mDefault;
20 protected $mOptions = false;
21 protected $mOptionsLabelsNotFromMessage = false;
22 protected $mHideIf = null;
24 /**
25 * @var bool If true will generate an empty div element with no label
26 * @since 1.22
28 protected $mShowEmptyLabels = true;
30 /**
31 * @var HTMLForm|null
33 public $mParent;
35 /**
36 * This function must be implemented to return the HTML to generate
37 * the input object itself. It should not implement the surrounding
38 * table cells/rows, or labels/help messages.
40 * @param string $value The value to set the input to; eg a default
41 * text for a text input.
43 * @return string Valid HTML.
45 abstract public function getInputHTML( $value );
47 /**
48 * Same as getInputHTML, but returns an OOUI object.
49 * Defaults to false, which getOOUI will interpret as "use the HTML version"
51 * @param string $value
52 * @return OOUI\Widget|false
54 public function getInputOOUI( $value ) {
55 return false;
58 /**
59 * True if this field type is able to display errors; false if validation errors need to be
60 * displayed in the main HTMLForm error area.
61 * @return bool
63 public function canDisplayErrors() {
64 return $this->hasVisibleOutput();
67 /**
68 * Get a translated interface message
70 * This is a wrapper around $this->mParent->msg() if $this->mParent is set
71 * and wfMessage() otherwise.
73 * Parameters are the same as wfMessage().
75 * @return Message
77 public function msg() {
78 $args = func_get_args();
80 if ( $this->mParent ) {
81 $callback = [ $this->mParent, 'msg' ];
82 } else {
83 $callback = 'wfMessage';
86 return call_user_func_array( $callback, $args );
89 /**
90 * If this field has a user-visible output or not. If not,
91 * it will not be rendered
93 * @return bool
95 public function hasVisibleOutput() {
96 return true;
99 /**
100 * Fetch a field value from $alldata for the closest field matching a given
101 * name.
103 * This is complex because it needs to handle array fields like the user
104 * would expect. The general algorithm is to look for $name as a sibling
105 * of $this, then a sibling of $this's parent, and so on. Keeping in mind
106 * that $name itself might be referencing an array.
108 * @param array $alldata
109 * @param string $name
110 * @return string
112 protected function getNearestFieldByName( $alldata, $name ) {
113 $tmp = $this->mName;
114 $thisKeys = [];
115 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
116 array_unshift( $thisKeys, $m[2] );
117 $tmp = $m[1];
119 if ( substr( $tmp, 0, 2 ) == 'wp' &&
120 !array_key_exists( $tmp, $alldata ) &&
121 array_key_exists( substr( $tmp, 2 ), $alldata )
123 // Adjust for name mangling.
124 $tmp = substr( $tmp, 2 );
126 array_unshift( $thisKeys, $tmp );
128 $tmp = $name;
129 $nameKeys = [];
130 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
131 array_unshift( $nameKeys, $m[2] );
132 $tmp = $m[1];
134 array_unshift( $nameKeys, $tmp );
136 $testValue = '';
137 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
138 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
139 $data = $alldata;
140 while ( $keys ) {
141 $key = array_shift( $keys );
142 if ( !is_array( $data ) || !array_key_exists( $key, $data ) ) {
143 continue 2;
145 $data = $data[$key];
147 $testValue = (string)$data;
148 break;
151 return $testValue;
155 * Helper function for isHidden to handle recursive data structures.
157 * @param array $alldata
158 * @param array $params
159 * @return bool
160 * @throws MWException
162 protected function isHiddenRecurse( array $alldata, array $params ) {
163 $origParams = $params;
164 $op = array_shift( $params );
166 try {
167 switch ( $op ) {
168 case 'AND':
169 foreach ( $params as $i => $p ) {
170 if ( !is_array( $p ) ) {
171 throw new MWException(
172 "Expected array, found " . gettype( $p ) . " at index $i"
175 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
176 return false;
179 return true;
181 case 'OR':
182 foreach ( $params as $i => $p ) {
183 if ( !is_array( $p ) ) {
184 throw new MWException(
185 "Expected array, found " . gettype( $p ) . " at index $i"
188 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
189 return true;
192 return false;
194 case 'NAND':
195 foreach ( $params as $i => $p ) {
196 if ( !is_array( $p ) ) {
197 throw new MWException(
198 "Expected array, found " . gettype( $p ) . " at index $i"
201 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
202 return true;
205 return false;
207 case 'NOR':
208 foreach ( $params as $i => $p ) {
209 if ( !is_array( $p ) ) {
210 throw new MWException(
211 "Expected array, found " . gettype( $p ) . " at index $i"
214 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
215 return false;
218 return true;
220 case 'NOT':
221 if ( count( $params ) !== 1 ) {
222 throw new MWException( "NOT takes exactly one parameter" );
224 $p = $params[0];
225 if ( !is_array( $p ) ) {
226 throw new MWException(
227 "Expected array, found " . gettype( $p ) . " at index 0"
230 return !$this->isHiddenRecurse( $alldata, $p );
232 case '===':
233 case '!==':
234 if ( count( $params ) !== 2 ) {
235 throw new MWException( "$op takes exactly two parameters" );
237 list( $field, $value ) = $params;
238 if ( !is_string( $field ) || !is_string( $value ) ) {
239 throw new MWException( "Parameters for $op must be strings" );
241 $testValue = $this->getNearestFieldByName( $alldata, $field );
242 switch ( $op ) {
243 case '===':
244 return ( $value === $testValue );
245 case '!==':
246 return ( $value !== $testValue );
249 default:
250 throw new MWException( "Unknown operation" );
252 } catch ( Exception $ex ) {
253 throw new MWException(
254 "Invalid hide-if specification for $this->mName: " .
255 $ex->getMessage() . " in " . var_export( $origParams, true ),
256 0, $ex
262 * Test whether this field is supposed to be hidden, based on the values of
263 * the other form fields.
265 * @since 1.23
266 * @param array $alldata The data collected from the form
267 * @return bool
269 public function isHidden( $alldata ) {
270 if ( !$this->mHideIf ) {
271 return false;
274 return $this->isHiddenRecurse( $alldata, $this->mHideIf );
278 * Override this function if the control can somehow trigger a form
279 * submission that shouldn't actually submit the HTMLForm.
281 * @since 1.23
282 * @param string|array $value The value the field was submitted with
283 * @param array $alldata The data collected from the form
285 * @return bool True to cancel the submission
287 public function cancelSubmit( $value, $alldata ) {
288 return false;
292 * Override this function to add specific validation checks on the
293 * field input. Don't forget to call parent::validate() to ensure
294 * that the user-defined callback mValidationCallback is still run
296 * @param string|array $value The value the field was submitted with
297 * @param array $alldata The data collected from the form
299 * @return bool|string|Message True on success, or String/Message error to display, or
300 * false to fail validation without displaying an error.
302 public function validate( $value, $alldata ) {
303 if ( $this->isHidden( $alldata ) ) {
304 return true;
307 if ( isset( $this->mParams['required'] )
308 && $this->mParams['required'] !== false
309 && $value === ''
311 return $this->msg( 'htmlform-required' );
314 if ( isset( $this->mValidationCallback ) ) {
315 return call_user_func( $this->mValidationCallback, $value, $alldata, $this->mParent );
318 return true;
321 public function filter( $value, $alldata ) {
322 if ( isset( $this->mFilterCallback ) ) {
323 $value = call_user_func( $this->mFilterCallback, $value, $alldata, $this->mParent );
326 return $value;
330 * Should this field have a label, or is there no input element with the
331 * appropriate id for the label to point to?
333 * @return bool True to output a label, false to suppress
335 protected function needsLabel() {
336 return true;
340 * Tell the field whether to generate a separate label element if its label
341 * is blank.
343 * @since 1.22
345 * @param bool $show Set to false to not generate a label.
346 * @return void
348 public function setShowEmptyLabel( $show ) {
349 $this->mShowEmptyLabels = $show;
353 * Can we assume that the request is an attempt to submit a HTMLForm, as opposed to an attempt to
354 * just view it? This can't normally be distinguished for e.g. checkboxes.
356 * Returns true if the request has a field for a CSRF token (wpEditToken) or a form identifier
357 * (wpFormIdentifier).
359 * @param WebRequest $request
360 * @return boolean
362 protected function isSubmitAttempt( WebRequest $request ) {
363 return $request->getCheck( 'wpEditToken' ) || $request->getCheck( 'wpFormIdentifier' );
367 * Get the value that this input has been set to from a posted form,
368 * or the input's default value if it has not been set.
370 * @param WebRequest $request
371 * @return string The value
373 public function loadDataFromRequest( $request ) {
374 if ( $request->getCheck( $this->mName ) ) {
375 return $request->getText( $this->mName );
376 } else {
377 return $this->getDefault();
382 * Initialise the object
384 * @param array $params Associative Array. See HTMLForm doc for syntax.
386 * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
387 * @throws MWException
389 public function __construct( $params ) {
390 $this->mParams = $params;
392 if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm ) {
393 $this->mParent = $params['parent'];
396 # Generate the label from a message, if possible
397 if ( isset( $params['label-message'] ) ) {
398 $this->mLabel = $this->getMessage( $params['label-message'] )->parse();
399 } elseif ( isset( $params['label'] ) ) {
400 if ( $params['label'] === '&#160;' ) {
401 // Apparently some things set &nbsp directly and in an odd format
402 $this->mLabel = '&#160;';
403 } else {
404 $this->mLabel = htmlspecialchars( $params['label'] );
406 } elseif ( isset( $params['label-raw'] ) ) {
407 $this->mLabel = $params['label-raw'];
410 $this->mName = "wp{$params['fieldname']}";
411 if ( isset( $params['name'] ) ) {
412 $this->mName = $params['name'];
415 if ( isset( $params['dir'] ) ) {
416 $this->mDir = $params['dir'];
419 $validName = Sanitizer::escapeId( $this->mName );
420 $validName = str_replace( [ '.5B', '.5D' ], [ '[', ']' ], $validName );
421 if ( $this->mName != $validName && !isset( $params['nodata'] ) ) {
422 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__ );
425 $this->mID = "mw-input-{$this->mName}";
427 if ( isset( $params['default'] ) ) {
428 $this->mDefault = $params['default'];
431 if ( isset( $params['id'] ) ) {
432 $id = $params['id'];
433 $validId = Sanitizer::escapeId( $id );
435 if ( $id != $validId ) {
436 throw new MWException( "Invalid id '$id' passed to " . __METHOD__ );
439 $this->mID = $id;
442 if ( isset( $params['cssclass'] ) ) {
443 $this->mClass = $params['cssclass'];
446 if ( isset( $params['csshelpclass'] ) ) {
447 $this->mHelpClass = $params['csshelpclass'];
450 if ( isset( $params['validation-callback'] ) ) {
451 $this->mValidationCallback = $params['validation-callback'];
454 if ( isset( $params['filter-callback'] ) ) {
455 $this->mFilterCallback = $params['filter-callback'];
458 if ( isset( $params['hidelabel'] ) ) {
459 $this->mShowEmptyLabels = false;
462 if ( isset( $params['hide-if'] ) ) {
463 $this->mHideIf = $params['hide-if'];
468 * Get the complete table row for the input, including help text,
469 * labels, and whatever.
471 * @param string $value The value to set the input to.
473 * @return string Complete HTML table row.
475 public function getTableRow( $value ) {
476 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
477 $inputHtml = $this->getInputHTML( $value );
478 $fieldType = get_class( $this );
479 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
480 $cellAttributes = [];
481 $rowAttributes = [];
482 $rowClasses = '';
484 if ( !empty( $this->mParams['vertical-label'] ) ) {
485 $cellAttributes['colspan'] = 2;
486 $verticalLabel = true;
487 } else {
488 $verticalLabel = false;
491 $label = $this->getLabelHtml( $cellAttributes );
493 $field = Html::rawElement(
494 'td',
495 [ 'class' => 'mw-input' ] + $cellAttributes,
496 $inputHtml . "\n$errors"
499 if ( $this->mHideIf ) {
500 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
501 $rowClasses .= ' mw-htmlform-hide-if';
504 if ( $verticalLabel ) {
505 $html = Html::rawElement( 'tr',
506 $rowAttributes + [ 'class' => "mw-htmlform-vertical-label $rowClasses" ], $label );
507 $html .= Html::rawElement( 'tr',
508 $rowAttributes + [
509 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
511 $field );
512 } else {
513 $html =
514 Html::rawElement( 'tr',
515 $rowAttributes + [
516 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
518 $label . $field );
521 return $html . $helptext;
525 * Get the complete div for the input, including help text,
526 * labels, and whatever.
527 * @since 1.20
529 * @param string $value The value to set the input to.
531 * @return string Complete HTML table row.
533 public function getDiv( $value ) {
534 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
535 $inputHtml = $this->getInputHTML( $value );
536 $fieldType = get_class( $this );
537 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
538 $cellAttributes = [];
539 $label = $this->getLabelHtml( $cellAttributes );
541 $outerDivClass = [
542 'mw-input',
543 'mw-htmlform-nolabel' => ( $label === '' )
546 $horizontalLabel = isset( $this->mParams['horizontal-label'] )
547 ? $this->mParams['horizontal-label'] : false;
549 if ( $horizontalLabel ) {
550 $field = '&#160;' . $inputHtml . "\n$errors";
551 } else {
552 $field = Html::rawElement(
553 'div',
554 [ 'class' => $outerDivClass ] + $cellAttributes,
555 $inputHtml . "\n$errors"
558 $divCssClasses = [ "mw-htmlform-field-$fieldType",
559 $this->mClass, $this->mVFormClass, $errorClass ];
561 $wrapperAttributes = [
562 'class' => $divCssClasses,
564 if ( $this->mHideIf ) {
565 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
566 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
568 $html = Html::rawElement( 'div', $wrapperAttributes, $label . $field );
569 $html .= $helptext;
571 return $html;
575 * Get the OOUI version of the div. Falls back to getDiv by default.
576 * @since 1.26
578 * @param string $value The value to set the input to.
580 * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
582 public function getOOUI( $value ) {
583 $inputField = $this->getInputOOUI( $value );
585 if ( !$inputField ) {
586 // This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
587 // generate the whole field, label and errors and all, then wrap it in a Widget.
588 // It might look weird, but it'll work OK.
589 return $this->getFieldLayoutOOUI(
590 new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $this->getDiv( $value ) ) ] ),
591 [ 'infusable' => false, 'align' => 'top' ]
595 $infusable = true;
596 if ( is_string( $inputField ) ) {
597 // We have an OOUI implementation, but it's not proper, and we got a load of HTML.
598 // Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
599 // JavaScript doesn't know how to rebuilt the contents.
600 $inputField = new OOUI\Widget( [ 'content' => new OOUI\HtmlSnippet( $inputField ) ] );
601 $infusable = false;
604 $fieldType = get_class( $this );
605 $help = $this->getHelpText();
606 $errors = $this->getErrorsRaw( $value );
607 foreach ( $errors as &$error ) {
608 $error = new OOUI\HtmlSnippet( $error );
611 $notices = $this->getNotices();
612 foreach ( $notices as &$notice ) {
613 $notice = new OOUI\HtmlSnippet( $notice );
616 $config = [
617 'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass ],
618 'align' => $this->getLabelAlignOOUI(),
619 'help' => ( $help !== null && $help !== '' ) ? new OOUI\HtmlSnippet( $help ) : null,
620 'errors' => $errors,
621 'notices' => $notices,
622 'infusable' => $infusable,
625 $preloadModules = false;
627 if ( $infusable && $this->shouldInfuseOOUI() ) {
628 $preloadModules = true;
629 $config['classes'][] = 'mw-htmlform-field-autoinfuse';
632 // the element could specify, that the label doesn't need to be added
633 $label = $this->getLabel();
634 if ( $label ) {
635 $config['label'] = new OOUI\HtmlSnippet( $label );
638 if ( $this->mHideIf ) {
639 $preloadModules = true;
640 $config['hideIf'] = $this->mHideIf;
643 $config['modules'] = $this->getOOUIModules();
645 if ( $preloadModules ) {
646 $this->mParent->getOutput()->addModules( 'mediawiki.htmlform.ooui' );
647 $this->mParent->getOutput()->addModules( $this->getOOUIModules() );
650 return $this->getFieldLayoutOOUI( $inputField, $config );
654 * Get label alignment when generating field for OOUI.
655 * @return string 'left', 'right', 'top' or 'inline'
657 protected function getLabelAlignOOUI() {
658 return 'top';
662 * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
663 * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
665 protected function getFieldLayoutOOUI( $inputField, $config ) {
666 if ( isset( $this->mClassWithButton ) ) {
667 $buttonWidget = $this->mClassWithButton->getInputOOUI( '' );
668 return new HTMLFormActionFieldLayout( $inputField, $buttonWidget, $config );
670 return new HTMLFormFieldLayout( $inputField, $config );
674 * Whether the field should be automatically infused. Note that all OOjs UI HTMLForm fields are
675 * infusable (you can call OO.ui.infuse() on them), but not all are infused by default, since
676 * there is no benefit in doing it e.g. for buttons and it's a small performance hit on page load.
678 * @return bool
680 protected function shouldInfuseOOUI() {
681 // Always infuse fields with help text, since the interface for it is nicer with JS
682 return $this->getHelpText() !== null;
686 * Get the list of extra ResourceLoader modules which must be loaded client-side before it's
687 * possible to infuse this field's OOjs UI widget.
689 * @return string[]
691 protected function getOOUIModules() {
692 return [];
696 * Get the complete raw fields for the input, including help text,
697 * labels, and whatever.
698 * @since 1.20
700 * @param string $value The value to set the input to.
702 * @return string Complete HTML table row.
704 public function getRaw( $value ) {
705 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
706 $inputHtml = $this->getInputHTML( $value );
707 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
708 $cellAttributes = [];
709 $label = $this->getLabelHtml( $cellAttributes );
711 $html = "\n$errors";
712 $html .= $label;
713 $html .= $inputHtml;
714 $html .= $helptext;
716 return $html;
720 * Get the complete field for the input, including help text,
721 * labels, and whatever. Fall back from 'vform' to 'div' when not overridden.
723 * @since 1.25
724 * @param string $value The value to set the input to.
725 * @return string Complete HTML field.
727 public function getVForm( $value ) {
728 // Ewwww
729 $this->mVFormClass = ' mw-ui-vform-field';
730 return $this->getDiv( $value );
734 * Get the complete field as an inline element.
735 * @since 1.25
736 * @param string $value The value to set the input to.
737 * @return string Complete HTML inline element
739 public function getInline( $value ) {
740 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
741 $inputHtml = $this->getInputHTML( $value );
742 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
743 $cellAttributes = [];
744 $label = $this->getLabelHtml( $cellAttributes );
746 $html = "\n" . $errors .
747 $label . '&#160;' .
748 $inputHtml .
749 $helptext;
751 return $html;
755 * Generate help text HTML in table format
756 * @since 1.20
758 * @param string|null $helptext
759 * @return string
761 public function getHelpTextHtmlTable( $helptext ) {
762 if ( is_null( $helptext ) ) {
763 return '';
766 $rowAttributes = [];
767 if ( $this->mHideIf ) {
768 $rowAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
769 $rowAttributes['class'] = 'mw-htmlform-hide-if';
772 $tdClasses = [ 'htmlform-tip' ];
773 if ( $this->mHelpClass !== false ) {
774 $tdClasses[] = $this->mHelpClass;
776 $row = Html::rawElement( 'td', [ 'colspan' => 2, 'class' => $tdClasses ], $helptext );
777 $row = Html::rawElement( 'tr', $rowAttributes, $row );
779 return $row;
783 * Generate help text HTML in div format
784 * @since 1.20
786 * @param string|null $helptext
788 * @return string
790 public function getHelpTextHtmlDiv( $helptext ) {
791 if ( is_null( $helptext ) ) {
792 return '';
795 $wrapperAttributes = [
796 'class' => 'htmlform-tip',
798 if ( $this->mHelpClass !== false ) {
799 $wrapperAttributes['class'] .= " {$this->mHelpClass}";
801 if ( $this->mHideIf ) {
802 $wrapperAttributes['data-hide-if'] = FormatJson::encode( $this->mHideIf );
803 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
805 $div = Html::rawElement( 'div', $wrapperAttributes, $helptext );
807 return $div;
811 * Generate help text HTML formatted for raw output
812 * @since 1.20
814 * @param string|null $helptext
815 * @return string
817 public function getHelpTextHtmlRaw( $helptext ) {
818 return $this->getHelpTextHtmlDiv( $helptext );
822 * Determine the help text to display
823 * @since 1.20
824 * @return string|null HTML
826 public function getHelpText() {
827 $helptext = null;
829 if ( isset( $this->mParams['help-message'] ) ) {
830 $this->mParams['help-messages'] = [ $this->mParams['help-message'] ];
833 if ( isset( $this->mParams['help-messages'] ) ) {
834 foreach ( $this->mParams['help-messages'] as $msg ) {
835 $msg = $this->getMessage( $msg );
837 if ( $msg->exists() ) {
838 if ( is_null( $helptext ) ) {
839 $helptext = '';
840 } else {
841 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
843 $helptext .= $msg->parse(); // Append message
846 } elseif ( isset( $this->mParams['help'] ) ) {
847 $helptext = $this->mParams['help'];
850 return $helptext;
854 * Determine form errors to display and their classes
855 * @since 1.20
857 * @param string $value The value of the input
858 * @return array array( $errors, $errorClass )
860 public function getErrorsAndErrorClass( $value ) {
861 $errors = $this->validate( $value, $this->mParent->mFieldData );
863 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
864 $errors = '';
865 $errorClass = '';
866 } else {
867 $errors = self::formatErrors( $errors );
868 $errorClass = 'mw-htmlform-invalid-input';
871 return [ $errors, $errorClass ];
875 * Determine form errors to display, returning them in an array.
877 * @since 1.26
878 * @param string $value The value of the input
879 * @return string[] Array of error HTML strings
881 public function getErrorsRaw( $value ) {
882 $errors = $this->validate( $value, $this->mParent->mFieldData );
884 if ( is_bool( $errors ) || !$this->mParent->wasSubmitted() ) {
885 $errors = [];
888 if ( !is_array( $errors ) ) {
889 $errors = [ $errors ];
891 foreach ( $errors as &$error ) {
892 if ( $error instanceof Message ) {
893 $error = $error->parse();
897 return $errors;
901 * Determine notices to display for the field.
903 * @since 1.28
904 * @return string[]
906 public function getNotices() {
907 $notices = [];
909 if ( isset( $this->mParams['notice-message'] ) ) {
910 $notices[] = $this->getMessage( $this->mParams['notice-message'] )->parse();
913 if ( isset( $this->mParams['notice-messages'] ) ) {
914 foreach ( $this->mParams['notice-messages'] as $msg ) {
915 $notices[] = $this->getMessage( $msg )->parse();
917 } elseif ( isset( $this->mParams['notice'] ) ) {
918 $notices[] = $this->mParams['notice'];
921 return $notices;
925 * @return string HTML
927 public function getLabel() {
928 return is_null( $this->mLabel ) ? '' : $this->mLabel;
931 public function getLabelHtml( $cellAttributes = [] ) {
932 # Don't output a for= attribute for labels with no associated input.
933 # Kind of hacky here, possibly we don't want these to be <label>s at all.
934 $for = [];
936 if ( $this->needsLabel() ) {
937 $for['for'] = $this->mID;
940 $labelValue = trim( $this->getLabel() );
941 $hasLabel = false;
942 if ( $labelValue !== '&#160;' && $labelValue !== '' ) {
943 $hasLabel = true;
946 $displayFormat = $this->mParent->getDisplayFormat();
947 $html = '';
948 $horizontalLabel = isset( $this->mParams['horizontal-label'] )
949 ? $this->mParams['horizontal-label'] : false;
951 if ( $displayFormat === 'table' ) {
952 $html =
953 Html::rawElement( 'td',
954 [ 'class' => 'mw-label' ] + $cellAttributes,
955 Html::rawElement( 'label', $for, $labelValue ) );
956 } elseif ( $hasLabel || $this->mShowEmptyLabels ) {
957 if ( $displayFormat === 'div' && !$horizontalLabel ) {
958 $html =
959 Html::rawElement( 'div',
960 [ 'class' => 'mw-label' ] + $cellAttributes,
961 Html::rawElement( 'label', $for, $labelValue ) );
962 } else {
963 $html = Html::rawElement( 'label', $for, $labelValue );
967 return $html;
970 public function getDefault() {
971 if ( isset( $this->mDefault ) ) {
972 return $this->mDefault;
973 } else {
974 return null;
979 * Returns the attributes required for the tooltip and accesskey.
981 * @return array Attributes
983 public function getTooltipAndAccessKey() {
984 if ( empty( $this->mParams['tooltip'] ) ) {
985 return [];
988 return Linker::tooltipAndAccesskeyAttribs( $this->mParams['tooltip'] );
992 * Returns the given attributes from the parameters
994 * @param array $list List of attributes to get
995 * @return array Attributes
997 public function getAttributes( array $list ) {
998 static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
1000 $ret = [];
1001 foreach ( $list as $key ) {
1002 if ( in_array( $key, $boolAttribs ) ) {
1003 if ( !empty( $this->mParams[$key] ) ) {
1004 $ret[$key] = '';
1006 } elseif ( isset( $this->mParams[$key] ) ) {
1007 $ret[$key] = $this->mParams[$key];
1011 return $ret;
1015 * Given an array of msg-key => value mappings, returns an array with keys
1016 * being the message texts. It also forces values to strings.
1018 * @param array $options
1019 * @return array
1021 private function lookupOptionsKeys( $options ) {
1022 $ret = [];
1023 foreach ( $options as $key => $value ) {
1024 $key = $this->msg( $key )->plain();
1025 $ret[$key] = is_array( $value )
1026 ? $this->lookupOptionsKeys( $value )
1027 : strval( $value );
1029 return $ret;
1033 * Recursively forces values in an array to strings, because issues arise
1034 * with integer 0 as a value.
1036 * @param array $array
1037 * @return array|string
1039 public static function forceToStringRecursive( $array ) {
1040 if ( is_array( $array ) ) {
1041 return array_map( [ __CLASS__, 'forceToStringRecursive' ], $array );
1042 } else {
1043 return strval( $array );
1048 * Fetch the array of options from the field's parameters. In order, this
1049 * checks 'options-messages', 'options', then 'options-message'.
1051 * @return array|null Options array
1053 public function getOptions() {
1054 if ( $this->mOptions === false ) {
1055 if ( array_key_exists( 'options-messages', $this->mParams ) ) {
1056 $this->mOptions = $this->lookupOptionsKeys( $this->mParams['options-messages'] );
1057 } elseif ( array_key_exists( 'options', $this->mParams ) ) {
1058 $this->mOptionsLabelsNotFromMessage = true;
1059 $this->mOptions = self::forceToStringRecursive( $this->mParams['options'] );
1060 } elseif ( array_key_exists( 'options-message', $this->mParams ) ) {
1061 /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
1062 $message = $this->getMessage( $this->mParams['options-message'] )->inContentLanguage()->plain();
1064 $optgroup = false;
1065 $this->mOptions = [];
1066 foreach ( explode( "\n", $message ) as $option ) {
1067 $value = trim( $option );
1068 if ( $value == '' ) {
1069 continue;
1070 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
1071 # A new group is starting...
1072 $value = trim( substr( $value, 1 ) );
1073 $optgroup = $value;
1074 } elseif ( substr( $value, 0, 2 ) == '**' ) {
1075 # groupmember
1076 $opt = trim( substr( $value, 2 ) );
1077 if ( $optgroup === false ) {
1078 $this->mOptions[$opt] = $opt;
1079 } else {
1080 $this->mOptions[$optgroup][$opt] = $opt;
1082 } else {
1083 # groupless reason list
1084 $optgroup = false;
1085 $this->mOptions[$option] = $option;
1088 } else {
1089 $this->mOptions = null;
1093 return $this->mOptions;
1097 * Get options and make them into arrays suitable for OOUI.
1098 * @return array Options for inclusion in a select or whatever.
1100 public function getOptionsOOUI() {
1101 $oldoptions = $this->getOptions();
1103 if ( $oldoptions === null ) {
1104 return null;
1107 $options = [];
1109 foreach ( $oldoptions as $text => $data ) {
1110 $options[] = [
1111 'data' => (string)$data,
1112 'label' => (string)$text,
1116 return $options;
1120 * flatten an array of options to a single array, for instance,
1121 * a set of "<options>" inside "<optgroups>".
1123 * @param array $options Associative Array with values either Strings or Arrays
1124 * @return array Flattened input
1126 public static function flattenOptions( $options ) {
1127 $flatOpts = [];
1129 foreach ( $options as $value ) {
1130 if ( is_array( $value ) ) {
1131 $flatOpts = array_merge( $flatOpts, self::flattenOptions( $value ) );
1132 } else {
1133 $flatOpts[] = $value;
1137 return $flatOpts;
1141 * Formats one or more errors as accepted by field validation-callback.
1143 * @param string|Message|array $errors Array of strings or Message instances
1144 * @return string HTML
1145 * @since 1.18
1147 protected static function formatErrors( $errors ) {
1148 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1149 $errors = array_shift( $errors );
1152 if ( is_array( $errors ) ) {
1153 $lines = [];
1154 foreach ( $errors as $error ) {
1155 if ( $error instanceof Message ) {
1156 $lines[] = Html::rawElement( 'li', [], $error->parse() );
1157 } else {
1158 $lines[] = Html::rawElement( 'li', [], $error );
1162 return Html::rawElement( 'ul', [ 'class' => 'error' ], implode( "\n", $lines ) );
1163 } else {
1164 if ( $errors instanceof Message ) {
1165 $errors = $errors->parse();
1168 return Html::rawElement( 'span', [ 'class' => 'error' ], $errors );
1173 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1174 * name + parameters array) into a Message.
1175 * @param mixed $value
1176 * @return Message
1178 protected function getMessage( $value ) {
1179 $message = Message::newFromSpecifier( $value );
1181 if ( $this->mParent ) {
1182 $message->setContext( $this->mParent );
1185 return $message;
1189 * Skip this field when collecting data.
1190 * @param WebRequest $request
1191 * @return bool
1192 * @since 1.27
1194 public function skipLoadData( $request ) {
1195 return !empty( $this->mParams['nodata'] );
1199 * Whether this field requires the user agent to have JavaScript enabled for the client-side HTML5
1200 * form validation to work correctly.
1202 * @return boolean
1203 * @since 1.29
1205 public function needsJSForHtml5FormValidation() {
1206 if ( $this->mHideIf ) {
1207 // This is probably more restrictive than it needs to be, but better safe than sorry
1208 return true;
1210 return false;