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

    Zend_Form::setDefaultTranslator(new Zend_Translate(...));

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

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.

Brent
-- 
View this message in context: 
http://www.nabble.com/Form-Element-Translator-Bug-tp17416776p17416776.html
Sent from the Zend Framework mailing list archive at Nabble.com.

Reply via email to