Browse Source

ZF-5385: Make FormErrors handle custom form ErrorMessages

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@22272 44c647ce-9c0f-0410-b52a-842ac1e357ba
alab 15 years ago
parent
commit
94abd7fff3
2 changed files with 81 additions and 2 deletions
  1. 11 0
      library/Zend/Form.php
  2. 70 2
      library/Zend/Form/Decorator/FormErrors.php

+ 11 - 0
library/Zend/Form.php

@@ -2550,6 +2550,17 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
         return $messages;
     }
 
+    /**
+     * Retrieve translated custom error messages
+     * Proxies to {@link _getErrorMessages()}.
+     * 
+     * @return array
+     */
+    public function getCustomMessages()
+    {
+        return $this->_getErrorMessages();
+    }
+
 
     // Rendering
 

+ 70 - 2
library/Zend/Form/Decorator/FormErrors.php

@@ -44,6 +44,8 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
      */
     protected $_defaults = array(
         'ignoreSubForms'          => false,
+        'showCustomFormErrors'    => true,
+        'onlyCustomFormErrors'    => false,
         'markupElementLabelEnd'   => '</b>',
         'markupElementLabelStart' => '<b>',
         'markupListEnd'           => '</ul>',
@@ -57,6 +59,8 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
      * @var string
      */
     protected $_ignoreSubForms;
+    protected $_showCustomFormErrors;
+    protected $_onlyCustomFormErrors;
     protected $_markupElementLabelEnd;
     protected $_markupElementLabelStart;
     protected $_markupListEnd;
@@ -338,6 +342,65 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
     }
 
     /**
+     * Get showCustomFormErrors
+     * 
+     * @return bool
+     */
+    public function getShowCustomFormErrors()
+    {
+        if (null === $this->_showCustomFormErrors) {
+            if (null === ($how =  $this->getOption('showCustomFormErrors'))) {
+                $this->setShowCustomFormErrors($this->_defaults['showCustomFormErrors']);
+            } else {
+                $this->setShowCustomFormErrors($show);
+                $this->removeOption('showCustomFormErrors');
+            }
+        }
+    }
+
+    /**
+     * Set showCustomFormErrors
+     *
+     * @param  bool $showCustomFormErrors
+     * @return Zend_Form_Decorator_FormErrors
+     */
+    public function setShowCustomFormErrors($showCustomFormErrors)
+    {
+        $this->_showCustomFormErrors = (bool)$showCustomFormErrors;
+        return $this;
+    }
+
+    /**
+     * Get onlyCustomFormErrors
+     * 
+     * @return bool
+     */
+    public function getOnlyCustomFormErrors()
+    {
+        if (null === $this->_onlyCustomFormErrors) {
+            if (null === ($how =  $this->getOption('onlyCustomFormErrors'))) {
+                $this->setOnlyCustomFormErrors($this->_defaults['onlyCustomFormErrors']);
+            } else {
+                $this->setOnlyCustomFormErrors($show);
+                $this->removeOption('onlyCustomFormErrors');
+            }
+        }
+    }
+
+    /**
+     * Set onlyCustomFormErrors, whether to display elements messages
+     * in addition to custom form messages.
+     *
+     * @param  bool $onlyCustomFormErrors
+     * @return Zend_Form_Decorator_FormErrors
+     */
+    public function setOnlyCustomFormErrors($onlyCustomFormErrors)
+    {
+        $this->_onlyCustomFormErrors = (bool)$onlyCustomFormErrors;
+        return $this;
+    }
+
+    /**
      * Render element label
      *
      * @param  Zend_Form_Element $element
@@ -367,8 +430,14 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
     {
         $content = '';
 
+        $custom = $form->getCustomMessages();
+        if ($this->getShowCustomFormErrors() && count($custom)) {
+            $content .= $this->getMarkupListItemStart()
+                     .  $view->formErrors($custom, $this->getOptions())
+                     .  $this->getMarkupListItemEnd();
+        }
         foreach ($form->getElementsAndSubFormsOrdered() as $subitem) {
-            if ($subitem instanceof Zend_Form_Element) {
+            if ($subitem instanceof Zend_Form_Element && !$this->getOnlyCustomFormErrors()) {
                 $messages = $subitem->getMessages();
                 if (count($messages)) {
                     $subitem->setView($view);
@@ -383,7 +452,6 @@ class Zend_Form_Decorator_FormErrors extends Zend_Form_Decorator_Abstract
                           . $this->getMarkupListEnd();
             }
         }
-
         return $content;
     }
 }