MediaWiki master
HTMLAutoCompleteSelectField.php
Go to the documentation of this file.
1<?php
2
4
5use InvalidArgumentException;
9
40 protected $autocompleteData = [];
41
46 public function __construct( $params ) {
47 $params += [
48 'require-match' => false,
49 ];
50
51 parent::__construct( $params );
52
53 if ( array_key_exists( 'autocomplete-data-messages', $this->mParams ) ) {
54 foreach ( $this->mParams['autocomplete-data-messages'] as $key => $value ) {
55 $key = $this->msg( $key )->plain();
56 $this->autocompleteData[$key] = strval( $value );
57 }
58 } elseif ( array_key_exists( 'autocomplete-data', $this->mParams ) ) {
59 foreach ( $this->mParams['autocomplete-data'] as $key => $value ) {
60 $this->autocompleteData[$key] = strval( $value );
61 }
62 }
63 if ( !is_array( $this->autocompleteData ) || !$this->autocompleteData ) {
64 throw new InvalidArgumentException( 'HTMLAutoCompleteSelectField called without any autocompletions' );
65 }
66
67 $this->getOptions();
68 if ( $this->mOptions && !in_array( 'other', $this->mOptions, true ) ) {
69 if ( isset( $params['other-message'] ) ) {
70 $msg = $this->getMessage( $params['other-message'] )->text();
71 } elseif ( isset( $params['other'] ) ) {
72 $msg = $params['other'];
73 } else {
74 $msg = wfMessage( 'htmlform-selectorother-other' )->text();
75 }
76 $this->mOptions[$msg] = 'other';
77 }
78 }
79
80 public function loadDataFromRequest( $request ) {
81 if ( $request->getCheck( $this->mName ) ) {
82 $val = $request->getText( $this->mName . '-select', 'other' );
83
84 if ( $val === 'other' ) {
85 $val = $request->getText( $this->mName );
86 if ( isset( $this->autocompleteData[$val] ) ) {
87 $val = $this->autocompleteData[$val];
88 }
89 }
90
91 return $val;
92 } else {
93 return $this->getDefault();
94 }
95 }
96
97 public function validate( $value, $alldata ) {
98 $p = parent::validate( $value, $alldata );
99
100 if ( $p !== true ) {
101 return $p;
102 }
103
104 $validOptions = HTMLFormField::flattenOptions( $this->getOptions() ?: [] );
105
106 if (
107 in_array( strval( $value ), $validOptions, true ) ||
108 in_array( strval( $value ), $this->autocompleteData, true )
109 ) {
110 return true;
111 } elseif ( $this->mParams['require-match'] ) {
112 return $this->msg( 'htmlform-select-badoption' );
113 }
114
115 return true;
116 }
117
118 // FIXME Ewww, this shouldn't be adding any attributes not requested in $list :(
119 public function getAttributes( array $list ) {
120 $attribs = [
121 'type' => 'text',
122 'data-autocomplete' => FormatJson::encode( array_keys( $this->autocompleteData ) ),
123 ] + parent::getAttributes( $list );
124
125 if ( $this->getOptions() ) {
126 $attribs['data-cond-state'] = FormatJson::encode( [
127 'hide' => [ '!==', $this->mName . '-select', 'other' ],
128 ] );
129 }
130
131 return $attribs;
132 }
133
134 public function getInputHTML( $value ) {
135 $oldClass = $this->mClass;
136 $classes = (array)$this->mClass;
137
138 $valInSelect = false;
139 $ret = '';
140
141 if ( $this->getOptions() ) {
142 if ( $value !== false ) {
143 $value = strval( $value );
144 $valInSelect = in_array(
145 $value, HTMLFormField::flattenOptions( $this->getOptions() ), true
146 );
147 }
148
149 $selected = $valInSelect ? $value : 'other';
150 $select = new XmlSelect( $this->mName . '-select', $this->mID . '-select', $selected );
151 $select->addOptions( $this->getOptions() );
152
153 if ( !empty( $this->mParams['disabled'] ) ) {
154 $select->setAttribute( 'disabled', 'disabled' );
155 }
156
157 if ( isset( $this->mParams['tabindex'] ) ) {
158 $select->setAttribute( 'tabindex', $this->mParams['tabindex'] );
159 }
160
161 $ret = $select->getHTML() . "<br />\n";
162
163 $classes[] = 'mw-htmlform-hide-if';
164 }
165
166 if ( $valInSelect ) {
167 $value = '';
168 } else {
169 $key = array_search( strval( $value ), $this->autocompleteData, true );
170 if ( $key !== false ) {
171 $value = $key;
172 }
173 }
174
175 $classes[] = 'mw-htmlform-autocomplete';
176 $this->mClass = implode( ' ', $classes );
177 $ret .= parent::getInputHTML( $valInSelect ? '' : $value );
178 $this->mClass = $oldClass;
179
180 return $ret;
181 }
182
188 public function getInputOOUI( $value ) {
189 // To be implemented, for now override the function from HTMLTextField
190 return false;
191 }
192}
193
195class_alias( HTMLAutoCompleteSelectField::class, 'HTMLAutoCompleteSelectField' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
array $params
The job parameters.
Text field for selecting a value from a large list of possible values, with auto-completion and optio...
loadDataFromRequest( $request)
Get the value that this input has been set to from a posted form, or the input's default value if it ...
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.
getAttributes(array $list)
Returns the given attributes from the parameters.
getInputOOUI( $value)
Get the OOUI version of this input.
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
The parent class to generate form fields.
getMessage( $value)
Turns a *-message parameter (which could be a MessageSpecifier, or a message name,...
static flattenOptions( $options)
flatten an array of options to a single array, for instance, a set of "<options>" inside "<optgroups>...
getOptions()
Fetch the array of options from the field's parameters.
msg( $key,... $params)
Get a translated interface message.
JSON formatter wrapper class.
Class for generating HTML <select> or <datalist> elements.
Definition XmlSelect.php:30