ZF-1998: Customizing messages from Zend_Validate_EmailAddress

Issue Type: Improvement Created: 2007-09-26T10:50:36.000+0000 Last Updated: 2008-12-08T12:30:14.000+0000 Status: Resolved Fix version(s): - 1.8.0 (30/Apr/09)

Reporter: Darby Felton (darby) Assignee: Thomas Weidner (thomas) Tags: - Zend_Filter_Input

  • Zend_Validate

Related issues: Attachments:


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.


<pre class="highlight">
$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:

<pre class="highlight">
["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}


Posted by Thomas Weidner (thomas) on 2007-09-26T12:17:37.000+0000

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

Posted by Simon R Jones (studio24) on 2007-09-26T12:54:00.000+0000

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

Posted by Bryce Lohr (gearhead) on 2007-09-26T21:19:34.000+0000

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:…

Posted by Simon R Jones (studio24) on 2008-05-26T11:34:56.000+0000

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

Posted by Thomas Weidner (thomas) on 2008-12-08T12:29:40.000+0000

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.

Posted by Thomas Weidner (thomas) on 2008-12-08T12:30:14.000+0000

No longer an issue as with 1.5 or higher

Have you found an issue?

See the Overview section for more details.


© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.