Merge "HTMLForm: Allow returning Message objects from HTMLFormField::validate()"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 25 Nov 2016 09:11:07 +0000 (09:11 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 25 Nov 2016 09:11:07 +0000 (09:11 +0000)
17 files changed:
includes/htmlform/HTMLForm.php
includes/htmlform/HTMLFormField.php
includes/htmlform/fields/HTMLAutoCompleteSelectField.php
includes/htmlform/fields/HTMLButtonField.php
includes/htmlform/fields/HTMLCheckMatrix.php
includes/htmlform/fields/HTMLDateTimeField.php
includes/htmlform/fields/HTMLFloatField.php
includes/htmlform/fields/HTMLFormFieldCloner.php
includes/htmlform/fields/HTMLIntField.php
includes/htmlform/fields/HTMLMultiSelectField.php
includes/htmlform/fields/HTMLRadioField.php
includes/htmlform/fields/HTMLRestrictionsField.php
includes/htmlform/fields/HTMLSelectAndOtherField.php
includes/htmlform/fields/HTMLSelectField.php
includes/htmlform/fields/HTMLTitleTextField.php
includes/htmlform/fields/HTMLUserTextField.php
tests/phpunit/includes/htmlform/HTMLCheckMatrixTest.php

index cadb502..71ccaa3 100644 (file)
@@ -604,10 +604,14 @@ class HTMLForm extends ContextSource {
         */
        public function trySubmit() {
                $valid = true;
-               $hoistedErrors = [];
-               $hoistedErrors[] = isset( $this->mValidationErrorMessage )
-                       ? $this->mValidationErrorMessage
-                       : [ 'htmlform-invalid-input' ];
+               $hoistedErrors = Status::newGood();
+               if ( $this->mValidationErrorMessage ) {
+                       foreach ( (array)$this->mValidationErrorMessage as $error ) {
+                               call_user_func_array( [ $hoistedErrors, 'fatal' ], $error );
+                       }
+               } else {
+                       $hoistedErrors->fatal( 'htmlform-invalid-input' );
+               }
 
                $this->mWasSubmitted = true;
 
@@ -634,15 +638,16 @@ class HTMLForm extends ContextSource {
                        if ( $res !== true ) {
                                $valid = false;
                                if ( $res !== false && !$field->canDisplayErrors() ) {
-                                       $hoistedErrors[] = [ 'rawmessage', $res ];
+                                       if ( is_string( $res ) ) {
+                                               $hoistedErrors->fatal( 'rawmessage', $res );
+                                       } else {
+                                               $hoistedErrors->fatal( $res );
+                                       }
                                }
                        }
                }
 
                if ( !$valid ) {
-                       if ( count( $hoistedErrors ) === 1 ) {
-                               $hoistedErrors = $hoistedErrors[0];
-                       }
                        return $hoistedErrors;
                }
 
index 8390a0b..71aa275 100644 (file)
@@ -296,7 +296,7 @@ abstract class HTMLFormField {
         * @param string|array $value The value the field was submitted with
         * @param array $alldata The data collected from the form
         *
-        * @return bool|string True on success, or String error to display, or
+        * @return bool|string|Message True on success, or String/Message error to display, or
         *   false to fail validation without displaying an error.
         */
        public function validate( $value, $alldata ) {
@@ -308,7 +308,7 @@ abstract class HTMLFormField {
                        && $this->mParams['required'] !== false
                        && $value === ''
                ) {
-                       return $this->msg( 'htmlform-required' )->parse();
+                       return $this->msg( 'htmlform-required' );
                }
 
                if ( isset( $this->mValidationCallback ) ) {
index b0890c6..63e77ce 100644 (file)
@@ -114,7 +114,7 @@ class HTMLAutoCompleteSelectField extends HTMLTextField {
                } elseif ( in_array( strval( $value ), $this->autocompleteData, true ) ) {
                        return true;
                } elseif ( $this->mParams['require-match'] ) {
-                       return $this->msg( 'htmlform-select-badoption' )->parse();
+                       return $this->msg( 'htmlform-select-badoption' );
                }
 
                return true;
index 64fe7ed..285490b 100644 (file)
@@ -113,7 +113,7 @@ class HTMLButtonField extends HTMLFormField {
         * @param string $value
         * @param array $alldata
         *
-        * @return bool
+        * @return bool|string|Message
         */
        public function validate( $value, $alldata ) {
                return true;
index 890cd7c..46172a5 100644 (file)
@@ -65,7 +65,7 @@ class HTMLCheckMatrix extends HTMLFormField implements HTMLNestedFilterable {
                if ( count( $validValues ) == count( $value ) ) {
                        return true;
                } else {
-                       return $this->msg( 'htmlform-select-badoption' )->parse();
+                       return $this->msg( 'htmlform-select-badoption' );
                }
        }
 
index 88dcd24..5d4a15e 100644 (file)
@@ -100,15 +100,14 @@ class HTMLDateTimeField extends HTMLTextField {
                $date = $this->parseDate( $value );
                if ( !$date ) {
                        // Messages: htmlform-date-invalid htmlform-time-invalid htmlform-datetime-invalid
-                       return $this->msg( "htmlform-{$this->mType}-invalid" )->parseAsBlock();
+                       return $this->msg( "htmlform-{$this->mType}-invalid" );
                }
 
                if ( isset( $this->mParams['min'] ) ) {
                        $min = $this->parseDate( $this->mParams['min'] );
                        if ( $min && $date < $min ) {
                                // Messages: htmlform-date-toolow htmlform-time-toolow htmlform-datetime-toolow
-                               return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) )
-                                       ->parseAsBlock();
+                               return $this->msg( "htmlform-{$this->mType}-toolow", $this->formatDate( $min ) );
                        }
                }
 
@@ -116,8 +115,7 @@ class HTMLDateTimeField extends HTMLTextField {
                        $max = $this->parseDate( $this->mParams['max'] );
                        if ( $max && $date > $max ) {
                                // Messages: htmlform-date-toohigh htmlform-time-toohigh htmlform-datetime-toohigh
-                               return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) )
-                                       ->parseAsBlock();
+                               return $this->msg( "htmlform-{$this->mType}-toohigh", $this->formatDate( $max ) );
                        }
                }
 
index d1250c0..d2d54e2 100644 (file)
@@ -20,7 +20,7 @@ class HTMLFloatField extends HTMLTextField {
                # https://www.w3.org/TR/html5/infrastructure.html#floating-point-numbers
                # with the addition that a leading '+' sign is ok.
                if ( !preg_match( '/^((\+|\-)?\d+(\.\d+)?(E(\+|\-)?\d+)?)?$/i', $value ) ) {
-                       return $this->msg( 'htmlform-float-invalid' )->parseAsBlock();
+                       return $this->msg( 'htmlform-float-invalid' );
                }
 
                # The "int" part of these message names is rather confusing.
@@ -29,7 +29,7 @@ class HTMLFloatField extends HTMLTextField {
                        $min = $this->mParams['min'];
 
                        if ( $min > $value ) {
-                               return $this->msg( 'htmlform-int-toolow', $min )->parseAsBlock();
+                               return $this->msg( 'htmlform-int-toolow', $min );
                        }
                }
 
@@ -37,7 +37,7 @@ class HTMLFloatField extends HTMLTextField {
                        $max = $this->mParams['max'];
 
                        if ( $max < $value ) {
-                               return $this->msg( 'htmlform-int-toohigh', $max )->parseAsBlock();
+                               return $this->msg( 'htmlform-int-toohigh', $max );
                        }
                }
 
index 5d8f491..2584b6b 100644 (file)
@@ -224,7 +224,7 @@ class HTMLFormFieldCloner extends HTMLFormField {
                        && $this->mParams['required'] !== false
                        && !$values
                ) {
-                       return $this->msg( 'htmlform-cloner-required' )->parseAsBlock();
+                       return $this->msg( 'htmlform-cloner-required' );
                }
 
                if ( isset( $values['nonjs'] ) ) {
index c87a778..02af7de 100644 (file)
@@ -18,7 +18,7 @@ class HTMLIntField extends HTMLFloatField {
                # input does not require its value to be numeric).  If you want a tidier
                # value to, eg, save in the DB, clean it up with intval().
                if ( !preg_match( '/^((\+|\-)?\d+)?$/', trim( $value ) ) ) {
-                       return $this->msg( 'htmlform-int-invalid' )->parseAsBlock();
+                       return $this->msg( 'htmlform-int-invalid' );
                }
 
                return true;
index 58de763..05a2ba6 100644 (file)
@@ -46,7 +46,7 @@ class HTMLMultiSelectField extends HTMLFormField implements HTMLNestedFilterable
                if ( count( $validValues ) == count( $value ) ) {
                        return true;
                } else {
-                       return $this->msg( 'htmlform-select-badoption' )->parse();
+                       return $this->msg( 'htmlform-select-badoption' );
                }
        }
 
index 69dc617..06ec372 100644 (file)
@@ -27,7 +27,7 @@ class HTMLRadioField extends HTMLFormField {
                }
 
                if ( !is_string( $value ) && !is_int( $value ) ) {
-                       return $this->msg( 'htmlform-required' )->parse();
+                       return $this->msg( 'htmlform-required' );
                }
 
                $validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
@@ -35,7 +35,7 @@ class HTMLRadioField extends HTMLFormField {
                if ( in_array( strval( $value ), $validOptions, true ) ) {
                        return true;
                } else {
-                       return $this->msg( 'htmlform-select-badoption' )->parse();
+                       return $this->msg( 'htmlform-select-badoption' );
                }
        }
 
index 5a18025..dbf2c8f 100644 (file)
@@ -61,7 +61,7 @@ class HTMLRestrictionsField extends HTMLTextAreaField {
         * @param string|MWRestrictions $value The value the field was submitted with
         * @param array $alldata The data collected from the form
         *
-        * @return bool|string True on success, or String error to display, or
+        * @return bool|string|Message True on success, or String/Message error to display, or
         *   false to fail validation without displaying an error.
         */
        public function validate( $value, $alldata ) {
@@ -73,7 +73,7 @@ class HTMLRestrictionsField extends HTMLTextAreaField {
                        isset( $this->mParams['required'] ) && $this->mParams['required'] !== false
                        && $value instanceof MWRestrictions && !$value->toArray()['IPAddresses']
                ) {
-                       return $this->msg( 'htmlform-required' )->parse();
+                       return $this->msg( 'htmlform-required' );
                }
 
                if ( is_string( $value ) ) {
@@ -87,7 +87,7 @@ class HTMLRestrictionsField extends HTMLTextAreaField {
                        if ( $status->isOK() ) {
                                $status->fatal( 'unknown-error' );
                        }
-                       return $status->getMessage()->parse();
+                       return $status->getMessage();
                }
 
                if ( isset( $this->mValidationCallback ) ) {
index 86e8e75..9af60e5 100644 (file)
@@ -125,7 +125,7 @@ class HTMLSelectAndOtherField extends HTMLSelectField {
                        && $this->mParams['required'] !== false
                        && $value[1] === ''
                ) {
-                       return $this->msg( 'htmlform-required' )->parse();
+                       return $this->msg( 'htmlform-required' );
                }
 
                return true;
index c1f8e42..18c741b 100644 (file)
@@ -16,7 +16,7 @@ class HTMLSelectField extends HTMLFormField {
                if ( in_array( strval( $value ), $validOptions, true ) ) {
                        return true;
                } else {
-                       return $this->msg( 'htmlform-select-badoption' )->parse();
+                       return $this->msg( 'htmlform-select-badoption' );
                }
        }
 
index a15b90e..3eb3f5d 100644 (file)
@@ -51,22 +51,22 @@ class HTMLTitleTextField extends HTMLTextField {
                        if ( $params ) {
                                $msg->params( $params );
                        }
-                       return $msg->parse();
+                       return $msg;
                }
 
                $text = $title->getPrefixedText();
                if ( $this->mParams['namespace'] !== false &&
                        !$title->inNamespace( $this->mParams['namespace'] )
                ) {
-                       return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text )->parse();
+                       return $this->msg( 'htmlform-title-badnamespace', $this->mParams['namespace'], $text );
                }
 
                if ( $this->mParams['creatable'] && !$title->canExist() ) {
-                       return $this->msg( 'htmlform-title-not-creatable', $text )->escaped();
+                       return $this->msg( 'htmlform-title-not-creatable', $text );
                }
 
                if ( $this->mParams['exists'] && !$title->exists() ) {
-                       return $this->msg( 'htmlform-title-not-exists', $text )->parse();
+                       return $this->msg( 'htmlform-title-not-exists', $text );
                }
 
                return parent::validate( $value, $alldata );
index 14b5e59..12c09c1 100644 (file)
@@ -28,12 +28,12 @@ class HTMLUserTextField extends HTMLTextField {
                $user = User::newFromName( $value, false );
 
                if ( !$user ) {
-                       return $this->msg( 'htmlform-user-not-valid', $value )->parse();
+                       return $this->msg( 'htmlform-user-not-valid', $value );
                } elseif (
                        ( $this->mParams['exists'] && $user->getId() === 0 ) &&
                        !( $this->mParams['ipallowed'] && User::isIP( $value ) )
                ) {
-                       return $this->msg( 'htmlform-user-not-exists', $user->getName() )->parse();
+                       return $this->msg( 'htmlform-user-not-exists', $user->getName() );
                }
 
                return parent::validate( $value, $alldata );
index dd5b58b..f97716b 100644 (file)
@@ -51,7 +51,7 @@ class HtmlCheckMatrixTest extends MediaWikiTestCase {
 
        public function testValidateAllowsOnlyKnownTags() {
                $field = new HTMLCheckMatrix( self::$defaultOptions );
-               $this->assertInternalType( 'string', $this->validate( $field, [ 'foo' ] ) );
+               $this->assertInstanceOf( Message::class, $this->validate( $field, [ 'foo' ] ) );
        }
 
        public function testValidateAcceptsPartialTagList() {