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 ) ) {
54 $select = parent::getInputHTML( $value[1] );
60 if ( isset( $this->mParams[
'maxlength-unit'] ) ) {
61 $textAttribs[
'data-mw-maxlength-unit'] = $this->mParams[
'maxlength-unit'];
76 $textbox = Html::input( $this->mName .
'-other', $value[2],
'text', $textAttribs );
80 'class' => self::FIELD_CLASS
82 if ( $this->mClass !==
'' ) {
85 return Html::rawElement(
88 "$select<br />\n$textbox"
94 return [
'mediawiki.widgets.SelectWithInputWidget',
'mediawiki.widgets.visibleLengthLimit' ];
99 $this->mParent->getOutput()->addModuleStyles(
'mediawiki.widgets.SelectWithInputWidget.styles' );
103 'name' => $this->mName .
'-other',
104 'value' => $value[2],
116 $textAttribs += \OOUI\Element::configFromHtmlAttributes(
121 $dropdownInputAttribs = [
124 'value' => $value[1],
132 $dropdownInputAttribs += \OOUI\Element::configFromHtmlAttributes(
137 if ( isset( $this->mParams[
'disabled' ] ) && $this->mParams[
'disabled' ] ) {
141 $inputClasses = [ self::FIELD_CLASS ];
142 if ( $this->mClass !==
'' ) {
143 $inputClasses = array_merge( $inputClasses, explode(
' ', $this->mClass ) );
147 'disabled' => $disabled,
148 'textinput' => $textAttribs,
149 'dropdowninput' => $dropdownInputAttribs,
151 'required' => $this->mParams[
'required' ] ??
false,
152 'classes' => $inputClasses,
154 'maxlengthUnit' => $this->mParams[
'maxlength-unit'] ??
'bytes'
170 $select = parent::getInputCodex( $value[1], $hasErrors );
173 $textInputAttribs = [
175 'name' => $this->mName .
'-other'
178 if ( isset( $this->mParams[
'maxlength-unit'] ) ) {
179 $textInputAttribs[
'data-mw-maxlength-unit'] = $this->mParams[
'maxlength-unit'];
199 $this->mName .
'-other',
206 'class' => [ self::FIELD_CLASS ]
208 if ( $this->mClass !==
'' ) {
211 return Html::rawElement(
214 "$select<br />\n$textInput"
222 $default = parent::getDefault();
229 if ( $default !==
null ) {
234 foreach ( $this->mFlatOptions as $option ) {
235 $match = $option . $this->
msg(
'colon-separator' )->inContentLanguage()->text();
236 if ( str_starts_with( $final, $match ) ) {
238 $text = substr( $final, strlen( $match ) );
244 return [ $final, $list, $text ];
253 if ( $request->getCheck( $this->mName ) ) {
254 $list = $request->getText( $this->mName );
255 $text = $request->getText( $this->mName .
'-other' );
258 if ( $list ==
'other' ) {
260 } elseif ( !in_array( $list, $this->mFlatOptions,
true ) ) {
261 # User has spoofed the select form to give an option which wasn't
262 # in the original offer. Sulk...
264 } elseif ( $text ==
'' ) {
267 $final = $list . $this->
msg(
'colon-separator' )->inContentLanguage()->text() . $text;
269 return [ $final, $list, $text ];
276 return $this->mParams[
'size'] ?? 45;
281 # HTMLSelectField forces $value to be one of the options in the select
282 # field, which is not useful here. But we do want the validation further up
284 $p = parent::validate( $value[1], $alldata );
290 if ( isset( $this->mParams[
'required'] )
291 && $this->mParams[
'required'] !==
false
294 return $this->
msg(
'htmlform-required' );
302class_alias( HTMLSelectAndOtherField::class,
'HTMLSelectAndOtherField' );
static buildCodexComponent( $value, $hasErrors, $type, $name, $inputAttribs)
Build the markup of the Codex component.