From 16076cbf9931d84098db3e3aa2d9e3e6bf049796 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Gerg=C5=91=20Tisza?= Date: Tue, 15 Sep 2015 21:49:25 +0000 Subject: [PATCH] Hoist validation errors from hidden fields to the top of the form HTMLFormField subclasses are supposed to handle error display but some (like hidden fields) have no means of doing this. Add a HTMLFormField::canDisplayErrors() method which can be overridden to return false, in which case HTMLForm will take care of the error display. Also adds a 'rawmessage' message which can be used to wrap arbitrary text. This can be passed to methods which expect a message specifier array but do not allow a message object (so the RawMessage class cannot be used), such as HTMLFormField::trySubmit(). Bug: T112635 Change-Id: I5d73536805774ff2ee0ec64b5442650c4888dc84 --- includes/htmlform/HTMLForm.php | 27 +++++++++++++++++++-------- includes/htmlform/HTMLFormField.php | 9 +++++++++ includes/htmlform/HTMLHiddenField.php | 4 ++++ languages/i18n/en.json | 1 + languages/i18n/qqq.json | 1 + 5 files changed, 34 insertions(+), 8 deletions(-) diff --git a/includes/htmlform/HTMLForm.php b/includes/htmlform/HTMLForm.php index 38729a5ec3..11eb04d9db 100644 --- a/includes/htmlform/HTMLForm.php +++ b/includes/htmlform/HTMLForm.php @@ -537,6 +537,12 @@ class HTMLForm extends ContextSource { * params) or strings (message keys) */ function trySubmit() { + $valid = true; + $hoistedErrors = array(); + $hoistedErrors[] = isset( $this->mValidationErrorMessage ) + ? $this->mValidationErrorMessage + : array( 'htmlform-invalid-input' ); + $this->mWasSubmitted = true; # Check for cancelled submission @@ -558,15 +564,20 @@ class HTMLForm extends ContextSource { if ( $field->isHidden( $this->mFieldData ) ) { continue; } - if ( $field->validate( - $this->mFieldData[$fieldname], - $this->mFieldData ) - !== true - ) { - return isset( $this->mValidationErrorMessage ) - ? $this->mValidationErrorMessage - : array( 'htmlform-invalid-input' ); + $res = $field->validate( $this->mFieldData[$fieldname], $this->mFieldData ); + if ( $res !== true ) { + $valid = false; + if ( $res !== false && !$field->canDisplayErrors() ) { + $hoistedErrors[] = array( 'rawmessage', $res ); + } + } + } + + if ( !$valid ) { + if ( count( $hoistedErrors ) === 1 ) { + $hoistedErrors = $hoistedErrors[0]; } + return $hoistedErrors; } $callback = $this->mSubmitCallback; diff --git a/includes/htmlform/HTMLFormField.php b/includes/htmlform/HTMLFormField.php index 0fab033dce..5d405c3135 100644 --- a/includes/htmlform/HTMLFormField.php +++ b/includes/htmlform/HTMLFormField.php @@ -55,6 +55,15 @@ abstract class HTMLFormField { return false; } + /** + * True if this field type is able to display errors; false if validation errors need to be + * displayed in the main HTMLForm error area. + * @return bool + */ + public function canDisplayErrors() { + return true; + } + /** * Get a translated interface message * diff --git a/includes/htmlform/HTMLHiddenField.php b/includes/htmlform/HTMLHiddenField.php index ffde9151f2..e4695f7c51 100644 --- a/includes/htmlform/HTMLHiddenField.php +++ b/includes/htmlform/HTMLHiddenField.php @@ -55,4 +55,8 @@ class HTMLHiddenField extends HTMLFormField { public function getInputHTML( $value ) { return ''; } + + public function canDisplayErrors() { + return false; + } } diff --git a/languages/i18n/en.json b/languages/i18n/en.json index 51cc927bdf..38bba3925d 100644 --- a/languages/i18n/en.json +++ b/languages/i18n/en.json @@ -3554,6 +3554,7 @@ "htmlform-title-not-exists": "[[:$1]] does not exist.", "htmlform-user-not-exists": "$1 does not exist.", "htmlform-user-not-valid": "$1 isn't a valid username.", + "rawmessage": "$1", "sqlite-has-fts": "$1 with full-text search support", "sqlite-no-fts": "$1 without full-text search support", "logentry-delete-delete": "$1 {{GENDER:$2|deleted}} page $3", diff --git a/languages/i18n/qqq.json b/languages/i18n/qqq.json index 0cf37cd884..0455a413f8 100644 --- a/languages/i18n/qqq.json +++ b/languages/i18n/qqq.json @@ -3725,6 +3725,7 @@ "htmlform-title-not-exists": "Error message shown if the page title provided by the user does not exist. $1 is the page title.", "htmlform-user-not-exists": "Error message shown if a user with the name provided by the user does not exist. $1 is the username.", "htmlform-user-not-valid": "Error message shown if the name provided by the user isn't a valid username. $1 is the username.", + "rawmessage": "{{notranslate}} Used to pass arbitrary text as a message specifier array", "sqlite-has-fts": "Shown on [[Special:Version]].\nParameters:\n* $1 - version", "sqlite-no-fts": "Shown on [[Special:Version]].\nParameters:\n* $1 - version", "logentry-delete-delete": "{{Logentry|[[Special:Log/delete]]}}", -- 2.20.1