ZF-1998: Customizing messages from Zend_Validate_EmailAddress


Ralf Kramer's original message:

{quote} Hi,

I tried to customize the message of Zend_Validate_EmailAddress, it works quite well, but I cant translate the messages that are added by Zend_Validate_Hostname in my $validators array.


$validators = array(
    'user_email' => array(
        'messages' => array(
                Zend_Validate_EmailAddress::INVALID =>
                "'%value%' ist keine gültige E-Mail-Adresse",

                Zend_Validate_EmailAddress::INVALID_HOSTNAME  =>
                "'%hostname%' ist kein gültiger Hostname. Bitte
                prüfen Sie Ihre E-Mail-Adresse  '%value%'",

                Zend_Validate_EmailAddress::INVALID_MX_RECORD  =>
                "'%hostname%' kein MX Record gefunden '%value%'",

                Zend_Validate_EmailAddress::DOT_ATOM =>
                "'%localPart%' ungültiges Format (dot-atom)",

                Zend_Validate_EmailAddress::QUOTED_STRING =>
                "'%localPart%' ungültiges Format",

                => "'%localPart%' ungültiges Format '%value%'",

When $user_email == "a" I get this messages:

["user_email"] => array(3) {
    [0] => string(81) "'a' ist kein gültiger Hostname. Bitte prüfen Sie Ihre E-Mail-Adresse  'aasda@a'"
    [1] => string(60) "'a' does not match the expected structure for a DNS hostname"
    [2] => string(82) "'a' appears to be a local network name but but local network names are not allowed"

So, there are two messages which are not translated, and I don't know how to translate them in my $validators array. Is there a way to achieve this?

If not, we should imho contemplate to ensure that each Zend_Validate_XXX class must gain access to all possible messages that could be added to the $messages array

Though Bill explained, in a recent posting, that developers might use $input->getErrors(); to customize their messages, I'd prefer to translate the messages using the MESSAGES metacommand of Zend_Filter_Input. Having it all defined in one place, allows for instance to pass the $validator array to the view, where it could be used for AJAX and other Javascript operations. It would also be somewhat more 'straight' if there is no exceptional rule bundled to the MESSAGES metacommand

I am not sure whether the proposals Zend_Validate_Builder, Zend_Filter_Builder will solve this issue. Actually I am not even sure whether it is an issue... ;-)

Best regards /Ralf {quote}


There is a proposal which aims to solve this issue for ALL classes...…

In my opinion it's better to have a framework wide solution than every class using it's own way. I would be willing to take over the work on this one, as it's related to Zend_Translate and Zend_Locale...

"Localizing and translating error messages for the Zend Framework"

Best regards Thomas I18N Team Leader

Wouldn't it be more in keeping with Zend_Translate to use this format to translate an error message?

echo $translate->_(Zend_Validate_EmailAddress::INVALID_HOSTNAME);

I believe this would work at present? Obviously I would prefer using some kind of codeword/error code to indentify each translated message since it will be easier to manage.

For example, if the error message changes by one character, it looks like you need to re-update your messages list. the old message would be invalid. Unless I misunderstand how Zend_Translate works.

It looks like the TMX format supports arbitrary message IDs.

Failing all this, the Hostname error messages may need to be added to Zend_Email. Unless the structure of the Zend_Validate_EmailAddress class is changed and it becomes a child of Zend_Validate_Hostname (since it shares some of its funtionality).

best wishes, Si

Actually, I believe that the Zend_Validate_Builder code (specifically Zend_Validate_Builder_ErrorManager) will do exactly what you want here. If you could, it would be great if you could check out the code from the Zend Laboratory, and give it a try. There are unit tests there that provide some usage examples (they're a bit terse, but not complicated).

You can check the code out from here, if you're interested:…

Can I confirm this issue should now be closed since customizing messages is now part of Zend_Validate core -…

No longer an issue.

The EmailAddress validator uses the Hostname validator for validating the domain part. So when using custom messages for EmailAddress you should also set custom messages for Hostname or the messages will be outputted with the default strings.

No longer an issue as with 1.5 or higher