Fix \n handling for HTMLUsersMultiselectField
authorMatthew Flaschen <mflaschen@wikimedia.org>
Sun, 21 May 2017 13:28:59 +0000 (15:28 +0200)
committerRoan Kattouw <roan.kattouw@gmail.com>
Fri, 30 Jun 2017 02:02:46 +0000 (19:02 -0700)
Bug: T166836
Change-Id: I51b772946f1e50a21fb86cab969defb4647b820b

includes/htmlform/fields/HTMLUsersMultiselectField.php

index 8829f66..eb88909 100644 (file)
@@ -15,18 +15,16 @@ use MediaWiki\Widget\UsersMultiselectWidget;
  * @note This widget is not likely to remain functional in non-OOUI forms.
  */
 class HTMLUsersMultiselectField extends HTMLUserTextField {
-
        public function loadDataFromRequest( $request ) {
-               if ( !$request->getCheck( $this->mName ) ) {
-                       return $this->getDefault();
-               }
+               $value = $request->getText( $this->mName, $this->getDefault() );
 
-               $usersArray = explode( "\n", $request->getText( $this->mName ) );
+               $usersArray = explode( "\n", $value );
                // Remove empty lines
                $usersArray = array_values( array_filter( $usersArray, function( $username ) {
                        return trim( $username ) !== '';
                } ) );
-               return $usersArray;
+               // This function is expected to return a string
+               return implode( "\n", $usersArray );
        }
 
        public function validate( $value, $alldata ) {
@@ -38,7 +36,9 @@ class HTMLUsersMultiselectField extends HTMLUserTextField {
                        return false;
                }
 
-               foreach ( $value as $username ) {
+               // $value is a string, because HTMLForm fields store their values as strings
+               $usersArray = explode( "\n", $value );
+               foreach ( $usersArray as $username ) {
                        $result = parent::validate( $username, $alldata );
                        if ( $result !== true ) {
                                return $result;
@@ -48,12 +48,12 @@ class HTMLUsersMultiselectField extends HTMLUserTextField {
                return true;
        }
 
-       public function getInputHTML( $values ) {
+       public function getInputHTML( $value ) {
                $this->mParent->getOutput()->enableOOUI();
-               return $this->getInputOOUI( $values );
+               return $this->getInputOOUI( $value );
        }
 
-       public function getInputOOUI( $values ) {
+       public function getInputOOUI( $value ) {
                $params = [ 'name' => $this->mName ];
 
                if ( isset( $this->mParams['default'] ) ) {
@@ -68,8 +68,9 @@ class HTMLUsersMultiselectField extends HTMLUserTextField {
                                                        ->plain();
                }
 
-               if ( !is_null( $values ) ) {
-                       $params['default'] = $values;
+               if ( !is_null( $value ) ) {
+                       // $value is a string, but the widget expects an array
+                       $params['default'] = explode( "\n", $value );
                }
 
                // Make the field auto-infusable when it's used inside a legacy HTMLForm rather than OOUIHTMLForm