13 private $mPlaceholder;
28 parent::__construct( $params );
31 if ( !isset( $this->mParams[
'disabled-options'] ) ) {
32 $this->mParams[
'disabled-options'] = [];
35 if ( isset( $params[
'dropdown'] ) ) {
36 $this->mClass .=
' mw-htmlform-dropdown';
37 if ( isset( $params[
'placeholder'] ) ) {
38 $this->mPlaceholder = $params[
'placeholder'];
39 } elseif ( isset( $params[
'placeholder-message'] ) ) {
40 $this->mPlaceholder = $this->
msg( $params[
'placeholder-message'] )->text();
44 if ( isset( $params[
'flatlist'] ) ) {
45 $this->mClass .=
' mw-htmlform-flatlist';
54 $p = parent::validate( $value, $alldata );
60 if ( !is_array( $value ) ) {
65 $value = array_filter( $value,
'is_scalar' );
67 # If all options are valid, array_intersect of the valid options
68 # and the provided options will return the provided options.
71 $validValues = array_intersect( $value, $validOptions );
72 if ( count( $validValues ) == count( $value ) ) {
75 return $this->
msg(
'htmlform-select-badoption' );
84 if ( isset( $this->mParams[
'dropdown'] ) ) {
85 $this->mParent->getOutput()->addModules(
'jquery.chosen' );
106 $attribs = $this->
getAttributes( [
'disabled',
'tabindex' ] );
108 foreach ( $options as $label => $info ) {
109 if ( is_array( $info ) ) {
110 $html .= Html::rawElement(
'h1', [], $label ) .
"\n";
114 'id' =>
"{$this->mID}-$info",
117 if ( in_array( $info, $this->mParams[
'disabled-options'],
true ) ) {
118 $thisAttribs[
'disabled'] =
'disabled';
120 $checked = in_array( $info, $value,
true );
122 $checkbox = $this->
getOneCheckbox( $checked, $attribs + $thisAttribs, $label );
124 $html .=
' ' . Html::rawElement(
126 [
'class' =>
'mw-htmlform-flatlist-item' ],
137 throw new MWException(
'HTMLMultiSelectField#getOneCheckbox() is not supported' );
139 $elementFunc = [ Html::class, $this->mOptionsLabelsNotFromMessage ?
'rawElement' :
'element' ];
141 Xml::check(
"{$this->mName}[]", $checked, $attribs ) .
143 call_user_func( $elementFunc,
145 [
'for' => $attribs[
'id'] ],
148 if ( $this->mParent->getConfig()->get( MainConfigNames::UseMediaWikiUIEverywhere ) ) {
149 $checkbox = Html::openElement(
'div', [
'class' =>
'mw-ui-checkbox' ] ) .
151 Html::closeElement(
'div' );
165 throw new MWException(
'HTMLMultiSelectField#getOptionsOOUI() is not supported' );
181 $this->mParent->getOutput()->addModules(
'oojs-ui-widgets' );
184 $value = array_filter( $value,
'is_scalar' );
186 $hasSections =
false;
187 $optionsOouiSections = [];
191 foreach ( $options as $label => $section ) {
192 if ( is_array( $section ) ) {
194 unset( $options[$label] );
201 $optionsOouiSections = array_merge(
206 '@phan-var array[][] $optionsOouiSections';
209 foreach ( $optionsOouiSections as $sectionLabel => $optionsOoui ) {
211 $attr[
'name'] =
"{$this->mName}[]";
213 $attr[
'value'] = $value;
215 $options = $optionsOoui;
216 foreach ( $options as &$option ) {
217 $option[
'disabled'] = in_array( $option[
'data'], $this->mParams[
'disabled-options'],
true );
219 if ( $this->mOptionsLabelsNotFromMessage ) {
220 foreach ( $options as &$option ) {
222 $option[
'label'] =
new OOUI\HtmlSnippet( $option[
'label'] );
226 $attr[
'options'] = $options;
228 $attr += OOUI\Element::configFromHtmlAttributes(
232 if ( $this->mClass !==
'' ) {
236 $widget =
new OOUI\CheckboxMultiselectInputWidget( $attr );
237 if ( $sectionLabel ) {
238 $out[] =
new OOUI\FieldsetLayout( [
239 'items' => [ $widget ],
241 'label' =>
new OOUI\HtmlSnippet( $sectionLabel ),
248 if ( !$hasSections && $out ) {
249 if ( $this->mPlaceholder ) {
250 $out[0]->setData( ( $out[0]->getData() ?: [] ) + [
251 'placeholder' => $this->mPlaceholder,
259 return implode(
'', $out );
269 $fromRequest = $request->getArray( $this->mName, [] );
289 return $this->mDefault ?? [];
299 $forcedOn = array_intersect( $this->mParams[
'disabled-options'], $this->
getDefault() );
302 foreach ( $options as $opt ) {
303 $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()
Stability: stableto 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.
static listDropDownOptionsOoui( $options)
Convert options for a drop-down box into a format accepted by OOUI\DropdownInputWidget etc.
static check( $name, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox.