Move LikeMatch to Rdbms namespace
[mediawiki.git] / includes / htmlform / fields / HTMLUsersMultiselectField.php
blob8c1241d015feabc0f6e108627846b366668f6a9a
1 <?php
3 use MediaWiki\Widget\UsersMultiselectWidget;
5 /**
6 * Implements a capsule multiselect input field for user names.
8 * Besides the parameters recognized by HTMLUserTextField, additional recognized
9 * parameters are:
10 * default - (optional) Array of usernames to use as preset data
11 * placeholder - (optional) Custom placeholder message for input
13 * The result is the array of usernames
15 * @note This widget is not likely to remain functional in non-OOUI forms.
17 class HTMLUsersMultiselectField extends HTMLUserTextField {
19 public function loadDataFromRequest( $request ) {
20 if ( !$request->getCheck( $this->mName ) ) {
21 return $this->getDefault();
24 $usersArray = explode( "\n", $request->getText( $this->mName ) );
25 // Remove empty lines
26 $usersArray = array_values( array_filter( $usersArray, function( $username ) {
27 return trim( $username ) !== '';
28 } ) );
29 return $usersArray;
32 public function validate( $value, $alldata ) {
33 if ( !$this->mParams['exists'] ) {
34 return true;
37 if ( is_null( $value ) ) {
38 return false;
41 foreach ( $value as $username ) {
42 $result = parent::validate( $username, $alldata );
43 if ( $result !== true ) {
44 return $result;
48 return true;
51 public function getInputHTML( $values ) {
52 $this->mParent->getOutput()->enableOOUI();
53 return $this->getInputOOUI( $values );
56 public function getInputOOUI( $values ) {
57 $params = [ 'name' => $this->mName ];
59 if ( isset( $this->mParams['default'] ) ) {
60 $params['default'] = $this->mParams['default'];
63 if ( isset( $this->mParams['placeholder'] ) ) {
64 $params['placeholder'] = $this->mParams['placeholder'];
65 } else {
66 $params['placeholder'] = $this->msg( 'mw-widgets-usersmultiselect-placeholder' )
67 ->inContentLanguage()
68 ->plain();
71 if ( !is_null( $values ) ) {
72 $params['default'] = $values;
75 return new UsersMultiselectWidget( $params );
78 protected function shouldInfuseOOUI() {
79 return true;
82 protected function getOOUIModules() {
83 return [ 'mediawiki.widgets.UsersMultiselectWidget' ];