Просмотр исходного кода

ZF-7404: properly initialize element when calling individual decorators (patch courtesy Mon Zafra)

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@19129 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 лет назад
Родитель
Сommit
7cee5375c3
2 измененных файлов с 27 добавлено и 0 удалено
  1. 17 0
      library/Zend/Form/Element.php
  2. 10 0
      tests/Zend/Form/Element/HashTest.php

+ 17 - 0
library/Zend/Form/Element.php

@@ -214,6 +214,16 @@ class Zend_Form_Element implements Zend_Validate_Interface
     protected $_view;
 
     /**
+     * Is a specific decorator being rendered via the magic renderDecorator()?
+     * 
+     * This is to allow execution of logic inside the render() methods of child
+     * elements during the magic call while skipping the parent render() method.
+     * 
+     * @var bool
+     */
+    protected $_isPartialRendering = false;
+
+    /**
      * Constructor
      *
      * $spec may be:
@@ -920,6 +930,9 @@ class Zend_Form_Element implements Zend_Validate_Interface
     public function __call($method, $args)
     {
         if ('render' == substr($method, 0, 6)) {
+            $this->_isPartialRendering = true;
+            $this->render();
+            $this->_isPartialRendering = false;
             $decoratorName = substr($method, 6);
             if (false !== ($decorator = $this->getDecorator($decoratorName))) {
                 $decorator->setElement($this);
@@ -1951,6 +1964,10 @@ class Zend_Form_Element implements Zend_Validate_Interface
      */
     public function render(Zend_View_Interface $view = null)
     {
+        if ($this->_isPartialRendering) {
+            return '';
+        }
+
         if (null !== $view) {
             $this->setView($view);
         }

+ 10 - 0
tests/Zend/Form/Element/HashTest.php

@@ -199,6 +199,16 @@ class Zend_Form_Element_HashTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-7404
+     */
+    public function testShouldRenderHashTokenIfRenderedThroughMagicCall()
+    {
+        $this->element->setView($this->getView());
+        $html = $this->element->renderViewHelper();
+        $this->assertContains($this->element->getHash(), $html, 'Html is: ' . $html);
+    }
+
+    /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete
      *
      * @link   http://framework.zend.com/issues/browse/ZF-2794