Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Minor
-
Resolution: Cannot Reproduce
-
Affects Version/s: 1.0.2
-
Fix Version/s: 1.8.1
-
Component/s: Zend_Validate
-
Labels:None
Description
In the following scenario, i want to check an email address for validity and show the user only a single message that the email address which was entered is invalid.
(this validator was used in combination with Zend_Filter_Input where the 'messages' => '...' key in the array is ignored in that case)
$emailToCheck = 'foo@bar.com)'; // note the ')'
$emailValidator = new Zend_Validate_EmailAddress();
$emailValidator->setMessage('Please enter a valid email address.');
$valid = $emailValidator->isValid($emailToCheck);
var_dump($valid);
if (!$valid) {
var_dump($emailValidator->getMessages());
}
I didn't set any key for the messages so I expected that all error messages are set to my specific error message but when I looked at the implementation of Zend_Validate_Abstract::setMessage(), only the first message key gets that message:
public function setMessage($messageString, $messageKey = null)
{
if ($messageKey === null) {
$keys = array_keys($this->_messageTemplates);
$messageKey = current($keys);
}
[...]
}
So I would expect that I can set all messages to my defined error message but neither setMessage() nor setMessages() has this implemented.
The next problem is located in the Zend_Validate_EmailAddress validator, where the hostname validator passes all messages and errors to the to Zend_Validate_EmailAddress validator:
$hostnameResult = $this->hostnameValidator->isValid($this->_hostname);
if (!$hostnameResult) {
$this->_error(self::INVALID_HOSTNAME);
// Get messages and errors from hostnameValidator
foreach ($this->hostnameValidator->getMessages() as $message) {
$this->_messages[] = $message;
}
foreach ($this->hostnameValidator->getErrors() as $error) {
$this->_errors[] = $error;
}
}
There is no possibility to break the chain at any place to only get one single user defined error message when the hostname was invalid.
Issue Links
| This issue is duplicated by: | ||||
| ZF-10690 | Zend_Validate_EmailAddress setMessage does not work properly |
|
|
|
Each validation class can emit multiple messages, each of which corresponding to a cause for validation failure. A validation failure may indeed be caused by more than one reason, and this is why validation classes can emit multiple messages. There is no method in the public API to change all of the validation failure messages to a single message because this would obfuscate the reasons for validation failure. These reasons are not necessarily intended for display to users, however, and this is where the confusion seems to be.
Of course, you may only want to show a single, consolidated validation failure message to your end users, and this is a common and perfectly valid use case.
It should be simple to implement in at least one of the following ways: