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
;
56 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
59 $textAttribs +
= $this->getAttributes( $allowedParams );
61 $textbox = Html
::input( $this->mName
. '-other', $value[2], 'text', $textAttribs );
63 return "$select<br />\n$textbox";
66 public function getInputOOUI( $value ) {
71 * @param WebRequest $request
73 * @return array("<overall message>","<select value>","<text field value>")
75 public function loadDataFromRequest( $request ) {
76 if ( $request->getCheck( $this->mName
) ) {
77 $list = $request->getText( $this->mName
);
78 $text = $request->getText( $this->mName
. '-other' );
80 // Should be built the same as in mediawiki.htmlform.js
81 if ( $list == 'other' ) {
83 } elseif ( !in_array( $list, $this->mFlatOptions
, true ) ) {
84 # User has spoofed the select form to give an option which wasn't
85 # in the original offer. Sulk...
87 } elseif ( $text == '' ) {
90 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
93 $final = $this->getDefault();
97 foreach ( $this->mFlatOptions
as $option ) {
98 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
99 if ( strpos( $text, $match ) === 0 ) {
101 $text = substr( $text, strlen( $match ) );
107 return [ $final, $list, $text ];
110 public function getSize() {
111 return isset( $this->mParams
['size'] ) ?
$this->mParams
['size'] : 45;
114 public function validate( $value, $alldata ) {
115 # HTMLSelectField forces $value to be one of the options in the select
116 # field, which is not useful here. But we do want the validation further up
118 $p = parent
::validate( $value[1], $alldata );
124 if ( isset( $this->mParams
['required'] )
125 && $this->mParams
['required'] !== false
128 return $this->msg( 'htmlform-required' );