Procházet zdrojové kódy

Merge r25222 to 1.12 release branch

git-svn-id: http://framework.zend.com/svn/framework/standard/branches/release-1.12@25223 44c647ce-9c0f-0410-b52a-842ac1e357ba
frosch před 13 roky
rodič
revize
53d299b5b7
2 změnil soubory, kde provedl 81 přidání a 4 odebrání
  1. 12 2
      library/Zend/Form.php
  2. 69 2
      tests/Zend/Form/FormTest.php

+ 12 - 2
library/Zend/Form.php

@@ -1027,7 +1027,9 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
         if (is_string($element)) {
             if (null === $name) {
                 require_once 'Zend/Form/Exception.php';
-                throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
+                throw new Zend_Form_Exception(
+                    'Elements specified by string must have an accompanying name'
+                );
             }
 
             $this->_elements[$name] = $this->createElement($element, $name, $options);
@@ -1038,6 +1040,12 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
                 $prefixPaths = array_merge($prefixPaths, $this->_elementPrefixPaths);
             }
 
+            if (is_array($this->_elementDecorators)
+                && 0 == count($element->getDecorators())
+            ) {
+                $element->setDecorators($this->_elementDecorators);
+            }
+
             if (null === $name) {
                 $name = $element->getName();
             }
@@ -1046,7 +1054,9 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
             $this->_elements[$name]->addPrefixPaths($prefixPaths);
         } else {
             require_once 'Zend/Form/Exception.php';
-            throw new Zend_Form_Exception('Element must be specified by string or Zend_Form_Element instance');
+            throw new Zend_Form_Exception(
+                'Element must be specified by string or Zend_Form_Element instance'
+            );
         }
 
         $this->_order[$name] = $this->_elements[$name]->getOrder();

+ 69 - 2
tests/Zend/Form/FormTest.php

@@ -4729,8 +4729,8 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
         $form = new Zend_Form();
         $form->setElementDecorators(
             array(
-                 new Zend_Form_Decorator_ViewHelper,
-                 new Zend_Form_Decorator_Label,
+                 new Zend_Form_Decorator_ViewHelper(),
+                 new Zend_Form_Decorator_Label(),
             )
         );
         $element = $form->createElement('text', 'foo');
@@ -4748,6 +4748,73 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
 
         $this->assertEquals($expected, $actual);
     }
+
+    /**
+     * @group ZF-12387
+     */
+    public function testAddElementAsObjectWithDecoratorsShouldNotLoseDecorators()
+    {
+        // Init form
+        $form = new Zend_Form();
+        $form->setElementDecorators(
+            array(
+                 new Zend_Form_Decorator_ViewHelper(),
+                 new Zend_Form_Decorator_Label(),
+            )
+        );
+
+        $element = new Zend_Form_Element_Text('foo');
+        $element->setDecorators(array('Errors', 'Description'));
+        $form->addElement($element);
+
+        // Test
+        $expected = array(
+            'Zend_Form_Decorator_Errors',
+            'Zend_Form_Decorator_Description',
+        );
+
+        $actual = array();
+        foreach ($form->getElement('foo')->getDecorators() as $decorator) {
+            $actual[] = get_class($decorator);
+        }
+
+        $this->assertEquals($expected, $actual);
+    }
+
+    /**
+     * @group ZF-12387
+     */
+    public function testAddElementAsObjectGetsElementDecoratorsIfNoneIsExplicitlySet()
+    {
+        // Init form
+        $form = new Zend_Form();
+        $form->setElementDecorators(
+            array(
+                 new Zend_Form_Decorator_ViewHelper(),
+                 new Zend_Form_Decorator_Label(),
+            )
+        );
+
+        // Add element
+        $element = new Zend_Form_Element_Text(
+            'foo',
+            array('disableLoadDefaultDecorators' => true)
+        );
+        $form->addElement($element);
+
+        // Test
+        $expected = array(
+            'Zend_Form_Decorator_ViewHelper',
+            'Zend_Form_Decorator_Label',
+        );
+
+        $actual = array();
+        foreach ($form->getElement('foo')->getDecorators() as $decorator) {
+            $actual[] = get_class($decorator);
+        }
+
+        $this->assertEquals($expected, $actual);
+    }
 }
 
 class Zend_Form_FormTest_DisplayGroup extends Zend_Form_DisplayGroup