ZF-3313: Form Element Translator Bug

Issue Type: Bug Created: 2008-05-22T22:38:57.000+0000 Last Updated: 2011-12-08T08:02:07.000+0000 Status: Resolved Fix version(s): - 1.6.0 (02/Sep/08)

Reporter: Brent Foust (bfoust) Assignee: Satoru Yoshida (satoruyoshida) Tags: - Zend_Form

Related issues: - ZF-2954



This fix is for _createMessage in Abstract.php line 191. It has been tested with 1.5.1, but probably applies to 1.5.2, as well.


Custom messages are not translated for form element validators. Inserting the following two lines before line 191 of Abstract.php will solve the problem:

<pre class="highlight">
           if ($translator->isTranslated($message))
                $message = $translator->translate($message);

== Background ==

The default translator used by all forms may be set by calling:

<pre class="highlight">

Then, when $form->isValid is called, each of the element validators have their translators set to that same default translator (if they don't have a translator explicitly defined).

This works fine, if you want to use the same error message for every validator instance of the same name. For example, NotEmpty, the validator automatically created by $element->setRequired(true), uses the key 'isEmpty' for its error message. If 'isEmpty' is mapped to a string in the default translator, it will be picked up and used as the error message when a value is not supplied for that field (element) of the form.

However, it is more-likely the case that in a large application, different fields will require different error messages when they are left blank. This is no problem if you're not relying on a translator. Each of those NotEmpty validator instances can be given customized messages by calling setMessage on them.

But in that case, the translator stops working, seemingly because of an oversight in the implementation:

(for this example, assume $messageKey has the value 'isEmpty') _createMessage in Abstract.php line 191:

    $message = $this->_messageTemplates[$messageKey];

    if (null !== ($translator = $this->getTranslator())) {
        if ($translator->isTranslated($messageKey)) {
            $message = $translator->translate($messageKey);

But what if the developer has specified:

$password->addValidator('NotEmpty', true); $password->getValidator('NotEmpty')->setMessage('form.login.password.errBlank');

<pre class="highlight">

with the following line in en_US.php:

        'form.login.password.errBlank'    => "Please enter your password."

The above code in Zend Framework's Abstract::_createMessage will output only the translation key as the error message ('form.login.password.errBlank').

== The Solution ==

What is needed, is to translate the message (user-defined key), if it has been set, otherwise, the default key for that validator:

        if ($translator->isTranslated($message))
            $message = $translator->translate($message);
        else if ($translator->isTranslated($messageKey))
            $message = $translator->translate($messageKey);

This fix will behave as expected in both cases -- whether the developer has defined a custom message key, or not.



### Comments

Posted by Wil Sinclair (wil) on 2008-06-01T14:06:54.000+0000

Please evaluate and categorize as necessary.



Posted by Justin Hendrickson (toxygene) on 2008-06-04T10:25:50.000+0000

Duplicate of [#ZF-2954] <>



Posted by old of Satoru Yoshida ( on 2008-09-01T01:19:45.000+0000

I change Resolution value to Duplicate.



Posted by Wil Sinclair (wil) on 2008-09-02T10:38:57.000+0000

Updating for the 1.6.0 release.



Posted by malt (malt) on 2010-11-03T04:33:42.000+0000

I noticed that this fix was reversed in Version 1.10.6 and is still in the latest version. Any particular reason why this was changed back? However the reported problem is present again and should be fixed.



Have you found an issue?

See the Overview section for more details.


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

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