*/
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;
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;
}
* @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 ) {
&& $this->mParams['required'] !== false
&& $value === ''
) {
- return $this->msg( 'htmlform-required' )->parse();
+ return $this->msg( 'htmlform-required' );
}
if ( isset( $this->mValidationCallback ) ) {
} elseif ( in_array( strval( $value ), $this->autocomplete, true ) ) {
return true;
} elseif ( $this->mParams['require-match'] ) {
- return $this->msg( 'htmlform-select-badoption' )->parse();
+ return $this->msg( 'htmlform-select-badoption' );
}
return true;
* @param string $value
* @param array $alldata
*
- * @return bool
+ * @return bool|string|Message
*/
public function validate( $value, $alldata ) {
return true;
if ( count( $validValues ) == count( $value ) ) {
return true;
} else {
- return $this->msg( 'htmlform-select-badoption' )->parse();
+ return $this->msg( 'htmlform-select-badoption' );
}
}
$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 ) );
}
}
$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 ) );
}
}
# 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.
$min = $this->mParams['min'];
if ( $min > $value ) {
- return $this->msg( 'htmlform-int-toolow', $min )->parseAsBlock();
+ return $this->msg( 'htmlform-int-toolow', $min );
}
}
$max = $this->mParams['max'];
if ( $max < $value ) {
- return $this->msg( 'htmlform-int-toohigh', $max )->parseAsBlock();
+ return $this->msg( 'htmlform-int-toohigh', $max );
}
}
&& $this->mParams['required'] !== false
&& !$values
) {
- return $this->msg( 'htmlform-cloner-required' )->parseAsBlock();
+ return $this->msg( 'htmlform-cloner-required' );
}
if ( isset( $values['nonjs'] ) ) {
# 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;
if ( count( $validValues ) == count( $value ) ) {
return true;
} else {
- return $this->msg( 'htmlform-select-badoption' )->parse();
+ return $this->msg( 'htmlform-select-badoption' );
}
}
}
if ( !is_string( $value ) && !is_int( $value ) ) {
- return $this->msg( 'htmlform-required' )->parse();
+ return $this->msg( 'htmlform-required' );
}
$validOptions = HTMLFormField::flattenOptions( $this->getOptions() );
if ( in_array( strval( $value ), $validOptions, true ) ) {
return true;
} else {
- return $this->msg( 'htmlform-select-badoption' )->parse();
+ return $this->msg( 'htmlform-select-badoption' );
}
}
* @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 ) {
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 ) ) {
if ( $status->isOK() ) {
$status->fatal( 'unknown-error' );
}
- return $status->getMessage()->parse();
+ return $status->getMessage();
}
if ( isset( $this->mValidationCallback ) ) {
&& $this->mParams['required'] !== false
&& $value[1] === ''
) {
- return $this->msg( 'htmlform-required' )->parse();
+ return $this->msg( 'htmlform-required' );
}
return true;
if ( in_array( strval( $value ), $validOptions, true ) ) {
return true;
} else {
- return $this->msg( 'htmlform-select-badoption' )->parse();
+ return $this->msg( 'htmlform-select-badoption' );
}
}
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 );
$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 );
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() {