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 $checkbox = Xml
::check(
52 in_array( $info, $value, true ),
53 $attribs +
$thisAttribs
55 $checkbox .= ' ' . call_user_func( $elementFunc,
57 array( 'for' => "{$this->mID}-$info" ),
61 $html .= ' ' . Html
::rawElement(
63 array( 'class' => 'mw-htmlform-flatlist-item' ),
73 * @param WebRequest $request
77 function loadDataFromRequest( $request ) {
78 if ( $this->mParent
->getMethod() == 'post' ) {
79 if ( $request->wasPosted() ) {
80 # Checkboxes are just not added to the request arrays if they're not checked,
81 # so it's perfectly possible for there not to be an entry at all
82 return $request->getArray( $this->mName
, array() );
84 # That's ok, the user has not yet submitted the form, so show the defaults
85 return $this->getDefault();
88 # This is the impossible case: if we look at $_GET and see no data for our
89 # field, is it because the user has not yet submitted the form, or that they
90 # have submitted it with all the options unchecked? We will have to assume the
91 # latter, which basically means that you can't specify 'positive' defaults
94 return $request->getArray( $this->mName
, array() );
98 function getDefault() {
99 if ( isset( $this->mDefault
) ) {
100 return $this->mDefault
;
106 function filterDataForSubmit( $data ) {
107 $data = HTMLFormField
::forceToStringRecursive( $data );
108 $options = HTMLFormField
::flattenOptions( $this->getOptions() );
111 foreach ( $options as $opt ) {
112 $res["$opt"] = in_array( $opt, $data, true );
118 protected function needsLabel() {