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, as HTML. 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 * 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.
63 public function canDisplayErrors() {
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().
78 $args = func_get_args();
80 if ( $this->mParent
) {
81 $callback = [ $this->mParent
, 'msg' ];
83 $callback = 'wfMessage';
86 return call_user_func_array( $callback, $args );
90 * If this field has a user-visible output or not. If not,
91 * it will not be rendered
95 public function hasVisibleOutput() {
100 * Fetch a field value from $alldata for the closest field matching a given
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
112 protected function getNearestFieldByName( $alldata, $name ) {
115 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
116 array_unshift( $thisKeys, $m[2] );
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 );
130 while ( preg_match( '/^(.+)\[([^\]]+)\]$/', $tmp, $m ) ) {
131 array_unshift( $nameKeys, $m[2] );
134 array_unshift( $nameKeys, $tmp );
137 for ( $i = count( $thisKeys ) - 1; $i >= 0; $i-- ) {
138 $keys = array_merge( array_slice( $thisKeys, 0, $i ), $nameKeys );
141 $key = array_shift( $keys );
142 if ( !is_array( $data ) ||
!array_key_exists( $key, $data ) ) {
147 $testValue = (string)$data;
155 * Helper function for isHidden to handle recursive data structures.
157 * @param array $alldata
158 * @param array $params
160 * @throws MWException
162 protected function isHiddenRecurse( array $alldata, array $params ) {
163 $origParams = $params;
164 $op = array_shift( $params );
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 ) ) {
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 ) ) {
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 ) ) {
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 ) ) {
221 if ( count( $params ) !== 1 ) {
222 throw new MWException( "NOT takes exactly one parameter" );
225 if ( !is_array( $p ) ) {
226 throw new MWException(
227 "Expected array, found " . gettype( $p ) . " at index 0"
230 return !$this->isHiddenRecurse( $alldata, $p );
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 );
244 return ( $value === $testValue );
246 return ( $value !== $testValue );
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 ),
262 * Test whether this field is supposed to be hidden, based on the values of
263 * the other form fields.
266 * @param array $alldata The data collected from the form
269 function isHidden( $alldata ) {
270 if ( !$this->mHideIf
) {
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.
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 function cancelSubmit( $value, $alldata ) {
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 True on success, or String error to display, or
300 * false to fail validation without displaying an error.
302 function validate( $value, $alldata ) {
303 if ( $this->isHidden( $alldata ) ) {
307 if ( isset( $this->mParams
['required'] )
308 && $this->mParams
['required'] !== false
311 return $this->msg( 'htmlform-required' )->parse();
314 if ( isset( $this->mValidationCallback
) ) {
315 return call_user_func( $this->mValidationCallback
, $value, $alldata, $this->mParent
);
321 function filter( $value, $alldata ) {
322 if ( isset( $this->mFilterCallback
) ) {
323 $value = call_user_func( $this->mFilterCallback
, $value, $alldata, $this->mParent
);
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() {
340 * Tell the field whether to generate a separate label element if its label
345 * @param bool $show Set to false to not generate a label.
348 public function setShowEmptyLabel( $show ) {
349 $this->mShowEmptyLabels
= $show;
353 * Get the value that this input has been set to from a posted form,
354 * or the input's default value if it has not been set.
356 * @param WebRequest $request
357 * @return string The value
359 function loadDataFromRequest( $request ) {
360 if ( $request->getCheck( $this->mName
) ) {
361 return $request->getText( $this->mName
);
363 return $this->getDefault();
368 * Initialise the object
370 * @param array $params Associative Array. See HTMLForm doc for syntax.
372 * @since 1.22 The 'label' attribute no longer accepts raw HTML, use 'label-raw' instead
373 * @throws MWException
375 function __construct( $params ) {
376 $this->mParams
= $params;
378 if ( isset( $params['parent'] ) && $params['parent'] instanceof HTMLForm
) {
379 $this->mParent
= $params['parent'];
382 # Generate the label from a message, if possible
383 if ( isset( $params['label-message'] ) ) {
384 $this->mLabel
= $this->getMessage( $params['label-message'] )->parse();
385 } elseif ( isset( $params['label'] ) ) {
386 if ( $params['label'] === ' ' ) {
387 // Apparently some things set   directly and in an odd format
388 $this->mLabel
= ' ';
390 $this->mLabel
= htmlspecialchars( $params['label'] );
392 } elseif ( isset( $params['label-raw'] ) ) {
393 $this->mLabel
= $params['label-raw'];
396 $this->mName
= "wp{$params['fieldname']}";
397 if ( isset( $params['name'] ) ) {
398 $this->mName
= $params['name'];
401 if ( isset( $params['dir'] ) ) {
402 $this->mDir
= $params['dir'];
405 $validName = Sanitizer
::escapeId( $this->mName
);
406 $validName = str_replace( [ '.5B', '.5D' ], [ '[', ']' ], $validName );
407 if ( $this->mName
!= $validName && !isset( $params['nodata'] ) ) {
408 throw new MWException( "Invalid name '{$this->mName}' passed to " . __METHOD__
);
411 $this->mID
= "mw-input-{$this->mName}";
413 if ( isset( $params['default'] ) ) {
414 $this->mDefault
= $params['default'];
417 if ( isset( $params['id'] ) ) {
419 $validId = Sanitizer
::escapeId( $id );
421 if ( $id != $validId ) {
422 throw new MWException( "Invalid id '$id' passed to " . __METHOD__
);
428 if ( isset( $params['cssclass'] ) ) {
429 $this->mClass
= $params['cssclass'];
432 if ( isset( $params['csshelpclass'] ) ) {
433 $this->mHelpClass
= $params['csshelpclass'];
436 if ( isset( $params['validation-callback'] ) ) {
437 $this->mValidationCallback
= $params['validation-callback'];
440 if ( isset( $params['filter-callback'] ) ) {
441 $this->mFilterCallback
= $params['filter-callback'];
444 if ( isset( $params['flatlist'] ) ) {
445 $this->mClass
.= ' mw-htmlform-flatlist';
448 if ( isset( $params['hidelabel'] ) ) {
449 $this->mShowEmptyLabels
= false;
452 if ( isset( $params['hide-if'] ) ) {
453 $this->mHideIf
= $params['hide-if'];
458 * Get the complete table row for the input, including help text,
459 * labels, and whatever.
461 * @param string $value The value to set the input to.
463 * @return string Complete HTML table row.
465 function getTableRow( $value ) {
466 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
467 $inputHtml = $this->getInputHTML( $value );
468 $fieldType = get_class( $this );
469 $helptext = $this->getHelpTextHtmlTable( $this->getHelpText() );
470 $cellAttributes = [];
474 if ( !empty( $this->mParams
['vertical-label'] ) ) {
475 $cellAttributes['colspan'] = 2;
476 $verticalLabel = true;
478 $verticalLabel = false;
481 $label = $this->getLabelHtml( $cellAttributes );
483 $field = Html
::rawElement(
485 [ 'class' => 'mw-input' ] +
$cellAttributes,
486 $inputHtml . "\n$errors"
489 if ( $this->mHideIf
) {
490 $rowAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
491 $rowClasses .= ' mw-htmlform-hide-if';
494 if ( $verticalLabel ) {
495 $html = Html
::rawElement( 'tr',
496 $rowAttributes +
[ 'class' => "mw-htmlform-vertical-label $rowClasses" ], $label );
497 $html .= Html
::rawElement( 'tr',
499 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
504 Html
::rawElement( 'tr',
506 'class' => "mw-htmlform-field-$fieldType {$this->mClass} $errorClass $rowClasses"
511 return $html . $helptext;
515 * Get the complete div for the input, including help text,
516 * labels, and whatever.
519 * @param string $value The value to set the input to.
521 * @return string Complete HTML table row.
523 public function getDiv( $value ) {
524 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
525 $inputHtml = $this->getInputHTML( $value );
526 $fieldType = get_class( $this );
527 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
528 $cellAttributes = [];
529 $label = $this->getLabelHtml( $cellAttributes );
533 'mw-htmlform-nolabel' => ( $label === '' )
536 $horizontalLabel = isset( $this->mParams
['horizontal-label'] )
537 ?
$this->mParams
['horizontal-label'] : false;
539 if ( $horizontalLabel ) {
540 $field = ' ' . $inputHtml . "\n$errors";
542 $field = Html
::rawElement(
544 [ 'class' => $outerDivClass ] +
$cellAttributes,
545 $inputHtml . "\n$errors"
548 $divCssClasses = [ "mw-htmlform-field-$fieldType",
549 $this->mClass
, $this->mVFormClass
, $errorClass ];
551 $wrapperAttributes = [
552 'class' => $divCssClasses,
554 if ( $this->mHideIf
) {
555 $wrapperAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
556 $wrapperAttributes['class'][] = ' mw-htmlform-hide-if';
558 $html = Html
::rawElement( 'div', $wrapperAttributes, $label . $field );
565 * Get the OOUI version of the div. Falls back to getDiv by default.
568 * @param string $value The value to set the input to.
570 * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
572 public function getOOUI( $value ) {
573 $inputField = $this->getInputOOUI( $value );
575 if ( !$inputField ) {
576 // This field doesn't have an OOUI implementation yet at all. Fall back to getDiv() to
577 // generate the whole field, label and errors and all, then wrap it in a Widget.
578 // It might look weird, but it'll work OK.
579 return $this->getFieldLayoutOOUI(
580 new OOUI\
Widget( [ 'content' => new OOUI\
HtmlSnippet( $this->getDiv( $value ) ) ] ),
581 [ 'infusable' => false, 'align' => 'top' ]
586 if ( is_string( $inputField ) ) {
587 // We have an OOUI implementation, but it's not proper, and we got a load of HTML.
588 // Cheat a little and wrap it in a widget. It won't be infusable, though, since client-side
589 // JavaScript doesn't know how to rebuilt the contents.
590 $inputField = new OOUI\
Widget( [ 'content' => new OOUI\
HtmlSnippet( $inputField ) ] );
594 $fieldType = get_class( $this );
595 $helpText = $this->getHelpText();
596 $errors = $this->getErrorsRaw( $value );
597 foreach ( $errors as &$error ) {
598 $error = new OOUI\
HtmlSnippet( $error );
602 'classes' => [ "mw-htmlform-field-$fieldType", $this->mClass
],
603 'align' => $this->getLabelAlignOOUI(),
604 'help' => $helpText !== null ?
new OOUI\
HtmlSnippet( $helpText ) : null,
606 'infusable' => $infusable,
609 // the element could specify, that the label doesn't need to be added
610 $label = $this->getLabel();
612 $config['label'] = new OOUI\
HtmlSnippet( $label );
615 return $this->getFieldLayoutOOUI( $inputField, $config );
619 * Get label alignment when generating field for OOUI.
620 * @return string 'left', 'right', 'top' or 'inline'
622 protected function getLabelAlignOOUI() {
627 * Get a FieldLayout (or subclass thereof) to wrap this field in when using OOUI output.
628 * @return OOUI\FieldLayout|OOUI\ActionFieldLayout
630 protected function getFieldLayoutOOUI( $inputField, $config ) {
631 if ( isset( $this->mClassWithButton
) ) {
632 $buttonWidget = $this->mClassWithButton
->getInputOOUI( '' );
633 return new OOUI\
ActionFieldLayout( $inputField, $buttonWidget, $config );
635 return new OOUI\
FieldLayout( $inputField, $config );
639 * Get the complete raw fields for the input, including help text,
640 * labels, and whatever.
643 * @param string $value The value to set the input to.
645 * @return string Complete HTML table row.
647 public function getRaw( $value ) {
648 list( $errors, ) = $this->getErrorsAndErrorClass( $value );
649 $inputHtml = $this->getInputHTML( $value );
650 $helptext = $this->getHelpTextHtmlRaw( $this->getHelpText() );
651 $cellAttributes = [];
652 $label = $this->getLabelHtml( $cellAttributes );
663 * Get the complete field for the input, including help text,
664 * labels, and whatever. Fall back from 'vform' to 'div' when not overridden.
667 * @param string $value The value to set the input to.
668 * @return string Complete HTML field.
670 public function getVForm( $value ) {
672 $this->mVFormClass
= ' mw-ui-vform-field';
673 return $this->getDiv( $value );
677 * Get the complete field as an inline element.
679 * @param string $value The value to set the input to.
680 * @return string Complete HTML inline element
682 public function getInline( $value ) {
683 list( $errors, $errorClass ) = $this->getErrorsAndErrorClass( $value );
684 $inputHtml = $this->getInputHTML( $value );
685 $helptext = $this->getHelpTextHtmlDiv( $this->getHelpText() );
686 $cellAttributes = [];
687 $label = $this->getLabelHtml( $cellAttributes );
689 $html = "\n" . $errors .
698 * Generate help text HTML in table format
701 * @param string|null $helptext
704 public function getHelpTextHtmlTable( $helptext ) {
705 if ( is_null( $helptext ) ) {
710 if ( $this->mHideIf
) {
711 $rowAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
712 $rowAttributes['class'] = 'mw-htmlform-hide-if';
715 $tdClasses = [ 'htmlform-tip' ];
716 if ( $this->mHelpClass
!== false ) {
717 $tdClasses[] = $this->mHelpClass
;
719 $row = Html
::rawElement( 'td', [ 'colspan' => 2, 'class' => $tdClasses ], $helptext );
720 $row = Html
::rawElement( 'tr', $rowAttributes, $row );
726 * Generate help text HTML in div format
729 * @param string|null $helptext
733 public function getHelpTextHtmlDiv( $helptext ) {
734 if ( is_null( $helptext ) ) {
738 $wrapperAttributes = [
739 'class' => 'htmlform-tip',
741 if ( $this->mHelpClass
!== false ) {
742 $wrapperAttributes['class'] .= " {$this->mHelpClass}";
744 if ( $this->mHideIf
) {
745 $wrapperAttributes['data-hide-if'] = FormatJson
::encode( $this->mHideIf
);
746 $wrapperAttributes['class'] .= ' mw-htmlform-hide-if';
748 $div = Html
::rawElement( 'div', $wrapperAttributes, $helptext );
754 * Generate help text HTML formatted for raw output
757 * @param string|null $helptext
760 public function getHelpTextHtmlRaw( $helptext ) {
761 return $this->getHelpTextHtmlDiv( $helptext );
765 * Determine the help text to display
767 * @return string HTML
769 public function getHelpText() {
772 if ( isset( $this->mParams
['help-message'] ) ) {
773 $this->mParams
['help-messages'] = [ $this->mParams
['help-message'] ];
776 if ( isset( $this->mParams
['help-messages'] ) ) {
777 foreach ( $this->mParams
['help-messages'] as $msg ) {
778 $msg = $this->getMessage( $msg );
780 if ( $msg->exists() ) {
781 if ( is_null( $helptext ) ) {
784 $helptext .= $this->msg( 'word-separator' )->escaped(); // some space
786 $helptext .= $msg->parse(); // Append message
789 } elseif ( isset( $this->mParams
['help'] ) ) {
790 $helptext = $this->mParams
['help'];
797 * Determine form errors to display and their classes
800 * @param string $value The value of the input
801 * @return array array( $errors, $errorClass )
803 public function getErrorsAndErrorClass( $value ) {
804 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
806 if ( is_bool( $errors ) ||
!$this->mParent
->wasSubmitted() ) {
810 $errors = self
::formatErrors( $errors );
811 $errorClass = 'mw-htmlform-invalid-input';
814 return [ $errors, $errorClass ];
818 * Determine form errors to display, returning them in an array.
821 * @param string $value The value of the input
822 * @return string[] Array of error HTML strings
824 public function getErrorsRaw( $value ) {
825 $errors = $this->validate( $value, $this->mParent
->mFieldData
);
827 if ( is_bool( $errors ) ||
!$this->mParent
->wasSubmitted() ) {
831 if ( !is_array( $errors ) ) {
832 $errors = [ $errors ];
834 foreach ( $errors as &$error ) {
835 if ( $error instanceof Message
) {
836 $error = $error->parse();
844 * @return string HTML
846 function getLabel() {
847 return is_null( $this->mLabel
) ?
'' : $this->mLabel
;
850 function getLabelHtml( $cellAttributes = [] ) {
851 # Don't output a for= attribute for labels with no associated input.
852 # Kind of hacky here, possibly we don't want these to be <label>s at all.
855 if ( $this->needsLabel() ) {
856 $for['for'] = $this->mID
;
859 $labelValue = trim( $this->getLabel() );
861 if ( $labelValue !== ' ' && $labelValue !== '' ) {
865 $displayFormat = $this->mParent
->getDisplayFormat();
867 $horizontalLabel = isset( $this->mParams
['horizontal-label'] )
868 ?
$this->mParams
['horizontal-label'] : false;
870 if ( $displayFormat === 'table' ) {
872 Html
::rawElement( 'td',
873 [ 'class' => 'mw-label' ] +
$cellAttributes,
874 Html
::rawElement( 'label', $for, $labelValue ) );
875 } elseif ( $hasLabel ||
$this->mShowEmptyLabels
) {
876 if ( $displayFormat === 'div' && !$horizontalLabel ) {
878 Html
::rawElement( 'div',
879 [ 'class' => 'mw-label' ] +
$cellAttributes,
880 Html
::rawElement( 'label', $for, $labelValue ) );
882 $html = Html
::rawElement( 'label', $for, $labelValue );
889 function getDefault() {
890 if ( isset( $this->mDefault
) ) {
891 return $this->mDefault
;
898 * Returns the attributes required for the tooltip and accesskey.
900 * @return array Attributes
902 public function getTooltipAndAccessKey() {
903 if ( empty( $this->mParams
['tooltip'] ) ) {
907 return Linker
::tooltipAndAccesskeyAttribs( $this->mParams
['tooltip'] );
911 * Returns the given attributes from the parameters
913 * @param array $list List of attributes to get
914 * @return array Attributes
916 public function getAttributes( array $list ) {
917 static $boolAttribs = [ 'disabled', 'required', 'autofocus', 'multiple', 'readonly' ];
920 foreach ( $list as $key ) {
921 if ( in_array( $key, $boolAttribs ) ) {
922 if ( !empty( $this->mParams
[$key] ) ) {
925 } elseif ( isset( $this->mParams
[$key] ) ) {
926 $ret[$key] = $this->mParams
[$key];
934 * Given an array of msg-key => value mappings, returns an array with keys
935 * being the message texts. It also forces values to strings.
937 * @param array $options
940 private function lookupOptionsKeys( $options ) {
942 foreach ( $options as $key => $value ) {
943 $key = $this->msg( $key )->plain();
944 $ret[$key] = is_array( $value )
945 ?
$this->lookupOptionsKeys( $value )
952 * Recursively forces values in an array to strings, because issues arise
953 * with integer 0 as a value.
955 * @param array $array
958 static function forceToStringRecursive( $array ) {
959 if ( is_array( $array ) ) {
960 return array_map( [ __CLASS__
, 'forceToStringRecursive' ], $array );
962 return strval( $array );
967 * Fetch the array of options from the field's parameters. In order, this
968 * checks 'options-messages', 'options', then 'options-message'.
970 * @return array|null Options array
972 public function getOptions() {
973 if ( $this->mOptions
=== false ) {
974 if ( array_key_exists( 'options-messages', $this->mParams
) ) {
975 $this->mOptions
= $this->lookupOptionsKeys( $this->mParams
['options-messages'] );
976 } elseif ( array_key_exists( 'options', $this->mParams
) ) {
977 $this->mOptionsLabelsNotFromMessage
= true;
978 $this->mOptions
= self
::forceToStringRecursive( $this->mParams
['options'] );
979 } elseif ( array_key_exists( 'options-message', $this->mParams
) ) {
980 /** @todo This is copied from Xml::listDropDown(), deprecate/avoid duplication? */
981 $message = $this->getMessage( $this->mParams
['options-message'] )->inContentLanguage()->plain();
984 $this->mOptions
= [];
985 foreach ( explode( "\n", $message ) as $option ) {
986 $value = trim( $option );
987 if ( $value == '' ) {
989 } elseif ( substr( $value, 0, 1 ) == '*' && substr( $value, 1, 1 ) != '*' ) {
990 # A new group is starting...
991 $value = trim( substr( $value, 1 ) );
993 } elseif ( substr( $value, 0, 2 ) == '**' ) {
995 $opt = trim( substr( $value, 2 ) );
996 if ( $optgroup === false ) {
997 $this->mOptions
[$opt] = $opt;
999 $this->mOptions
[$optgroup][$opt] = $opt;
1002 # groupless reason list
1004 $this->mOptions
[$option] = $option;
1008 $this->mOptions
= null;
1012 return $this->mOptions
;
1016 * Get options and make them into arrays suitable for OOUI.
1017 * @return array Options for inclusion in a select or whatever.
1019 public function getOptionsOOUI() {
1020 $oldoptions = $this->getOptions();
1022 if ( $oldoptions === null ) {
1028 foreach ( $oldoptions as $text => $data ) {
1030 'data' => (string)$data,
1031 'label' => (string)$text,
1039 * flatten an array of options to a single array, for instance,
1040 * a set of "<options>" inside "<optgroups>".
1042 * @param array $options Associative Array with values either Strings or Arrays
1043 * @return array Flattened input
1045 public static function flattenOptions( $options ) {
1048 foreach ( $options as $value ) {
1049 if ( is_array( $value ) ) {
1050 $flatOpts = array_merge( $flatOpts, self
::flattenOptions( $value ) );
1052 $flatOpts[] = $value;
1060 * Formats one or more errors as accepted by field validation-callback.
1062 * @param string|Message|array $errors Array of strings or Message instances
1063 * @return string HTML
1066 protected static function formatErrors( $errors ) {
1067 if ( is_array( $errors ) && count( $errors ) === 1 ) {
1068 $errors = array_shift( $errors );
1071 if ( is_array( $errors ) ) {
1073 foreach ( $errors as $error ) {
1074 if ( $error instanceof Message
) {
1075 $lines[] = Html
::rawElement( 'li', [], $error->parse() );
1077 $lines[] = Html
::rawElement( 'li', [], $error );
1081 return Html
::rawElement( 'ul', [ 'class' => 'error' ], implode( "\n", $lines ) );
1083 if ( $errors instanceof Message
) {
1084 $errors = $errors->parse();
1087 return Html
::rawElement( 'span', [ 'class' => 'error' ], $errors );
1092 * Turns a *-message parameter (which could be a MessageSpecifier, or a message name, or a
1093 * name + parameters array) into a Message.
1094 * @param mixed $value
1097 protected function getMessage( $value ) {
1098 $message = Message
::newFromSpecifier( $value );
1100 if ( $this->mParent
) {
1101 $message->setContext( $this->mParent
);
1108 * Skip this field when collecting data.
1109 * @param WebRequest $request
1113 public function skipLoadData( $request ) {
1114 return !empty( $this->mParams
['nodata'] );