Explorar o código

ZF-11729: do not render elements in display groups twice

- Added handling in FormElements decorator to ensure that if an element belongs
  to a display group, it is not rendered (until it's _in_ the display group)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@24452 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew %!s(int64=14) %!d(string=hai) anos
pai
achega
063645b982
Modificáronse 2 ficheiros con 28 adicións e 0 borrados
  1. 10 0
      library/Zend/Form/Decorator/FormElements.php
  2. 18 0
      tests/Zend/Form/FormTest.php

+ 10 - 0
library/Zend/Form/Decorator/FormElements.php

@@ -76,6 +76,7 @@ class Zend_Form_Decorator_FormElements extends Zend_Form_Decorator_Abstract
 
         $belongsTo      = ($form instanceof Zend_Form) ? $form->getElementsBelongTo() : null;
         $elementContent = '';
+        $displayGroups  = ($form instanceof Zend_Form) ? $form->getDisplayGroups() : array();
         $separator      = $this->getSeparator();
         $translator     = $form->getTranslator();
         $items          = array();
@@ -84,6 +85,15 @@ class Zend_Form_Decorator_FormElements extends Zend_Form_Decorator_Abstract
             $item->setView($view)
                  ->setTranslator($translator);
             if ($item instanceof Zend_Form_Element) {
+                foreach ($displayGroups as $group) {
+                    $elementName = $item->getName();
+                    $element     = $group->getElement($elementName);
+                    if ($element) {
+                        // Element belongs to display group; only render in that
+                        // context.
+                        continue 2;
+                    }
+                }
                 $item->setBelongsTo($belongsTo);
             } elseif (!empty($belongsTo) && ($item instanceof Zend_Form)) {
                 if ($item->isArray()) {

+ 18 - 0
tests/Zend/Form/FormTest.php

@@ -4461,6 +4461,24 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
         $form = new Zend_Form();
         $form->addElement(NULL);
     }
+
+    /**
+     * @group ZF-11729
+     */
+    public function testDashSeparatedElementsInDisplayGroupsShouldNotRenderOutsideDisplayGroup()
+    {
+        $form = new Zend_Form();
+        $form->addElement('text', 'random-element-name', array(
+            'label' => 'This is weird',
+            'value' => 'think its a bug',
+        ));
+        $form->addDisplayGroup(array('random-element-name'), 'foobar', array(
+            'legend' => 'foobar',
+        ));
+        $html = $form->render($this->getView());
+        $count = substr_count($html, 'randomelementname-element');
+        $this->assertEquals(1, $count, $html);
+    }
 }
 
 class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup