Issues

ZF-3313: Form Element Translator Bug

Description

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.

Abstract:

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


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

== Background ==

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

<pre class="highlight"><code>

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');



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

Please evaluate and categorize as necessary.

I change Resolution value to Duplicate.

Updating for the 1.6.0 release.

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.