5use InvalidArgumentException;
23 private const FIELD_CLASS =
'mw-htmlform-select-and-other-field';
25 private $mFlatOptions;
32 if ( array_key_exists(
'other', $params ) ) {
34 } elseif ( array_key_exists(
'other-message', $params ) ) {
35 $params[
'other'] = $this->
getMessage( $params[
'other-message'] )->plain();
37 $params[
'other'] = $this->
msg(
'htmlform-selectorother-other' )->plain();
40 parent::__construct( $params );
43 throw new InvalidArgumentException(
'HTMLSelectAndOtherField called without any options' );
45 if ( !in_array(
'other', $this->mOptions,
true ) ) {
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"
92 return [
'mediawiki.widgets.SelectWithInputWidget' ];
96 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.SelectWithInputWidget.styles' );
100 'name' => $this->mName .
'-other',
101 'value' => $value[2],
113 $textAttribs += \OOUI\Element::configFromHtmlAttributes(
118 $dropdownInputAttribs = [
121 'value' => $value[1],
129 $dropdownInputAttribs += \OOUI\Element::configFromHtmlAttributes(
134 if ( isset( $this->mParams[
'disabled' ] ) && $this->mParams[
'disabled' ] ) {
138 $inputClasses = [ self::FIELD_CLASS ];
139 if ( $this->mClass !==
'' ) {
140 $inputClasses = array_merge( $inputClasses, explode(
' ', $this->mClass ) );
144 'disabled' => $disabled,
145 'textinput' => $textAttribs,
146 'dropdowninput' => $dropdownInputAttribs,
148 'required' => $this->mParams[
'required' ] ??
false,
149 'classes' => $inputClasses,
151 'maxlengthUnit' => $this->mParams[
'maxlength-unit'] ??
'bytes'
166 $select = parent::getInputCodex( $value[1], $hasErrors );
169 $textInputAttribs = [
171 'name' => $this->mName .
'-other'
174 if ( isset( $this->mParams[
'maxlength-unit'] ) ) {
175 $textInputAttribs[
'data-mw-maxlength-unit'] = $this->mParams[
'maxlength-unit'];
195 $this->mName .
'-other',
202 'class' => [ self::FIELD_CLASS ]
204 if ( $this->mClass !==
'' ) {
207 return Html::rawElement(
210 "$select<br />\n$textInput"
218 $default = parent::getDefault();
225 if ( $default !==
null ) {
230 foreach ( $this->mFlatOptions as $option ) {
231 $match = $option . $this->
msg(
'colon-separator' )->inContentLanguage()->text();
232 if ( str_starts_with( $final, $match ) ) {
234 $text = substr( $final, strlen( $match ) );
240 return [ $final, $list, $text ];
249 if ( $request->getCheck( $this->mName ) ) {
250 $list = $request->getText( $this->mName );
251 $text = $request->getText( $this->mName .
'-other' );
254 if ( $list ==
'other' ) {
256 } elseif ( !in_array( $list, $this->mFlatOptions,
true ) ) {
257 # User has spoofed the select form to give an option which wasn't
258 # in the original offer. Sulk...
260 } elseif ( $text ==
'' ) {
263 $final = $list . $this->
msg(
'colon-separator' )->inContentLanguage()->text() . $text;
265 return [ $final, $list, $text ];
271 return $this->mParams[
'size'] ?? 45;
275 # HTMLSelectField forces $value to be one of the options in the select
276 # field, which is not useful here. But we do want the validation further up
278 $p = parent::validate( $value[1], $alldata );
284 if ( isset( $this->mParams[
'required'] )
285 && $this->mParams[
'required'] !==
false
288 return $this->
msg(
'htmlform-required' );
296class_alias( HTMLSelectAndOtherField::class,
'HTMLSelectAndOtherField' );
static buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs)
Build the markup of the Codex component.