5use InvalidArgumentException;
23 private const FIELD_CLASS =
'mw-htmlform-select-and-other-field';
25 private $mFlatOptions;
32 parent::__construct( $params );
35 throw new InvalidArgumentException(
'HTMLSelectAndOtherField called without any options' );
37 if ( !in_array(
'other', $this->mOptions,
true ) ) {
38 if ( array_key_exists(
'other', $params ) ) {
39 $msg = $params[
'other'];
40 } elseif ( array_key_exists(
'other-message', $params ) ) {
41 $msg = $this->
getMessage( $params[
'other-message'] )->text();
43 $msg = $this->
msg(
'htmlform-selectorother-other' )->text();
53 $select = parent::getInputHTML( $value[1] );
59 if ( isset( $this->mParams[
'maxlength-unit'] ) ) {
60 $textAttribs[
'data-mw-maxlength-unit'] = $this->mParams[
'maxlength-unit'];
75 $textbox = Html::input( $this->mName .
'-other', $value[2],
'text', $textAttribs );
79 'class' => self::FIELD_CLASS
81 if ( $this->mClass !==
'' ) {
84 return Html::rawElement(
87 "$select<br />\n$textbox"
93 return [
'mediawiki.widgets.SelectWithInputWidget',
'mediawiki.widgets.visibleLengthLimit' ];
98 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.SelectWithInputWidget.styles' );
102 'name' => $this->mName .
'-other',
103 'value' => $value[2],
115 $textAttribs += \OOUI\Element::configFromHtmlAttributes(
120 $dropdownInputAttribs = [
123 'value' => $value[1],
131 $dropdownInputAttribs += \OOUI\Element::configFromHtmlAttributes(
136 if ( isset( $this->mParams[
'disabled' ] ) && $this->mParams[
'disabled' ] ) {
140 $inputClasses = [ self::FIELD_CLASS ];
141 if ( $this->mClass !==
'' ) {
142 $inputClasses = array_merge( $inputClasses, explode(
' ', $this->mClass ) );
146 'disabled' => $disabled,
147 'textinput' => $textAttribs,
148 'dropdowninput' => $dropdownInputAttribs,
150 'required' => $this->mParams[
'required' ] ??
false,
151 'classes' => $inputClasses,
153 'maxlengthUnit' => $this->mParams[
'maxlength-unit'] ??
'bytes'
169 $select = parent::getInputCodex( $value[1], $hasErrors );
172 $textInputAttribs = [
174 'name' => $this->mName .
'-other'
177 if ( isset( $this->mParams[
'maxlength-unit'] ) ) {
178 $textInputAttribs[
'data-mw-maxlength-unit'] = $this->mParams[
'maxlength-unit'];
198 $this->mName .
'-other',
205 'class' => [ self::FIELD_CLASS ]
207 if ( $this->mClass !==
'' ) {
210 return Html::rawElement(
213 "$select<br />\n$textInput"
221 $default = parent::getDefault();
228 if ( $default !==
null ) {
233 foreach ( $this->mFlatOptions as $option ) {
234 $match = $option . $this->
msg(
'colon-separator' )->inContentLanguage()->text();
235 if ( str_starts_with( $final, $match ) ) {
237 $text = substr( $final, strlen( $match ) );
243 return [ $final, $list, $text ];
252 if ( $request->getCheck( $this->mName ) ) {
253 $list = $request->getText( $this->mName );
254 $text = $request->getText( $this->mName .
'-other' );
257 if ( $list ==
'other' ) {
259 } elseif ( !in_array( $list, $this->mFlatOptions,
true ) ) {
260 # User has spoofed the select form to give an option which wasn't
261 # in the original offer. Sulk...
263 } elseif ( $text ==
'' ) {
266 $final = $list . $this->
msg(
'colon-separator' )->inContentLanguage()->text() . $text;
268 return [ $final, $list, $text ];
278 return $this->mParams[
'size'] ?? 45;
283 # HTMLSelectField forces $value to be one of the options in the select
284 # field, which is not useful here. But we do want the validation further up
286 $p = parent::validate( $value[1], $alldata );
292 if ( isset( $this->mParams[
'required'] )
293 && $this->mParams[
'required'] !==
false
296 return $this->
msg(
'htmlform-required' );
304class_alias( HTMLSelectAndOtherField::class,
'HTMLSelectAndOtherField' );
static buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs)
Build the markup of the Codex component.