فهرست منبع

ZF-6424: disable echoing of for attribute in Radio element

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18546 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 سال پیش
والد
کامیت
23c953f05d

+ 15 - 0
library/Zend/Form/Element/Radio.php

@@ -39,4 +39,19 @@ class Zend_Form_Element_Radio extends Zend_Form_Element_Multi
      * @var string
      */
     public $helper = 'formRadio';
+
+    /**
+     * Load default decorators
+     *
+     * Disables "for" attribute of label if label decorator enabled.
+     * 
+     * @return void
+     */
+    public function loadDefaultDecorators()
+    {
+        parent::loadDefaultDecorators();
+        if (false !== $decorator = $this->getDecorator('Label')) {
+            $decorator->setOption('disableFor', true);
+        }
+    }
 }

+ 12 - 9
library/Zend/View/Helper/FormLabel.php

@@ -50,17 +50,20 @@ class Zend_View_Helper_FormLabel extends Zend_View_Helper_FormElement
         // build the element
         if ($disable) {
             // disabled; display nothing
-            $xhtml = '';
-        } else {
-            $value = ($escape) ? $this->view->escape($value) : $value;
-
-            // enabled; display label
-            $xhtml = '<label'
-                   . ' for="' . $this->view->escape($id) . '"'
-                   . $this->_htmlAttribs($attribs)
-                   . '>' . $value . '</label>';
+            return  '';
         }
 
+        $value = ($escape) ? $this->view->escape($value) : $value;
+        $for   = (empty($attribs['disableFor']) || !$attribs['disableFor'])
+               ? ' for="' . $this->view->escape($id) . '"'
+               : '';
+
+        // enabled; display label
+        $xhtml = '<label'
+                . $for
+                . $this->_htmlAttribs($attribs)
+                . '>' . $value . '</label>';
+
         return $xhtml;
     }
 }

+ 14 - 0
tests/Zend/Form/Element/RadioTest.php

@@ -172,6 +172,20 @@ class Zend_Form_Element_RadioTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-6426
+     */
+    public function testRenderingShouldCreateLabelWithoutForAttribute()
+    {
+        $this->element->setMultiOptions(array(
+                'foo'  => 'Foo',
+                'bar'  => 'Bar',
+             ))
+             ->setLabel('Foo');
+        $html = $this->element->render($this->getView());
+        $this->assertNotContains('for="foo"', $html);
+    }
+
+    /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete
      *
      * @link   http://framework.zend.com/issues/browse/ZF-2794

+ 10 - 1
tests/Zend/View/Helper/FormLabelTest.php

@@ -116,7 +116,7 @@ class Zend_View_Helper_FormLabelTest extends PHPUnit_Framework_TestCase
     }
     
     /**
-     * ZF-2473
+     * @group ZF-2473
      */
     public function testCanDisableEscapingLabelValue()
     {
@@ -127,6 +127,15 @@ class Zend_View_Helper_FormLabelTest extends PHPUnit_Framework_TestCase
         $label = $this->helper->formLabel(array('name' => 'foo', 'value' => '<b>Label This!</b>', 'attribs' => array('escape' => false)));
         $this->assertContains('<b>Label This!</b>', $label);
     }
+
+    /**
+     * @group ZF-6426
+     */
+    public function testHelperShouldAllowSuppressionOfForAttribute()
+    {
+        $label = $this->helper->formLabel('foo', 'bar', array('disableFor' => true));
+        $this->assertNotContains('for="foo"', $label);
+    }
 }
 
 // Call Zend_View_Helper_FormLabelTest::main() if this source file is executed directly.