Issues

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

Issue Type: Bug Created: 2012-10-10T02:52:27.000+0000 Last Updated: 2012-12-22T20:05:40.000+0000 Status: Resolved Fix version(s): - 1.12.2 (25/Feb/13)

Reporter: Chris Kings-Lynne (chriskl) Assignee: Rob Allen (rob) Tags: - Zend_Form

  • FixForZF1.12.1

Related issues: Attachments:

Description

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

Culprit is this code:

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

Posted by Rob Allen (rob) on 2012-11-16T15:45:59.000+0000

Does this patch fix it?

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

Posted by Rob Allen (rob) on 2012-12-22T20:05:40.000+0000

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

Have you found an issue?

See the Overview section for more details.

Copyright

© 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.

Contacts