Merge "Remove single-item HTML list for re-upload link"
[lhc/web/wiklou.git] / includes / htmlform / fields / HTMLSelectAndOtherField.php
index cdb8f5b..354432b 100644 (file)
@@ -47,6 +47,10 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        $textAttribs['class'][] = $this->mClass;
                }
 
+               if ( isset( $this->mParams['maxlength-unit'] ) ) {
+                       $textAttribs['data-mw-maxlength-unit'] = $this->mParams['maxlength-unit'];
+               }
+
                $allowedParams = [
                        'required',
                        'autofocus',
@@ -54,6 +58,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        'disabled',
                        'tabindex',
                        'maxlength', // gets dynamic with javascript, see mediawiki.htmlform.js
+                       'maxlength-unit', // 'bytes' or 'codepoints', see mediawiki.htmlform.js
                ];
 
                $textAttribs += $this->getAttributes( $allowedParams );
@@ -73,9 +78,6 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                # TextInput
                $textAttribs = [
                        'name' => $this->mName . '-other',
-                       'size' => $this->getSize(),
-                       'class' => [ 'mw-htmlform-select-and-other-field' ],
-                       'data-id-select' => $this->mID . '-select',
                        'value' => $value[2],
                ];
 
@@ -117,11 +119,22 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        $dropdownInputAttribs['classes'] = [ $this->mClass ];
                }
 
+               $disabled = false;
+               if ( isset( $this->mParams[ 'disabled' ] ) && $this->mParams[ 'disabled' ] ) {
+                       $disabled = true;
+               }
+
                return $this->getInputWidget( [
                        'id' => $this->mID,
+                       'disabled' => $disabled,
                        'textinput' => $textAttribs,
                        'dropdowninput' => $dropdownInputAttribs,
                        'or' => false,
+                       'required' => $this->mParams[ 'required' ] ?? false,
+                       'classes' => [ 'mw-htmlform-select-and-other-field' ],
+                       'data' => [
+                               'maxlengthUnit' => $this->mParams['maxlength-unit'] ?? 'bytes'
+                       ],
                ] );
        }
 
@@ -129,10 +142,39 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                return new MediaWiki\Widget\SelectWithInputWidget( $params );
        }
 
+       /**
+        * @inheritDoc
+        */
+       public function getDefault() {
+               $default = parent::getDefault();
+
+               // Default values of empty form
+               $final = '';
+               $list = 'other';
+               $text = '';
+
+               if ( $default !== null ) {
+                       $final = $default;
+                       // Assume the default is a text value, with the 'other' option selected.
+                       // Then check if that assumption is correct, and update $list and $text if not.
+                       $text = $final;
+                       foreach ( $this->mFlatOptions as $option ) {
+                               $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
+                               if ( strpos( $final, $match ) === 0 ) {
+                                       $list = $option;
+                                       $text = substr( $final, strlen( $match ) );
+                                       break;
+                               }
+                       }
+               }
+
+               return [ $final, $list, $text ];
+       }
+
        /**
         * @param WebRequest $request
         *
-        * @return array("<overall message>","<select value>","<text field value>")
+        * @return array ["<overall message>","<select value>","<text field value>"]
         */
        public function loadDataFromRequest( $request ) {
                if ( $request->getCheck( $this->mName ) ) {
@@ -151,26 +193,13 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        } else {
                                $final = $list . $this->msg( 'colon-separator' )->inContentLanguage()->text() . $text;
                        }
-               } else {
-                       $final = $this->getDefault();
-
-                       $list = 'other';
-                       $text = $final;
-                       foreach ( $this->mFlatOptions as $option ) {
-                               $match = $option . $this->msg( 'colon-separator' )->inContentLanguage()->text();
-                               if ( strpos( $text, $match ) === 0 ) {
-                                       $list = $option;
-                                       $text = substr( $text, strlen( $match ) );
-                                       break;
-                               }
-                       }
+                       return [ $final, $list, $text ];
                }
-
-               return [ $final, $list, $text ];
+               return $this->getDefault();
        }
 
        public function getSize() {
-               return isset( $this->mParams['size'] ) ? $this->mParams['size'] : 45;
+               return $this->mParams['size'] ?? 45;
        }
 
        public function validate( $value, $alldata ) {
@@ -185,7 +214,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
 
                if ( isset( $this->mParams['required'] )
                        && $this->mParams['required'] !== false
-                       && $value[1] === ''
+                       && $value[0] === ''
                ) {
                        return $this->msg( 'htmlform-required' );
                }