ZF-12439: Error message code produces array to string conversion error

Description

Going $element->setError('string') when $element->getValue() is an empty array causes an array to string conversion notice.

Culprit is this code:


    protected function _getErrorMessages()
    {
        $translator = $this->getTranslator();
        $messages   = $this->getErrorMessages();
        $value      = $this->getValue();
        foreach ($messages as $key => $message) {
            if (null !== $translator) {
                $message = $translator->translate($message);
            }
            if (($this->isArray() || is_array($value))
                && !empty($value)
            ) {
                $aggregateMessages = array();
                foreach ($value as $val) {
                    $aggregateMessages[] = str_replace('%value%', $val, $message);
                }
                $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
            } else {
                $messages[$key] = str_replace('%value%', $value, $message);
            }
        }
        return $messages;
    }

The problem is the !empty($value) causes the code to execute the second branch of the if() clause, as empty(array()) === true.

Comments

Does this patch fix it?


Index: library/Zend/Form/Element.php
===================================================================
--- library/Zend/Form/Element.php   (revision 25124)
+++ library/Zend/Form/Element.php   (working copy)
@@ -2242,14 +2242,14 @@
             if (null !== $translator) {
                 $message = $translator->translate($message);
             }
-            if (($this->isArray() || is_array($value))
-                && !empty($value)
-            ) {
+            if ($this->isArray() || is_array($value)) {
                 $aggregateMessages = array();
                 foreach ($value as $val) {
                     $aggregateMessages[] = str_replace('%value%', $val, $message);
                 }
-                $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
+                if (count($aggregateMessages)) {
+                    $messages[$key] = implode($this->getErrorMessageSeparator(), $arrayggregateMessages);
+                }
             } else {
                 $messages[$key] = str_replace('%value%', $value, $message);
             }

Fixed on trunk (25172) and release-1.12 (25173)