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

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:

### 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

Issue was introduced with ZF-6877

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


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

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:

### 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