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.
13 * - flatlist: If given, the options will be displayed on a single line (wrapping to following
14 * lines if necessary), rather than each one on a line of its own. This is desirable mostly
15 * for very short lists of concisely labelled options.
17 public function __construct( $params ) {
18 parent
::__construct( $params );
20 // For backwards compatibility, also handle the old way with 'cssclass' => 'mw-chosen'
21 if ( isset( $params['dropdown'] ) ||
strpos( $this->mClass
, 'mw-chosen' ) !== false ) {
22 $this->mClass
.= ' mw-htmlform-dropdown';
25 if ( isset( $params['flatlist'] ) ) {
26 $this->mClass
.= ' mw-htmlform-flatlist';
30 public function validate( $value, $alldata ) {
31 $p = parent
::validate( $value, $alldata );
37 if ( !is_array( $value ) ) {
41 # If all options are valid, array_intersect of the valid options
42 # and the provided options will return the provided options.
43 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
45 $validValues = array_intersect( $value, $validOptions );
46 if ( count( $validValues ) == count( $value ) ) {
49 return $this->msg( 'htmlform-select-badoption' );
53 public function getInputHTML( $value ) {
54 if ( isset( $this->mParams
['dropdown'] ) ) {
55 $this->mParent
->getOutput()->addModules( 'jquery.chosen' );
58 $value = HTMLFormField
::forceToStringRecursive( $value );
59 $html = $this->formatOptions( $this->getOptions(), $value );
64 public function formatOptions( $options, $value ) {
67 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
69 foreach ( $options as $label => $info ) {
70 if ( is_array( $info ) ) {
71 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
72 $html .= $this->formatOptions( $info, $value );
75 'id' => "{$this->mID}-$info",
78 $checked = in_array( $info, $value, true );
80 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
82 $html .= ' ' . Html
::rawElement(
84 [ 'class' => 'mw-htmlform-flatlist-item' ],
93 protected function getOneCheckbox( $checked, $attribs, $label ) {
94 if ( $this->mParent
instanceof OOUIHTMLForm
) {
95 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
97 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
99 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
101 call_user_func( $elementFunc,
103 [ 'for' => $attribs['id'] ],
106 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
107 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
109 Html
::closeElement( 'div' );
116 * Get the OOUI version of this field.
119 * @param string[] $value
120 * @return OOUI\CheckboxMultiselectInputWidget
122 public function getInputOOUI( $value ) {
123 $this->mParent
->getOutput()->addModules( 'oojs-ui-widgets' );
125 $attr = $this->getTooltipAndAccessKey();
126 $attr['id'] = $this->mID
;
127 $attr['name'] = "{$this->mName}[]";
129 $attr['value'] = $value;
130 $attr['options'] = $this->getOptionsOOUI();
132 if ( $this->mOptionsLabelsNotFromMessage
) {
133 foreach ( $attr['options'] as &$option ) {
134 $option['label'] = new OOUI\
HtmlSnippet( $option['label'] );
138 $attr +
= OOUI\Element
::configFromHtmlAttributes(
139 $this->getAttributes( [ 'disabled', 'tabindex' ] )
142 if ( $this->mClass
!== '' ) {
143 $attr['classes'] = [ $this->mClass
];
146 return new OOUI\
CheckboxMultiselectInputWidget( $attr );
150 * @param WebRequest $request
152 * @return string|array
154 public function loadDataFromRequest( $request ) {
155 if ( $this->isSubmitAttempt( $request ) ) {
156 // Checkboxes are just not added to the request arrays if they're not checked,
157 // so it's perfectly possible for there not to be an entry at all
158 return $request->getArray( $this->mName
, [] );
160 // That's ok, the user has not yet submitted the form, so show the defaults
161 return $this->getDefault();
165 public function getDefault() {
166 if ( isset( $this->mDefault
) ) {
167 return $this->mDefault
;
173 public function filterDataForSubmit( $data ) {
174 $data = HTMLFormField
::forceToStringRecursive( $data );
175 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
178 foreach ( $options as $opt ) {
179 $res["$opt"] = in_array( $opt, $data, true );
185 protected function needsLabel() {