6 class HTMLMultiSelectField
extends HTMLFormField
implements HTMLNestedFilterable
{
9 * In adition to the usual HTMLFormField parameters, this can take the following fields:
10 * - dropdown: If given, the options will be displayed inside a dropdown with a text field that
11 * can be used to filter them. This is desirable mostly for very long lists of options.
12 * This only works for users with JavaScript support and falls back to the list of checkboxes.
14 public function __construct( $params ) {
15 parent
::__construct( $params );
17 // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
18 if ( isset( $params['dropdown'] ) ||
strpos( $this->mClass
, 'mw-chosen' ) !== false ) {
19 $this->mClass
.= ' mw-htmlform-dropdown';
23 function validate( $value, $alldata ) {
24 $p = parent
::validate( $value, $alldata );
30 if ( !is_array( $value ) ) {
34 # If all options are valid, array_intersect of the valid options
35 # and the provided options will return the provided options.
36 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
38 $validValues = array_intersect( $value, $validOptions );
39 if ( count( $validValues ) == count( $value ) ) {
42 return $this->msg( 'htmlform-select-badoption' )->parse();
46 function getInputHTML( $value ) {
47 if ( isset( $this->mParams
['dropdown'] ) ) {
48 $this->mParent
->getOutput()->addModules( 'jquery.chosen' );
51 $value = HTMLFormField
::forceToStringRecursive( $value );
52 $html = $this->formatOptions( $this->getOptions(), $value );
57 function formatOptions( $options, $value ) {
60 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
62 foreach ( $options as $label => $info ) {
63 if ( is_array( $info ) ) {
64 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
65 $html .= $this->formatOptions( $info, $value );
68 'id' => "{$this->mID}-$info",
71 $checked = in_array( $info, $value, true );
73 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
75 $html .= ' ' . Html
::rawElement(
77 [ 'class' => 'mw-htmlform-flatlist-item' ],
86 protected function getOneCheckbox( $checked, $attribs, $label ) {
87 if ( $this->mParent
instanceof OOUIHTMLForm
) {
88 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
90 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
92 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
94 call_user_func( $elementFunc,
96 [ 'for' => $attribs['id'] ],
99 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
100 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
102 Html
::closeElement( 'div' );
109 * Get the OOUI version of this field.
112 * @param string[] $value
113 * @return OOUI\CheckboxMultiselectInputWidget
115 public function getInputOOUI( $value ) {
116 $attr = $this->getTooltipAndAccessKey();
117 $attr['id'] = $this->mID
;
118 $attr['name'] = "{$this->mName}[]";
120 $attr['value'] = $value;
121 $attr['options'] = $this->getOptionsOOUI();
123 if ( $this->mOptionsLabelsNotFromMessage
) {
124 foreach ( $attr['options'] as &$option ) {
125 $option['label'] = new OOUI\
HtmlSnippet( $option['label'] );
129 $attr +
= OOUI\Element
::configFromHtmlAttributes(
130 $this->getAttributes( [ 'disabled', 'tabindex' ] )
133 if ( $this->mClass
!== '' ) {
134 $attr['classes'] = [ $this->mClass
];
137 return new OOUI\
CheckboxMultiselectInputWidget( $attr );
141 * @param WebRequest $request
145 function loadDataFromRequest( $request ) {
146 if ( $this->isSubmitAttempt( $request ) ) {
147 // Checkboxes are just not added to the request arrays if they're not checked,
148 // so it's perfectly possible for there not to be an entry at all
149 return $request->getArray( $this->mName
, [] );
151 // That's ok, the user has not yet submitted the form, so show the defaults
152 return $this->getDefault();
156 function getDefault() {
157 if ( isset( $this->mDefault
) ) {
158 return $this->mDefault
;
164 function filterDataForSubmit( $data ) {
165 $data = HTMLFormField
::forceToStringRecursive( $data );
166 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
169 foreach ( $options as $opt ) {
170 $res["$opt"] = in_array( $opt, $data, true );
176 protected function needsLabel() {