Merge "Fix \n handling for HTMLUsersMultiselectField"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 30 Jun 2017 02:21:13 +0000 (02:21 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 30 Jun 2017 02:21:13 +0000 (02:21 +0000)
1  2 
includes/htmlform/fields/HTMLUsersMultiselectField.php

@@@ -15,18 -15,16 +15,16 @@@ use MediaWiki\Widget\UsersMultiselectWi
   * @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 ) {
 +              $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 +36,9 @@@
                        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;
                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 +68,9 @@@
                                                        ->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