Special:BlockList: Use mw-ui-progressive for search button
[mediawiki.git] / includes / htmlform / HTMLSelectAndOtherField.php
blob65176dd7ac4b525f289d3ae3bc465c1ecc172b51
1 <?php
3 /**
4 * Double field with a dropdown list constructed from a system message in the format
5 * * Optgroup header
6 * ** <option value>
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
10 * select dropdown.
11 * @todo FIXME: If made 'required', only the text field should be compulsory.
13 class HTMLSelectAndOtherField extends HTMLSelectField {
14 function __construct( $params ) {
15 if ( array_key_exists( 'other', $params ) ) {
16 } elseif ( array_key_exists( 'other-message', $params ) ) {
17 $params['other'] = wfMessage( $params['other-message'] )->plain();
18 } else {
19 $params['other'] = wfMessage( 'htmlform-selectorother-other' )->plain();
22 parent::__construct( $params );
24 if ( $this->getOptions() === null ) {
25 # Sulk
26 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
28 if ( !in_array( 'other', $this->mOptions, true ) ) {
29 // Have 'other' always as first element
30 $this->mOptions = array( $params['other'] => 'other' ) + $this->mOptions;
32 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
36 function getInputHTML( $value ) {
37 $select = parent::getInputHTML( $value[1] );
39 $textAttribs = array(
40 'id' => $this->mID . '-other',
41 'size' => $this->getSize(),
44 if ( $this->mClass !== '' ) {
45 $textAttribs['class'] = $this->mClass;
48 $allowedParams = array(
49 'required',
50 'autofocus',
51 'multiple',
52 'disabled',
53 'tabindex'
56 $textAttribs += $this->getAttributes( $allowedParams );
58 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
60 return "$select<br />\n$textbox";
63 /**
64 * @param WebRequest $request
66 * @return array("<overall message>","<select value>","<text field value>")
68 function loadDataFromRequest( $request ) {
69 if ( $request->getCheck( $this->mName ) ) {
71 $list = $request->getText( $this->mName );
72 $text = $request->getText( $this->mName . '-other' );
74 if ( $list == 'other' ) {
75 $final = $text;
76 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
77 # User has spoofed the select form to give an option which wasn't
78 # in the original offer. Sulk...
79 $final = $text;
80 } elseif ( $text == '' ) {
81 $final = $list;
82 } else {
83 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
85 } else {
86 $final = $this->getDefault();
88 $list = 'other';
89 $text = $final;
90 foreach ( $this->mFlatOptions as $option ) {
91 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
92 if ( strpos( $text, $match ) === 0 ) {
93 $list = $option;
94 $text = substr( $text, strlen( $match ) );
95 break;
100 return array( $final, $list, $text );
103 function getSize() {
104 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
107 function validate( $value, $alldata ) {
108 # HTMLSelectField forces $value to be one of the options in the select
109 # field, which is not useful here. But we do want the validation further up
110 # the chain
111 $p = parent::validate( $value[1], $alldata );
113 if ( $p !== true ) {
114 return $p;
117 if ( isset( $this->mParams['required'] )
118 && $this->mParams['required'] !== false
119 && $value[1] === ''
121 return $this->msg( 'htmlform-required' )->parse();
124 return true;