4 * The parent class to generate form fields. Any field type should
5 * be a subclass of this.
7 abstract class HTMLFormField
{
10 protected $mValidationCallback;
11 protected $mFilterCallback;
14 protected $mLabel; # String label. Set on construction
16 protected $mClass = '';
17 protected $mVFormClass = '';
18 protected $mHelpClass = false;
20 protected $mOptions = false;
21 protected $mOptionsLabelsNotFromMessage = false;
22 protected $mHideIf = null;
25 * @var bool If true will generate an empty div element with no label
28 protected $mShowEmptyLabels = true;
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 function getInputHTML( $value );
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 function getInputOOUI( $value ) {
59 * Get a translated interface message
61 * This is a wrapper around $this->mParent->msg() if $this->mParent is set
62 * and wfMessage() otherwise.
64 * Parameters are the same as wfMessage().
69 $args = func_get_args();
71 if ( $this->mParent
) {
72 $callback = array( $this->mParent
, 'msg' );
74 $callback = 'wfMessage';
77 return call_user_func_array( $callback, $args );
82 * Fetch a field value from $alldata for the closest field matching a given
85 * This is complex because it needs to handle array fields like the user
86 * would expect. The general algorithm is to look for $name as a sibling
87 * of $this, then a sibling of $this's parent, and so on. Keeping in mind
88 * that $name itself might be referencing an array.
90 * @param array $alldata
94 protected function getNearestFieldByName( $alldata, $name ) {
97 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
98 array_unshift( $thisKeys, $m[2] );
101 if ( substr( $tmp, 0, 2 ) == 'wp' &&
102 !isset( $alldata[$tmp] ) &&
103 isset( $alldata[substr( $tmp, 2 )] )
105 // Adjust for name mangling.
106 $tmp = substr( $tmp, 2 );
108 array_unshift( $thisKeys, $tmp );
112 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
113 array_unshift( $nameKeys, $m[2] );
116 array_unshift( $nameKeys, $tmp );
119 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
120 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
123 $key = array_shift( $keys );
124 if ( !is_array( $data ) ||
!isset( $data[$key] ) ) {
129 $testValue = (string)$data;
137 * Helper function for isHidden to handle recursive data structures.
139 * @param array $alldata
140 * @param array $params
142 * @throws MWException
144 protected function isHiddenRecurse( array $alldata, array $params ) {
145 $origParams = $params;
146 $op = array_shift( $params );
151 foreach ( $params as $i => $p ) {
152 if ( !is_array( $p ) ) {
153 throw new MWException(
154 "Expected array, found " . gettype( $p ) . " at index $i"
157 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
164 foreach ( $params as $p ) {
165 if ( !is_array( $p ) ) {
166 throw new MWException(
167 "Expected array, found " . gettype( $p ) . " at index $i"
170 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
177 foreach ( $params as $i => $p ) {
178 if ( !is_array( $p ) ) {
179 throw new MWException(
180 "Expected array, found " . gettype( $p ) . " at index $i"
183 if ( !$this->isHiddenRecurse( $alldata, $p ) ) {
190 foreach ( $params as $p ) {
191 if ( !is_array( $p ) ) {
192 throw new MWException(
193 "Expected array, found " . gettype( $p ) . " at index $i"
196 if ( $this->isHiddenRecurse( $alldata, $p ) ) {
203 if ( count( $params ) !== 1 ) {
204 throw new MWException( "NOT takes exactly one parameter" );
207 if ( !is_array( $p ) ) {
208 throw new MWException(
209 "Expected array, found " . gettype( $p ) . " at index 0"
212 return !$this->isHiddenRecurse( $alldata, $p );
216 if ( count( $params ) !== 2 ) {
217 throw new MWException( "$op takes exactly two parameters" );
219 list( $field, $value ) = $params;
220 if ( !is_string( $field ) ||
!is_string( $value ) ) {
221 throw new MWException( "Parameters for $op must be strings" );
223 $testValue = $this->getNearestFieldByName( $alldata, $field );
226 return ( $value === $testValue );
228 return ( $value !== $testValue );
232 throw new MWException( "Unknown operation" );
234 } catch ( Exception
$ex ) {
235 throw new MWException(
236 "Invalid hide-if specification for $this->mName: " .
237 $ex->getMessage() . " in " . var_export( $origParams, true ),
244 * Test whether this field is supposed to be hidden, based on the values of
245 * the other form fields.
248 * @param array $alldata The data collected from the form
251 function isHidden( $alldata ) {
252 if ( !$this->mHideIf
) {
256 return $this->isHiddenRecurse( $alldata, $this->mHideIf
);
260 * Override this function if the control can somehow trigger a form
261 * submission that shouldn't actually submit the HTMLForm.
264 * @param string|array $value The value the field was submitted with
265 * @param array $alldata The data collected from the form
267 * @return bool True to cancel the submission
269 function cancelSubmit( $value, $alldata ) {
274 * Override this function to add specific validation checks on the
275 * field input. Don't forget to call parent::validate() to ensure
276 * that the user-defined callback mValidationCallback is still run
278 * @param string|array $value The value the field was submitted with
279 * @param array $alldata The data collected from the form
281 * @return bool|string True on success, or String error to display, or
282 * false to fail validation without displaying an error.
284 function validate( $value, $alldata ) {
285 if ( $this->isHidden( $alldata ) ) {
289 if ( isset( $this->mParams
['required'] )
290 && $this->mParams
['required'] !== false
293 return $this->msg( 'htmlform-required' )->parse();
296 if ( isset( $this->mValidationCallback
) ) {
297 return call_user_func( $this->mValidationCallback
, $value, $alldata, $this->mParent
);
303 function filter( $value, $alldata ) {
304 if ( isset( $this->mFilterCallback
) ) {
305 $value = call_user_func( $this->mFilterCallback
, $value, $alldata, $this->mParent
);
312 * Should this field have a label, or is there no input element with the
313 * appropriate id for the label to point to?
315 * @return bool True to output a label, false to suppress
317 protected function needsLabel() {
322 * Tell the field whether to generate a separate label element if its label
327 * @param bool $show Set to false to not generate a label.
330 public function setShowEmptyLabel( $show ) {
331 $this->mShowEmptyLabels
= $show;
335 * Get the value that this input has been set to from a posted form,
336 * or the input's default value if it has not been set.
338 * @param WebRequest $request
339 * @return string The value
341 function loadDataFromRequest( $request ) {
342 if ( $request->getCheck( $this->mName
) ) {
343 return $request->getText( $this->mName
);
345 return $this->getDefault();
350 * Initialise the object
352 * @param array $params Associative Array. See HTMLForm doc for syntax.
354 * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
355 * @throws MWException
357 function __construct( $params ) {
358 $this->mParams
= $params;
360 if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm
) {
361 $this->mParent
= $params['parent'];
364 # Generate the label from a message, if possible
365 if ( isset( $params['label-message'] ) ) {
366 $msgInfo = $params['label-message'];
368 if ( is_array( $msgInfo ) ) {
369 $msg = array_shift( $msgInfo );
375 $this->mLabel
= $this->msg( $msg, $msgInfo )->parse();
376 } elseif ( isset( $params['label'] ) ) {
377 if ( $params['label'] === ' ' ) {
378 // Apparently some things set   directly and in an odd format
379 $this->mLabel
= ' ';
381 $this->mLabel
= htmlspecialchars( $params['label'] );
383 } elseif ( isset( $params['label-raw'] ) ) {
384 $this->mLabel
= $params['label-raw'];
387 $this->mName
= "wp{$params['fieldname']}";
388 if ( isset( $params['name'] ) ) {
389 $this->mName
= $params['name'];
392 if ( isset( $params['dir'] ) ) {
393 $this->mDir
= $params['dir'];
396 $validName = Sanitizer
::escapeId( $this->mName
);
397 $validName = str_replace( array( '.5B', '.5D' ), array( '[', ']' ), $validName );
398 if ( $this->mName
!= $validName && !isset( $params['nodata'] ) ) {
399 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__
);
402 $this->mID
= "mw-input-{$this->mName}";
404 if ( isset( $params['default'] ) ) {
405 $this->mDefault
= $params['default'];
408 if ( isset( $params['id'] ) ) {
410 $validId = Sanitizer
::escapeId( $id );
412 if ( $id != $validId ) {
413 throw new MWException( "Invalid id '$id' passed to " . __METHOD__
);
419 if ( isset( $params['cssclass'] ) ) {
420 $this->mClass
= $params['cssclass'];
423 if ( isset( $params['csshelpclass'] ) ) {
424 $this->mHelpClass
= $params['csshelpclass'];
427 if ( isset( $params['validation-callback'] ) ) {
428 $this->mValidationCallback
= $params['validation-callback'];
431 if ( isset( $params['filter-callback'] ) ) {
432 $this->mFilterCallback
= $params['filter-callback'];
435 if ( isset( $params['flatlist'] ) ) {
436 $this->mClass
.= ' mw-htmlform-flatlist';
439 if ( isset( $params['hidelabel'] ) ) {
440 $this->mShowEmptyLabels
= false;
443 if ( isset( $params['hide-if'] ) ) {
444 $this->mHideIf
= $params['hide-if'];
449 * Get the complete table row for the input, including help text,
450 * labels, and whatever.
452 * @param string $value The value to set the input to.
454 * @return string Complete HTML table row.
456 function getTableRow( $value ) {
457 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
458 $inputHtml = $this->getInputHTML( $value );
459 $fieldType = get_class( $this );
460 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
461 $cellAttributes = array();
462 $rowAttributes = array();
465 if ( !empty( $this->mParams
['vertical-label'] ) ) {
466 $cellAttributes['colspan'] = 2;
467 $verticalLabel = true;
469 $verticalLabel = false;
472 $label = $this->getLabelHtml( $cellAttributes );
474 $field = Html
::rawElement(
476 array( 'class' => 'mw-input' ) +
$cellAttributes,
477 $inputHtml . "\n$errors"
480 if ( $this->mHideIf
) {
481 $rowAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
482 $rowClasses .= ' mw-htmlform-hide-if';
485 if ( $verticalLabel ) {
486 $html = Html
::rawElement( 'tr',
487 $rowAttributes +
array( 'class' => "mw-htmlform-vertical-label $rowClasses" ), $label );
488 $html .= Html
::rawElement( 'tr',
489 $rowAttributes +
array(
490 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
495 Html
::rawElement( 'tr',
496 $rowAttributes +
array(
497 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
502 return $html . $helptext;
506 * Get the complete div for the input, including help text,
507 * labels, and whatever.
510 * @param string $value The value to set the input to.
512 * @return string Complete HTML table row.
514 public function getDiv( $value ) {
515 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
516 $inputHtml = $this->getInputHTML( $value );
517 $fieldType = get_class( $this );
518 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
519 $cellAttributes = array();
520 $label = $this->getLabelHtml( $cellAttributes );
522 $outerDivClass = array(
524 'mw-htmlform-nolabel' => ( $label === '' )
527 $horizontalLabel = isset( $this->mParams
['horizontal-label'] )
528 ?
$this->mParams
['horizontal-label'] : false;
530 if ( $horizontalLabel ) {
531 $field = ' ' . $inputHtml . "\n$errors";
533 $field = Html
::rawElement(
535 array( 'class' => $outerDivClass ) +
$cellAttributes,
536 $inputHtml . "\n$errors"
539 $divCssClasses = array( "mw-htmlform-field-$fieldType",
540 $this->mClass
, $this->mVFormClass
, $errorClass );
542 $wrapperAttributes = array(
543 'class' => $divCssClasses,
545 if ( $this->mHideIf
) {
546 $wrapperAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
547 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
549 $html = Html
::rawElement( 'div', $wrapperAttributes, $label . $field );
556 * Get the OOUI version of the div. Falls back to getDiv by default.
559 * @param string $value The value to set the input to.
561 * @return OOUI\\FieldLayout|OOUI\\ActionFieldLayout
563 public function getOOUI( $value ) {
564 $inputField = $this->getInputOOUI( $value );
566 if ( !$inputField ) {
567 // This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
568 // generate the whole field, label and errors and all, then wrap it in a Widget.
569 // It might look weird, but it'll work OK.
570 return $this->getFieldLayoutOOUI(
571 new OOUI\
Widget( array( 'content' => new OOUI\
HtmlSnippet( $this->getDiv( $value ) ) ) ),
572 array( 'infusable' => false )
577 if ( is_string( $inputField ) ) {
578 // We have an OOUI implementation, but it's not proper, and we got a load of HTML.
579 // Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
580 // JavaScript doesn't know how to rebuilt the contents.
581 $inputField = new OOUI\
Widget( array( 'content' => new OOUI\
HtmlSnippet( $inputField ) ) );
585 $fieldType = get_class( $this );
586 $helpText = $this->getHelpText();
587 $errors = $this->getErrorsRaw( $value );
588 foreach ( $errors as &$error ) {
589 $error = new OOUI\
HtmlSnippet( $error );
593 'classes' => array( "mw-htmlform-field-$fieldType", $this->mClass
),
594 'align' => $this->getLabelAlignOOUI(),
595 'label' => $this->getLabel(),
596 'help' => $helpText !== null ?
new OOUI\
HtmlSnippet( $helpText ) : null,
598 'infusable' => $infusable,
601 return $this->getFieldLayoutOOUI( $inputField, $config );
605 * Get label alignment when generating field for OOUI.
606 * @return string 'left', 'right', 'top' or 'inline'
608 protected function getLabelAlignOOUI() {
613 * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
614 * @return OOUI\\FieldLayout|OOUI\\ActionFieldLayout
616 protected function getFieldLayoutOOUI( $inputField, $config ) {
617 if ( isset( $this->mClassWithButton
) ) {
618 $buttonWidget = $this->mClassWithButton
->getInputOOUI( '' );
619 return new OOUI\
ActionFieldLayout( $inputField, $buttonWidget, $config );
621 return new OOUI\
FieldLayout( $inputField, $config );
625 * Get the complete raw fields for the input, including help text,
626 * labels, and whatever.
629 * @param string $value The value to set the input to.
631 * @return string Complete HTML table row.
633 public function getRaw( $value ) {
634 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
635 $inputHtml = $this->getInputHTML( $value );
636 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
637 $cellAttributes = array();
638 $label = $this->getLabelHtml( $cellAttributes );
649 * Get the complete field for the input, including help text,
650 * labels, and whatever. Fall back from 'vform' to 'div' when not overridden.
653 * @param string $value The value to set the input to.
654 * @return string Complete HTML field.
656 public function getVForm( $value ) {
658 $this->mVFormClass
= ' mw-ui-vform-field';
659 return $this->getDiv( $value );
663 * Get the complete field as an inline element.
665 * @param string $value The value to set the input to.
666 * @return string Complete HTML inline element
668 public function getInline( $value ) {
669 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
670 $inputHtml = $this->getInputHTML( $value );
671 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
672 $cellAttributes = array();
673 $label = $this->getLabelHtml( $cellAttributes );
675 $html = "\n" . $errors .
684 * Generate help text HTML in table format
687 * @param string|null $helptext
690 public function getHelpTextHtmlTable( $helptext ) {
691 if ( is_null( $helptext ) ) {
695 $rowAttributes = array();
696 if ( $this->mHideIf
) {
697 $rowAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
698 $rowAttributes['class'] = 'mw-htmlform-hide-if';
701 $tdClasses = array( 'htmlform-tip' );
702 if ( $this->mHelpClass
!== false ) {
703 $tdClasses[] = $this->mHelpClass
;
705 $row = Html
::rawElement( 'td', array( 'colspan' => 2, 'class' => $tdClasses ), $helptext );
706 $row = Html
::rawElement( 'tr', $rowAttributes, $row );
712 * Generate help text HTML in div format
715 * @param string|null $helptext
719 public function getHelpTextHtmlDiv( $helptext ) {
720 if ( is_null( $helptext ) ) {
724 $wrapperAttributes = array(
725 'class' => 'htmlform-tip',
727 if ( $this->mHelpClass
!== false ) {
728 $wrapperAttributes['class'] .= " {$this->mHelpClass}";
730 if ( $this->mHideIf
) {
731 $wrapperAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
732 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
734 $div = Html
::rawElement( 'div', $wrapperAttributes, $helptext );
740 * Generate help text HTML formatted for raw output
743 * @param string|null $helptext
746 public function getHelpTextHtmlRaw( $helptext ) {
747 return $this->getHelpTextHtmlDiv( $helptext );
751 * Determine the help text to display
753 * @return string HTML
755 public function getHelpText() {
758 if ( isset( $this->mParams
['help-message'] ) ) {
759 $this->mParams
['help-messages'] = array( $this->mParams
['help-message'] );
762 if ( isset( $this->mParams
['help-messages'] ) ) {
763 foreach ( $this->mParams
['help-messages'] as $name ) {
764 $helpMessage = (array)$name;
765 $msg = $this->msg( array_shift( $helpMessage ), $helpMessage );
767 if ( $msg->exists() ) {
768 if ( is_null( $helptext ) ) {
771 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
773 $helptext .= $msg->parse(); // Append message
776 } elseif ( isset( $this->mParams
['help'] ) ) {
777 $helptext = $this->mParams
['help'];
784 * Determine form errors to display and their classes
787 * @param string $value The value of the input
788 * @return array array( $errors, $errorClass )
790 public function getErrorsAndErrorClass( $value ) {
791 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
793 if ( is_bool( $errors ) ||
!$this->mParent
->wasSubmitted() ) {
797 $errors = self
::formatErrors( $errors );
798 $errorClass = 'mw-htmlform-invalid-input';
801 return array( $errors, $errorClass );
805 * Determine form errors to display, returning them in an array.
808 * @param string $value The value of the input
809 * @return string[] Array of error HTML strings
811 public function getErrorsRaw( $value ) {
812 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
814 if ( is_bool( $errors ) ||
!$this->mParent
->wasSubmitted() ) {
818 if ( !is_array( $errors ) ) {
819 $errors = array( $errors );
821 foreach ( $errors as &$error ) {
822 if ( $error instanceof Message
) {
823 $error = $error->parse();
833 function getLabel() {
834 return is_null( $this->mLabel
) ?
'' : $this->mLabel
;
837 function getLabelHtml( $cellAttributes = array() ) {
838 # Don't output a for= attribute for labels with no associated input.
839 # Kind of hacky here, possibly we don't want these to be <label>s at all.
842 if ( $this->needsLabel() ) {
843 $for['for'] = $this->mID
;
846 $labelValue = trim( $this->getLabel() );
848 if ( $labelValue !== ' ' && $labelValue !== '' ) {
852 $displayFormat = $this->mParent
->getDisplayFormat();
854 $horizontalLabel = isset( $this->mParams
['horizontal-label'] )
855 ?
$this->mParams
['horizontal-label'] : false;
857 if ( $displayFormat === 'table' ) {
859 Html
::rawElement( 'td',
860 array( 'class' => 'mw-label' ) +
$cellAttributes,
861 Html
::rawElement( 'label', $for, $labelValue ) );
862 } elseif ( $hasLabel ||
$this->mShowEmptyLabels
) {
863 if ( $displayFormat === 'div' && !$horizontalLabel ) {
865 Html
::rawElement( 'div',
866 array( 'class' => 'mw-label' ) +
$cellAttributes,
867 Html
::rawElement( 'label', $for, $labelValue ) );
869 $html = Html
::rawElement( 'label', $for, $labelValue );
876 function getDefault() {
877 if ( isset( $this->mDefault
) ) {
878 return $this->mDefault
;
885 * Returns the attributes required for the tooltip and accesskey.
887 * @return array Attributes
889 public function getTooltipAndAccessKey() {
890 if ( empty( $this->mParams
['tooltip'] ) ) {
894 return Linker
::tooltipAndAccesskeyAttribs( $this->mParams
['tooltip'] );
898 * Get a translated key if necessary.
899 * @param array|null $mappings Array of mappings, 'original' => 'translated'
903 protected function getMappedKey( $mappings, $key ) {
904 if ( !is_array( $mappings ) ) {
908 if ( !empty( $mappings[$key] ) ) {
909 return $mappings[$key];
916 * Returns the given attributes from the parameters
918 * @param array $list List of attributes to get
919 * @param array $mappings Optional - Key/value map of attribute names to use instead of the ones passed in
920 * @return array Attributes
922 public function getAttributes( array $list, array $mappings = null ) {
923 static $boolAttribs = array( 'disabled', 'required', 'multiple', 'readonly' );
926 foreach ( $list as $key ) {
927 $mappedKey = $this->getMappedKey( $mappings, $key );
929 if ( in_array( $key, $boolAttribs ) ) {
930 if ( !empty( $this->mParams
[$key] ) ) {
931 $ret[$mappedKey] = '';
933 } elseif ( isset( $this->mParams
[$key] ) ) {
934 $ret[$mappedKey] = $this->mParams
[$key];
942 * Given an array of msg-key => value mappings, returns an array with keys
943 * being the message texts. It also forces values to strings.
945 * @param array $options
948 private function lookupOptionsKeys( $options ) {
950 foreach ( $options as $key => $value ) {
951 $key = $this->msg( $key )->plain();
952 $ret[$key] = is_array( $value )
953 ?
$this->lookupOptionsKeys( $value )
960 * Recursively forces values in an array to strings, because issues arise
961 * with integer 0 as a value.
963 * @param array $array
966 static function forceToStringRecursive( $array ) {
967 if ( is_array( $array ) ) {
968 return array_map( array( __CLASS__
, 'forceToStringRecursive' ), $array );
970 return strval( $array );
975 * Fetch the array of options from the field's parameters. In order, this
976 * checks 'options-messages', 'options', then 'options-message'.
978 * @return array|null Options array
980 public function getOptions() {
981 if ( $this->mOptions
=== false ) {
982 if ( array_key_exists( 'options-messages', $this->mParams
) ) {
983 $this->mOptions
= $this->lookupOptionsKeys( $this->mParams
['options-messages'] );
984 } elseif ( array_key_exists( 'options', $this->mParams
) ) {
985 $this->mOptionsLabelsNotFromMessage
= true;
986 $this->mOptions
= self
::forceToStringRecursive( $this->mParams
['options'] );
987 } elseif ( array_key_exists( 'options-message', $this->mParams
) ) {
988 /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
989 $message = $this->msg( $this->mParams
['options-message'] )->inContentLanguage()->plain();
992 $this->mOptions
= array();
993 foreach ( explode( "\n", $message ) as $option ) {
994 $value = trim( $option );
995 if ( $value == '' ) {
997 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
998 # A new group is starting...
999 $value = trim( substr( $value, 1 ) );
1001 } elseif ( substr( $value, 0, 2 ) == '**' ) {
1003 $opt = trim( substr( $value, 2 ) );
1004 if ( $optgroup === false ) {
1005 $this->mOptions
[$opt] = $opt;
1007 $this->mOptions
[$optgroup][$opt] = $opt;
1010 # groupless reason list
1012 $this->mOptions
[$option] = $option;
1016 $this->mOptions
= null;
1020 return $this->mOptions
;
1024 * Get options and make them into arrays suitable for OOUI.
1025 * @return array Options for inclusion in a select or whatever.
1027 public function getOptionsOOUI() {
1028 $oldoptions = $this->getOptions();
1030 if ( $oldoptions === null ) {
1036 foreach ( $oldoptions as $text => $data ) {
1047 * flatten an array of options to a single array, for instance,
1048 * a set of "<options>" inside "<optgroups>".
1050 * @param array $options Associative Array with values either Strings or Arrays
1051 * @return array Flattened input
1053 public static function flattenOptions( $options ) {
1054 $flatOpts = array();
1056 foreach ( $options as $value ) {
1057 if ( is_array( $value ) ) {
1058 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
1060 $flatOpts[] = $value;
1068 * Formats one or more errors as accepted by field validation-callback.
1070 * @param string|Message|array $errors Array of strings or Message instances
1071 * @return string HTML
1074 protected static function formatErrors( $errors ) {
1075 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1076 $errors = array_shift( $errors );
1079 if ( is_array( $errors ) ) {
1081 foreach ( $errors as $error ) {
1082 if ( $error instanceof Message
) {
1083 $lines[] = Html
::rawElement( 'li', array(), $error->parse() );
1085 $lines[] = Html
::rawElement( 'li', array(), $error );
1089 return Html
::rawElement( 'ul', array( 'class' => 'error' ), implode( "\n", $lines ) );
1091 if ( $errors instanceof Message
) {
1092 $errors = $errors->parse();
1095 return Html
::rawElement( 'span', array( 'class' => 'error' ), $errors );