4 * Double field with a dropdown list constructed from a system message in the format
7 * * New Optgroup header
8 * Plus a text field underneath for an additional reason. The 'value' of the field is
9 * "<select>: <extra reason>", or "<extra reason>" if nothing has been selected in the
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
13 class HTMLSelectAndOtherField
extends HTMLSelectField
{
15 private $mFlatOptions;
17 public function __construct( $params ) {
18 if ( array_key_exists( 'other', $params ) ) {
20 } elseif ( array_key_exists( 'other-message', $params ) ) {
21 $params['other'] = $this->getMessage( $params['other-message'] )->plain();
23 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
26 parent
::__construct( $params );
28 if ( $this->getOptions() === null ) {
30 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
32 if ( !in_array( 'other', $this->mOptions
, true ) ) {
33 // Have 'other' always as first element
34 $this->mOptions
= [ $params['other'] => 'other' ] +
$this->mOptions
;
36 $this->mFlatOptions
= self
::flattenOptions( $this->getOptions() );
39 public function getInputHTML( $value ) {
40 $select = parent
::getInputHTML( $value[1] );
43 'id' => $this->mID
. '-other',
44 'size' => $this->getSize(),
45 'class' => [ 'mw-htmlform-select-and-other-field' ],
46 'data-id-select' => $this->mID
,
49 if ( $this->mClass
!== '' ) {
50 $textAttribs['class'][] = $this->mClass
;
53 if ( isset( $this->mParams
['maxlength-unit'] ) ) {
54 $textAttribs['data-mw-maxlength-unit'] = $this->mParams
['maxlength-unit'];
63 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
64 'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
67 $textAttribs +
= $this->getAttributes( $allowedParams );
69 $textbox = Html
::input( $this->mName
. '-other', $value[2], 'text', $textAttribs );
71 return "$select<br />\n$textbox";
74 protected function getOOUIModules() {
75 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
78 public function getInputOOUI( $value ) {
79 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
83 'name' => $this->mName
. '-other',
96 $textAttribs +
= OOUI\Element
::configFromHtmlAttributes(
97 $this->getAttributes( $allowedParams )
100 if ( $this->mClass
!== '' ) {
101 $textAttribs['classes'] = [ $this->mClass
];
105 $dropdownInputAttribs = [
106 'name' => $this->mName
,
107 'id' => $this->mID
. '-select',
108 'options' => $this->getOptionsOOUI(),
109 'value' => $value[1],
117 $dropdownInputAttribs +
= OOUI\Element
::configFromHtmlAttributes(
118 $this->getAttributes( $allowedParams )
121 if ( $this->mClass
!== '' ) {
122 $dropdownInputAttribs['classes'] = [ $this->mClass
];
126 if ( isset( $this->mParams
[ 'disabled' ] ) && $this->mParams
[ 'disabled' ] ) {
130 return $this->getInputWidget( [
132 'disabled' => $disabled,
133 'textinput' => $textAttribs,
134 'dropdowninput' => $dropdownInputAttribs,
136 'required' => $this->mParams
[ 'required' ] ??
false,
137 'classes' => [ 'mw-htmlform-select-and-other-field' ],
139 'maxlengthUnit' => $this->mParams
['maxlength-unit'] ??
'bytes'
144 public function getInputWidget( $params ) {
145 return new MediaWiki\Widget\
SelectWithInputWidget( $params );
151 public function getDefault() {
152 $default = parent
::getDefault();
154 // Default values of empty form
159 if ( $default !== null ) {
161 // Assume the default is a text value, with the 'other' option selected.
162 // Then check if that assumption is correct, and update $list and $text if not.
164 foreach ( $this->mFlatOptions
as $option ) {
165 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
166 if ( strpos( $final, $match ) === 0 ) {
168 $text = substr( $final, strlen( $match ) );
174 return [ $final, $list, $text ];
178 * @param WebRequest $request
180 * @return array ["<overall message>","<select value>","<text field value>"]
182 public function loadDataFromRequest( $request ) {
183 if ( $request->getCheck( $this->mName
) ) {
184 $list = $request->getText( $this->mName
);
185 $text = $request->getText( $this->mName
. '-other' );
187 // Should be built the same as in mediawiki.htmlform.js
188 if ( $list == 'other' ) {
190 } elseif ( !in_array( $list, $this->mFlatOptions
, true ) ) {
191 # User has spoofed the select form to give an option which wasn't
192 # in the original offer. Sulk...
194 } elseif ( $text == '' ) {
197 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
199 return [ $final, $list, $text ];
201 return $this->getDefault();
204 public function getSize() {
205 return $this->mParams
['size'] ??
45;
208 public function validate( $value, $alldata ) {
209 # HTMLSelectField forces $value to be one of the options in the select
210 # field, which is not useful here. But we do want the validation further up
212 $p = parent
::validate( $value[1], $alldata );
218 if ( isset( $this->mParams
['required'] )
219 && $this->mParams
['required'] !== false
222 return $this->msg( 'htmlform-required' );