Sfoglia il codice sorgente

ZF-8151: Make FormErrors work with ArrayNotation

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22270 44c647ce-9c0f-0410-b52a-842ac1e357ba
alab 15 anni fa
parent
commit
42c7f56857
2 ha cambiato i file con 45 aggiunte e 30 eliminazioni
  1. 34 0
      library/Zend/Form.php
  2. 11 30
      library/Zend/Form/Decorator/FormErrors.php

+ 34 - 0
library/Zend/Form.php

@@ -2139,6 +2139,40 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
     }
 
     /**
+     * Returns a one dimensional numerical indexed array with the
+     * Elements, SubForms and Elements from DisplayGroups as Values.
+     *
+     * Subitems are inserted based on their order Setting if set,
+     * otherwise they are appended, the resulting numerical index
+     * may differ from the order value.
+     * 
+     * @access protected
+     * @return array 
+     */
+    public function getElementsAndSubFormsOrdered()
+    {
+        $ordered = array();
+        foreach ($this->_order as $name => $order) {
+            $order = isset($order) ? $order : count($ordered);
+            if ($this->$name instanceof Zend_Form_Element ||
+                $this->$name instanceof Zend_Form) {
+                array_splice($ordered, $order, 0, array($this->$name));
+            } else if ($this->$name instanceof Zend_Form_DisplayGroup) {
+                $subordered = array();
+                foreach ($this->$name->getElements() as $element) {
+                    $suborder = $element->getOrder();
+                    $suborder = (null !== $suborder) ? $suborder : count($subordered);
+                    array_splice($subordered, $suborder, 0, array($element));
+                }
+                if (!empty($subordered)) {
+                    array_splice($ordered, $order, 0, $subordered);
+                }
+            }
+        }
+        return $ordered;
+    }
+
+    /**
      * This is a helper function until php 5.3 is widespreaded 
      * 
      * @param array $into

+ 11 - 30
library/Zend/Form/Decorator/FormErrors.php

@@ -367,38 +367,19 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
     {
         $content = '';
 
-        $errors = $form->getMessages();
-        
-        if ($form->isArray()) {
-            $name = $form->getElementsBelongTo();
-            $path = trim(strtr((string)$name, array('[' => '/', ']' => '')), '/');
-            $segs = ('' !== $path) ? explode('/', $path) : array();
-            foreach ($segs as $seg) {
-                if (!array_key_exists($seg, (array)$errors)) {
-                    return '';
+        foreach ($form->getElementsAndSubFormsOrdered() as $subitem) {
+            if ($subitem instanceof Zend_Form_Element) {
+                $messages = $subitem->getMessages();
+                if (count($messages)) {
+                    $subitem->setView($view);
+                    $content .= $this->getMarkupListItemStart()
+                             .  $this->renderLabel($subitem, $view)
+                             .  $view->formErrors($messages, $this->getOptions())
+                             .  $this->getMarkupListItemEnd();
                 }
-                $errors = $errors[$seg];
-            }
-        } else if ($form instanceof Zend_Form_SubForm) {
-            if ((1 == count($errors)) && array_key_exists($name, $errors)) {
-                $errors = $errors[$name];
-            }
-        }
-        if (empty($errors)) {
-            return $content;
-        }
-
-        foreach ($errors as $name => $list) {
-            $element = $form->$name;
-            if ($element instanceof Zend_Form_Element) {
-                $element->setView($view);
-                $content .= $this->getMarkupListItemStart()
-                         .  $this->renderLabel($element, $view)
-                         .  $view->formErrors($list, $this->getOptions())
-                         .  $this->getMarkupListItemEnd();
-            } elseif (!$this->ignoreSubForms() && ($element instanceof Zend_Form)) {
+            } else if (!$this->ignoreSubForms()) {
                 $content .= $this->getMarkupListStart()
-                          . $this->_recurseForm($element, $view)
+                          . $this->_recurseForm($subitem, $view)
                           . $this->getMarkupListEnd();
             }
         }