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->mParams
['options'] );
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 $html = $this->formatOptions( $this->mParams
['options'], $value );
36 function formatOptions( $options, $value ) {
41 if ( !empty( $this->mParams
['disabled'] ) ) {
42 $attribs['disabled'] = 'disabled';
45 foreach ( $options as $label => $info ) {
46 if ( is_array( $info ) ) {
47 $html .= Html
::rawElement( 'h1', array(), $label ) . "\n";
48 $html .= $this->formatOptions( $info, $value );
50 $thisAttribs = array( 'id' => "{$this->mID}-$info", 'value' => $info );
52 $checkbox = Xml
::check(
54 in_array( $info, $value, true ),
55 $attribs +
$thisAttribs
57 $checkbox .= ' ' . Html
::rawElement(
59 array( 'for' => "{$this->mID}-$info" ),
63 $html .= ' ' . Html
::rawElement(
65 array( 'class' => 'mw-htmlform-flatlist-item' ),
75 * @param $request WebRequest
79 function loadDataFromRequest( $request ) {
80 if ( $this->mParent
->getMethod() == 'post' ) {
81 if ( $request->wasPosted() ) {
82 # Checkboxes are just not added to the request arrays if they're not checked,
83 # so it's perfectly possible for there not to be an entry at all
84 return $request->getArray( $this->mName
, array() );
86 # That's ok, the user has not yet submitted the form, so show the defaults
87 return $this->getDefault();
90 # This is the impossible case: if we look at $_GET and see no data for our
91 # field, is it because the user has not yet submitted the form, or that they
92 # have submitted it with all the options unchecked? We will have to assume the
93 # latter, which basically means that you can't specify 'positive' defaults
96 return $request->getArray( $this->mName
, array() );
100 function getDefault() {
101 if ( isset( $this->mDefault
) ) {
102 return $this->mDefault
;
108 function filterDataForSubmit( $data ) {
109 $options = HTMLFormField
::flattenOptions( $this->mParams
['options'] );
112 foreach ( $options as $opt ) {
113 $res["$opt"] = in_array( $opt, $data );
119 protected function needsLabel() {