6 class HTMLMultiSelectField
extends HTMLFormField
implements HTMLNestedFilterable
{
7 function validate( $value, $alldata ) {
8 $p = parent
::validate( $value, $alldata );
14 if ( !is_array( $value ) ) {
18 # If all options are valid, array_intersect of the valid options
19 # and the provided options will return the provided options.
20 $validOptions = HTMLFormField
::flattenOptions( $this->getOptions() );
22 $validValues = array_intersect( $value, $validOptions );
23 if ( count( $validValues ) == count( $value ) ) {
26 return $this->msg( 'htmlform-select-badoption' )->parse();
30 function getInputHTML( $value ) {
31 $value = HTMLFormField
::forceToStringRecursive( $value );
32 $html = $this->formatOptions( $this->getOptions(), $value );
37 function formatOptions( $options, $value ) {
40 $attribs = $this->getAttributes( [ 'disabled', 'tabindex' ] );
42 foreach ( $options as $label => $info ) {
43 if ( is_array( $info ) ) {
44 $html .= Html
::rawElement( 'h1', [], $label ) . "\n";
45 $html .= $this->formatOptions( $info, $value );
48 'id' => "{$this->mID}-$info",
51 $checked = in_array( $info, $value, true );
53 $checkbox = $this->getOneCheckbox( $checked, $attribs +
$thisAttribs, $label );
55 $html .= ' ' . Html
::rawElement(
57 [ 'class' => 'mw-htmlform-flatlist-item' ],
66 protected function getOneCheckbox( $checked, $attribs, $label ) {
67 if ( $this->mParent
instanceof OOUIHTMLForm
) {
68 throw new MWException( 'HTMLMultiSelectField#getOneCheckbox() is not supported' );
70 $elementFunc = [ 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' ];
72 Xml
::check( "{$this->mName}[]", $checked, $attribs ) .
74 call_user_func( $elementFunc,
76 [ 'for' => $attribs['id'] ],
79 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
80 $checkbox = Html
::openElement( 'div', [ 'class' => 'mw-ui-checkbox' ] ) .
82 Html
::closeElement( 'div' );
89 * Get the OOUI version of this field.
92 * @param string[] $value
93 * @return OOUI\CheckboxMultiselectInputWidget
95 public function getInputOOUI( $value ) {
96 $attr = $this->getTooltipAndAccessKey();
97 $attr['id'] = $this->mID
;
98 $attr['name'] = "{$this->mName}[]";
100 $attr['value'] = $value;
101 $attr['options'] = $this->getOptionsOOUI();
103 if ( $this->mOptionsLabelsNotFromMessage
) {
104 foreach ( $attr['options'] as &$option ) {
105 $option['label'] = new OOUI\
HtmlSnippet( $option['label'] );
109 $attr +
= OOUI\Element
::configFromHtmlAttributes(
110 $this->getAttributes( [ 'disabled', 'tabindex' ] )
113 if ( $this->mClass
!== '' ) {
114 $attr['classes'] = [ $this->mClass
];
117 return new OOUI\
CheckboxMultiselectInputWidget( $attr );
121 * @param WebRequest $request
125 function loadDataFromRequest( $request ) {
126 if ( $this->mParent
->getMethod() == 'post' ) {
127 if ( $request->wasPosted() ) {
128 # Checkboxes are just not added to the request arrays if they're not checked,
129 # so it's perfectly possible for there not to be an entry at all
130 return $request->getArray( $this->mName
, [] );
132 # That's ok, the user has not yet submitted the form, so show the defaults
133 return $this->getDefault();
136 # This is the impossible case: if we look at $_GET and see no data for our
137 # field, is it because the user has not yet submitted the form, or that they
138 # have submitted it with all the options unchecked? We will have to assume the
139 # latter, which basically means that you can't specify 'positive' defaults
142 return $request->getArray( $this->mName
, [] );
146 function getDefault() {
147 if ( isset( $this->mDefault
) ) {
148 return $this->mDefault
;
154 function filterDataForSubmit( $data ) {
155 $data = HTMLFormField
::forceToStringRecursive( $data );
156 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
159 foreach ( $options as $opt ) {
160 $res["$opt"] = in_array( $opt, $data, true );
166 protected function needsLabel() {