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( array( 'disabled', 'tabindex' ) );
41 $elementFunc = array( 'Html', $this->mOptionsLabelsNotFromMessage ?
'rawElement' : 'element' );
43 foreach ( $options as $label => $info ) {
44 if ( is_array( $info ) ) {
45 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
46 $html .= $this->formatOptions( $info, $value );
48 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
50 // @todo: Make this use checkLabel for consistency purposes
51 $checkbox = Xml
::check(
53 in_array( $info, $value, true ),
54 $attribs +
$thisAttribs
56 $checkbox .= ' ' . call_user_func( $elementFunc,
58 array( 'for' => "{$this->mID}-$info" ),
62 if ( $this->mParent
->getConfig()->get( 'UseMediaWikiUIEverywhere' ) ) {
63 $checkbox = Html
::rawElement(
65 array( 'class' => 'mw-ui-checkbox' ),
70 $html .= ' ' . Html
::rawElement(
72 array( 'class' => 'mw-htmlform-flatlist-item' ),
82 * @param WebRequest $request
86 function loadDataFromRequest( $request ) {
87 if ( $this->mParent
->getMethod() == 'post' ) {
88 if ( $request->wasPosted() ) {
89 # Checkboxes are just not added to the request arrays if they're not checked,
90 # so it's perfectly possible for there not to be an entry at all
91 return $request->getArray( $this->mName
, array() );
93 # That's ok, the user has not yet submitted the form, so show the defaults
94 return $this->getDefault();
97 # This is the impossible case: if we look at $_GET and see no data for our
98 # field, is it because the user has not yet submitted the form, or that they
99 # have submitted it with all the options unchecked? We will have to assume the
100 # latter, which basically means that you can't specify 'positive' defaults
103 return $request->getArray( $this->mName
, array() );
107 function getDefault() {
108 if ( isset( $this->mDefault
) ) {
109 return $this->mDefault
;
115 function filterDataForSubmit( $data ) {
116 $data = HTMLFormField
::forceToStringRecursive( $data );
117 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
120 foreach ( $options as $opt ) {
121 $res["$opt"] = in_array( $opt, $data, true );
127 protected function needsLabel() {