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

ZF-6070: allow individual element options to override global element decorators

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16047 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 лет назад
Родитель
Сommit
896d83f53e
2 измененных файлов с 57 добавлено и 9 удалено
  1. 26 9
      library/Zend/Form.php
  2. 31 0
      tests/Zend/Form/FormTest.php

+ 26 - 9
library/Zend/Form.php

@@ -98,6 +98,12 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
     protected $_displayGroups = array();
 
     /**
+     * Global decorators to apply to all elements
+     * @var null|array
+     */
+    protected $_elementDecorators;
+
+    /**
      * Prefix paths to use when creating elements
      * @var array
      */
@@ -306,16 +312,16 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
             unset($options['displayGroupPrefixPath']);                             
         }
 
+        if (isset($options['elementDecorators'])) {
+            $this->_elementDecorators = $options['elementDecorators'];
+            unset($options['elementDecorators']);
+        }
+
         if (isset($options['elements'])) {
             $this->setElements($options['elements']);
             unset($options['elements']);
         }
 
-        if (isset($options['elementDecorators'])) {
-            $elementDecorators = $options['elementDecorators'];
-            unset($options['elementDecorators']);
-        }
-
         if (isset($options['defaultDisplayGroupClass'])) {
             $this->setDefaultDisplayGroupClass($options['defaultDisplayGroupClass']);
             unset($options['defaultDisplayGroupClass']);
@@ -355,10 +361,6 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
             }
         }
 
-        if (isset($elementDecorators)) {
-            $this->setElementDecorators($elementDecorators);
-        }
-
         if (isset($displayGroupDecorators)) {
             $this->setDisplayGroupDecorators($displayGroupDecorators);
         }
@@ -983,6 +985,19 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
                 throw new Zend_Form_Exception('Elements specified by string must have an accompanying name');
             }
 
+            if (is_array($this->_elementDecorators)) {
+                if (null === $options) {
+                    $options = array('decorators' => $this->_elementDecorators);
+                } elseif ($options instanceof Zend_Config) {
+                    $options = $options->toArray();
+                }
+                if (is_array($options) 
+                    && !array_key_exists('decorators', $options)
+                ) {
+                    $options['decorators'] = $this->_elementDecorators;
+                }
+            }
+
             $this->_elements[$name] = $this->createElement($element, $name, $options);
         } elseif ($element instanceof Zend_Form_Element) {
             $prefixPaths              = array();
@@ -2544,6 +2559,8 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
             $element->setDecorators($decorators);
         }
 
+        $this->_elementDecorators = $decorators;
+
         return $this;
     }
 

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

@@ -3668,6 +3668,37 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-6070
+     */
+    public function testIndividualElementDecoratorsShouldOverrideGlobalElementDecorators()
+    {
+        $this->form->setOptions(array(
+            'elementDecorators' => array(
+                'ViewHelper',
+                'Label',
+            ),
+            'elements' => array(
+                'foo' => array(
+                    'type' => 'text',
+                    'options' => array(
+                        'decorators' => array(
+                            'Errors',
+                            'ViewHelper',
+                        ),
+                    ),
+                ),
+            ),
+        ));
+        $element    = $this->form->getElement('foo');
+        $expected   = array('Zend_Form_Decorator_Errors', 'Zend_Form_Decorator_ViewHelper');
+        $actual     = array();
+        foreach ($element->getDecorators() as $decorator) {
+            $actual[] = get_class($decorator);
+        }
+        $this->assertSame($expected, $actual);
+    }
+
+    /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete
      *
      * @link   http://framework.zend.com/issues/browse/ZF-2794