Allow maxlength attribute on HTMLSelectAndOtherField
[lhc/web/wiklou.git] / includes / htmlform / HTMLSelectAndOtherField.php
1 <?php
2
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.
12 */
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();
20 }
21
22 parent::__construct( $params );
23
24 if ( $this->getOptions() === null ) {
25 # Sulk
26 throw new MWException( 'HTMLSelectAndOtherField called without any options' );
27 }
28 if ( !in_array( 'other', $this->mOptions, true ) ) {
29 // Have 'other' always as first element
30 $this->mOptions = array( $params['other'] => 'other' ) + $this->mOptions;
31 }
32 $this->mFlatOptions = self::flattenOptions( $this->getOptions() );
33
34 }
35
36 function getInputHTML( $value ) {
37 $select = parent::getInputHTML( $value[1] );
38
39 $textAttribs = array(
40 'id' => $this->mID . '-other',
41 'size' => $this->getSize(),
42 'class' => array( 'mw-htmlform-select-and-other-field' ),
43 'data-id-select' => $this->mID,
44 );
45
46 if ( $this->mClass !== '' ) {
47 $textAttribs['class'][] = $this->mClass;
48 }
49
50 $allowedParams = array(
51 'required',
52 'autofocus',
53 'multiple',
54 'disabled',
55 'tabindex',
56 'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
57 );
58
59 $textAttribs += $this->getAttributes( $allowedParams );
60
61 $textbox = Html::input( $this->mName . '-other', $value[2], 'text', $textAttribs );
62
63 return "$select<br />\n$textbox";
64 }
65
66 /**
67 * @param WebRequest $request
68 *
69 * @return array("<overall message>","<select value>","<text field value>")
70 */
71 function loadDataFromRequest( $request ) {
72 if ( $request->getCheck( $this->mName ) ) {
73
74 $list = $request->getText( $this->mName );
75 $text = $request->getText( $this->mName . '-other' );
76
77 // Should be built the same as in mediawiki.htmlform.js
78 if ( $list == 'other' ) {
79 $final = $text;
80 } elseif ( !in_array( $list, $this->mFlatOptions, true ) ) {
81 # User has spoofed the select form to give an option which wasn't
82 # in the original offer. Sulk...
83 $final = $text;
84 } elseif ( $text == '' ) {
85 $final = $list;
86 } else {
87 $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
88 }
89 } else {
90 $final = $this->getDefault();
91
92 $list = 'other';
93 $text = $final;
94 foreach ( $this->mFlatOptions as $option ) {
95 $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
96 if ( strpos( $text, $match ) === 0 ) {
97 $list = $option;
98 $text = substr( $text, strlen( $match ) );
99 break;
100 }
101 }
102 }
103
104 return array( $final, $list, $text );
105 }
106
107 function getSize() {
108 return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
109 }
110
111 function validate( $value, $alldata ) {
112 # HTMLSelectField forces $value to be one of the options in the select
113 # field, which is not useful here. But we do want the validation further up
114 # the chain
115 $p = parent::validate( $value[1], $alldata );
116
117 if ( $p !== true ) {
118 return $p;
119 }
120
121 if ( isset( $this->mParams['required'] )
122 && $this->mParams['required'] !== false
123 && $value[1] === ''
124 ) {
125 return $this->msg( 'htmlform-required' )->parse();
126 }
127
128 return true;
129 }
130 }