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
{
14 public function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
17 } elseif ( array_key_exists( 'other-message', $params ) ) {
18 $params['other'] = $this->getMessage( $params['other-message'] )->plain();
20 $params['other'] = $this->msg( 'htmlform-selectorother-other' )->plain();
23 parent
::__construct( $params );
25 if ( $this->getOptions() === null ) {
27 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
29 if ( !in_array( 'other', $this->mOptions
, true ) ) {
30 // Have 'other' always as first element
31 $this->mOptions
= [ $params['other'] => 'other' ] +
$this->mOptions
;
33 $this->mFlatOptions
= self
::flattenOptions( $this->getOptions() );
36 public function getInputHTML( $value ) {
37 $select = parent
::getInputHTML( $value[1] );
40 'id' => $this->mID
. '-other',
41 'size' => $this->getSize(),
42 'class' => [ 'mw-htmlform-select-and-other-field' ],
43 'data-id-select' => $this->mID
,
46 if ( $this->mClass
!== '' ) {
47 $textAttribs['class'][] = $this->mClass
;
50 if ( isset( $this->mParams
['maxlength-unit'] ) ) {
51 $textAttribs['data-mw-maxlength-unit'] = $this->mParams
['maxlength-unit'];
60 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
61 'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
64 $textAttribs +
= $this->getAttributes( $allowedParams );
66 $textbox = Html
::input( $this->mName
. '-other', $value[2], 'text', $textAttribs );
68 return "$select<br />\n$textbox";
71 protected function getOOUIModules() {
72 return [ 'mediawiki.widgets.SelectWithInputWidget' ];
75 public function getInputOOUI( $value ) {
76 $this->mParent
->getOutput()->addModuleStyles( 'mediawiki.widgets.SelectWithInputWidget.styles' );
80 'name' => $this->mName
. '-other',
93 $textAttribs +
= OOUI\Element
::configFromHtmlAttributes(
94 $this->getAttributes( $allowedParams )
97 if ( $this->mClass
!== '' ) {
98 $textAttribs['classes'] = [ $this->mClass
];
102 $dropdownInputAttribs = [
103 'name' => $this->mName
,
104 'id' => $this->mID
. '-select',
105 'options' => $this->getOptionsOOUI(),
106 'value' => $value[1],
114 $dropdownInputAttribs +
= OOUI\Element
::configFromHtmlAttributes(
115 $this->getAttributes( $allowedParams )
118 if ( $this->mClass
!== '' ) {
119 $dropdownInputAttribs['classes'] = [ $this->mClass
];
123 if ( isset( $this->mParams
[ 'disabled' ] ) && $this->mParams
[ 'disabled' ] ) {
127 return $this->getInputWidget( [
129 'disabled' => $disabled,
130 'textinput' => $textAttribs,
131 'dropdowninput' => $dropdownInputAttribs,
133 'classes' => [ 'mw-htmlform-select-and-other-field' ],
135 'maxlengthUnit' => $this->mParams
['maxlength-unit'] ??
'bytes'
140 public function getInputWidget( $params ) {
141 return new MediaWiki\Widget\
SelectWithInputWidget( $params );
147 public function getDefault() {
148 $default = parent
::getDefault();
150 // Default values of empty form
155 if ( $default !== null ) {
157 // Assume the default is a text value, with the 'other' option selected.
158 // Then check if that assumption is correct, and update $list and $text if not.
160 foreach ( $this->mFlatOptions
as $option ) {
161 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
162 if ( strpos( $final, $match ) === 0 ) {
164 $text = substr( $final, strlen( $match ) );
170 return [ $final, $list, $text ];
174 * @param WebRequest $request
176 * @return array ["<overall message>","<select value>","<text field value>"]
178 public function loadDataFromRequest( $request ) {
179 if ( $request->getCheck( $this->mName
) ) {
180 $list = $request->getText( $this->mName
);
181 $text = $request->getText( $this->mName
. '-other' );
183 // Should be built the same as in mediawiki.htmlform.js
184 if ( $list == 'other' ) {
186 } elseif ( !in_array( $list, $this->mFlatOptions
, true ) ) {
187 # User has spoofed the select form to give an option which wasn't
188 # in the original offer. Sulk...
190 } elseif ( $text == '' ) {
193 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
195 return [ $final, $list, $text ];
197 return $this->getDefault();
200 public function getSize() {
201 return $this->mParams
['size'] ??
45;
204 public function validate( $value, $alldata ) {
205 # HTMLSelectField forces $value to be one of the options in the select
206 # field, which is not useful here. But we do want the validation further up
208 $p = parent
::validate( $value[1], $alldata );
214 if ( isset( $this->mParams
['required'] )
215 && $this->mParams
['required'] !== false
218 return $this->msg( 'htmlform-required' );