12 private $mPlaceholder;
27 parent::__construct( $params );
30 if ( !isset( $this->mParams[
'disabled-options'] ) ) {
31 $this->mParams[
'disabled-options'] = [];
34 if ( isset( $params[
'dropdown'] ) ) {
35 $this->mClass .=
' mw-htmlform-dropdown';
36 if ( isset( $params[
'placeholder'] ) ) {
37 $this->mPlaceholder = $params[
'placeholder'];
38 } elseif ( isset( $params[
'placeholder-message'] ) ) {
39 $this->mPlaceholder = $this->
msg( $params[
'placeholder-message'] )->text();
43 if ( isset( $params[
'flatlist'] ) ) {
44 $this->mClass .=
' mw-htmlform-flatlist';
53 $p = parent::validate( $value, $alldata );
59 if ( !is_array( $value ) ) {
64 $value = array_filter( $value,
'is_scalar' );
66 # If all options are valid, array_intersect of the valid options
67 # and the provided options will return the provided options.
70 $validValues = array_intersect( $value, $validOptions );
71 if ( count( $validValues ) == count( $value ) ) {
74 return $this->
msg(
'htmlform-select-badoption' );
83 if ( isset( $this->mParams[
'dropdown'] ) ) {
84 $this->mParent->getOutput()->addModules(
'jquery.chosen' );
105 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
107 foreach ( $options as $label => $info ) {
108 if ( is_array( $info ) ) {
109 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
113 'id' =>
"{$this->mID}-$info",
116 if ( in_array( $info, $this->mParams[
'disabled-options'],
true ) ) {
117 $thisAttribs[
'disabled'] =
'disabled';
119 $checked = in_array( $info, $value,
true );
121 $checkbox = $this->
getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
123 $html .=
' ' . Html::rawElement(
125 [
'class' =>
'mw-htmlform-flatlist-item' ],
136 throw new MWException(
'HTMLMultiSelectField#getOneCheckbox() is not supported' );
138 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ?
'rawElement' :
'element' ];
140 Xml::check(
"{$this->mName}[]", $checked, $attribs ) .
142 call_user_func( $elementFunc,
144 [
'for' => $attribs[
'id'] ],
147 if ( $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere ) ) {
148 $checkbox = Html::openElement(
'div', [
'class' =>
'mw-ui-checkbox' ] ) .
150 Html::closeElement(
'div' );
164 throw new MWException(
'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
180 $this->mParent->getOutput()->addModules(
'oojs-ui-widgets' );
183 $value = array_filter( $value,
'is_scalar' );
185 $hasSections =
false;
186 $optionsOouiSections = [];
190 foreach ( $options as $label => $section ) {
191 if ( is_array( $section ) ) {
192 $optionsOouiSections[ $label ] = Xml::listDropDownOptionsOoui( $section );
193 unset( $options[$label] );
200 $optionsOouiSections = array_merge(
201 [
'' => Xml::listDropDownOptionsOoui( $options ) ],
205 '@phan-var array[][] $optionsOouiSections';
208 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
210 $attr[
'name'] =
"{$this->mName}[]";
212 $attr[
'value'] = $value;
214 $options = $optionsOoui;
215 foreach ( $options as &$option ) {
216 $option[
'disabled'] = in_array( $option[
'data'], $this->mParams[
'disabled-options'],
true );
218 if ( $this->mOptionsLabelsNotFromMessage ) {
219 foreach ( $options as &$option ) {
221 $option[
'label'] =
new OOUI\HtmlSnippet( $option[
'label'] );
225 $attr[
'options'] = $options;
227 $attr += OOUI\Element::configFromHtmlAttributes(
231 if ( $this->mClass !==
'' ) {
232 $attr[
'classes'] = [ $this->mClass ];
235 $widget =
new OOUI\CheckboxMultiselectInputWidget( $attr );
236 if ( $sectionLabel ) {
237 $out[] =
new OOUI\FieldsetLayout( [
238 'items' => [ $widget ],
240 'label' =>
new OOUI\HtmlSnippet( $sectionLabel ),
247 if ( !$hasSections && $out ) {
248 if ( $this->mPlaceholder ) {
249 $out[0]->setData( ( $out[0]->getData() ?: [] ) + [
250 'placeholder' => $this->mPlaceholder,
258 return implode(
'', $out );
268 $fromRequest = $request->getArray( $this->mName, [] );
288 return $this->mDefault ?? [];
298 $forcedOn = array_intersect( $this->mParams[
'disabled-options'], $this->
getDefault() );
301 foreach ( $options as $opt ) {
302 $res[
"$opt"] = in_array( $opt, $forcedOn,
true ) || in_array( $opt, $data,
true );
validate( $value, $alldata)
Override this function to add specific validation checks on the field input.Don't forget to call pare...
needsLabel()
Should this field have a label, or is there no input element with the appropriate id for the label to...
getOneCheckbox( $checked, $attribs, $label)
filterDataForSubmit( $data)
Support for separating multi-option preferences into multiple preferences Due to lack of array suppor...
getInputHTML( $value)
This function must be implemented to return the HTML to generate the input object itself....
getInputOOUI( $value)
Get the OOUI version of this field.
formatOptions( $options, $value)
getDefault()
to override mixed
getOptionsOOUI()
Get options and make them into arrays suitable for OOUI.
loadDataFromRequest( $request)
A class containing constants representing the names of configuration variables.