Issues

ZF-11614: Label translation is attempted though label is NULL

Issue Type: Patch Created: 2011-07-27T10:34:00.000+0000 Last Updated: 2011-07-29T08:20:08.000+0000 Status: Open Fix version(s): Reporter: David Fuhr (davidfuhr) Assignee: Christian Albrecht (alab) Tags: - Zend_Form

Related issues: Attachments:

Description

The method getLabel() in Zend_Form_Elements sends the label value to the translator even if the label is not set (NULL). This results in an attempt to translate an empty string which in turn triggers a "missing translation" log entry or notice in Zend_Translate.

This patch resolves the issue and avoids a useless call on the translator:

<pre class="literal">
### Eclipse Workspace Patch 1.0
#P ZendFramework-trunk
Index: library/Zend/Form/Element.php
===================================================================
--- library/Zend/Form/Element.php   (Revision 24270)
+++ library/Zend/Form/Element.php   (Arbeitskopie)
@@ -624,7 +624,7 @@
     public function getLabel()
     {
         $translator = $this->getTranslator();
-        if (null !== $translator) {
+        if (null !== $this->_label && null !== $translator) {
             return $translator->translate($this->_label);
         }

Comments

Posted by David Fuhr (davidfuhr) on 2011-07-27T10:38:50.000+0000

Issue was introduced with ZF-6877

Posted by Matthew Weier O'Phinney (matthew) on 2011-07-28T21:50:00.000+0000

I wrote the following test in tests/Zend/Form/ElementTest.php, which should confirm the behavior you see:

<pre class="highlight">
    public function testDoesNotAttemptToTranslateEmptyLabels()
    {
        $this->element->setLabel(null);
        $translator = new Zend_Translate('array', array('FooBar' => 'BazBar'));
        $this->element->setTranslator($translator);
        $this->assertEquals('', $this->element->getLabel());
    }

The assumption is that if a notice or exception is raised, this test will not pass.

However, it does, on current trunk.

Can you provide a test case that reproduces the issue? If not, I'll resolve as "Can't Reproduce".

Posted by David Fuhr (davidfuhr) on 2011-07-29T08:20:08.000+0000

You may not call setLabel(null) as setLabel() contains a typecast to string (maybe the typecast should only be applied if the argument is not null - but that's a different story). So this problem only occures with a new untouched object with no label as the initial value is null. The translator has to have the option "logUntranslated" set to true:

<pre class="literal">
### Eclipse Workspace Patch 1.0
#P ZendFramework-trunk
Index: tests/Zend/Form/ElementTest.php
===================================================================
--- tests/Zend/Form/ElementTest.php (Revision 24297)
+++ tests/Zend/Form/ElementTest.php (Arbeitskopie)
@@ -2189,6 +2189,20 @@
         $validator = $username->getValidator('regex');
         $this->assertTrue($validator->zfBreakChainOnFailure);
     }
+    
+    /**
+     * @group ZF-11614
+     */
+    public function testDoesNotAttemptToTranslateNullLabels()
+    {
+        $translator = new Zend_Translate(array(
+            'adapter' => 'array',
+            'content' => array('FooBar' => 'BazBar'),
+            'logUntranslated' => true,
+        ));
+        $this->element->setTranslator($translator);
+        $this->assertNull($this->element->getLabel());
+    }
 }
 
 class Zend_Form_ElementTest_Decorator extends Zend_Form_Decorator_Abstract

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