Explorar o código

ZF-11035: support tag class for labels

- Applied patch; adds getters and setters for label tag class, and HTML
  generation of label tag class attribute

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@23778 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew %!s(int64=15) %!d(string=hai) anos
pai
achega
df17ee93e4

+ 52 - 2
library/Zend/Form/Decorator/Label.php

@@ -58,6 +58,12 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
     protected $_tag;
 
     /**
+     * Class for the HTML tag with which to surround label
+     * @var string
+     */
+    protected $_tagClass;
+
+    /**
      * Set element ID
      *
      * @param  string $id
@@ -129,6 +135,43 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
     }
 
     /**
+     * Set the class to apply to the HTML tag with which to surround label
+     *
+     * @param  string $tagClass
+     * @return Zend_Form_Decorator_Label
+     */
+    public function setTagClass($tagClass)
+    {
+        if (empty($tagClass)) {
+            $this->_tagClass = null;
+        } else {
+            $this->_tagClass = (string) $tagClass;
+        }
+
+        $this->removeOption('tagClass');
+
+        return $this;
+    }
+
+    /**
+     * Get the class to apply to the HTML tag, if any, with which to surround label
+     *
+     * @return void
+     */
+    public function getTagClass()
+    {
+        if (null === $this->_tagClass) {
+            $tagClass = $this->getOption('tagClass');
+            if (null !== $tagClass) {
+                $this->removeOption('tagClass');
+                $this->setTagClass($tagClass);
+            }
+        }
+
+        return $this->_tagClass;
+    }
+
+    /**
      * Get class with which to define label
      *
      * Appends either 'optional' or 'required' to class, depending on whether
@@ -297,6 +340,7 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
         $separator = $this->getSeparator();
         $placement = $this->getPlacement();
         $tag       = $this->getTag();
+        $tagClass  = $this->getTagClass();
         $id        = $this->getId();
         $class     = $this->getClass();
         $options   = $this->getOptions();
@@ -316,8 +360,14 @@ class Zend_Form_Decorator_Label extends Zend_Form_Decorator_Abstract
         if (null !== $tag) {
             require_once 'Zend/Form/Decorator/HtmlTag.php';
             $decorator = new Zend_Form_Decorator_HtmlTag();
-            $decorator->setOptions(array('tag' => $tag,
-                                         'id'  => $id . '-label'));
+            if (null !== $this->_tagClass) {
+                $decorator->setOptions(array('tag'   => $tag,
+                                             'id'    => $id . '-label',
+                                             'class' => $tagClass));
+            } else {
+                $decorator->setOptions(array('tag'   => $tag,
+                                             'id'    => $id . '-label'));
+            }
 
             $label = $decorator->render($label);
         }

+ 15 - 0
tests/Zend/Form/Decorator/LabelTest.php

@@ -20,6 +20,7 @@
  * @version    $Id$
  */
 
+ 
 // Call Zend_Form_Decorator_LabelTest::main() if this source file is executed directly.
 if (!defined("PHPUnit_MAIN_METHOD")) {
     define("PHPUnit_MAIN_METHOD", "Zend_Form_Decorator_LabelTest::main");
@@ -308,6 +309,20 @@ class Zend_Form_Decorator_LabelTest extends PHPUnit_Framework_TestCase
         $tag = $this->decorator->getTag();
         $this->assertTrue( NULL === $tag, $tag );
     }
+    
+
+    /**
+     * @group ZF-4841
+     */
+    public function testSettingTagClassToEmptyValueShouldSetTagClassToNull()
+    {
+        $element = new Zend_Form_Element_Text('foo', array('label' => 'Foo'));
+        $this->decorator->setElement($element)
+                        ->setOptions(array('tag' => 'dt'));
+        $this->decorator->setTagClass('');
+        $tagClass = $this->decorator->getTagClass();
+        $this->assertTrue( NULL === $tagClass, $tagClass );
+    }
 }
 
 // Call Zend_Form_Decorator_LabelTest::main() if this source file is executed directly.