浏览代码

ZF-5150: marking an element as an error should cause isValid() to fail

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@18189 44c647ce-9c0f-0410-b52a-842ac1e357ba
matthew 16 年之前
父节点
当前提交
bcd5284af8
共有 4 个文件被更改,包括 44 次插入1 次删除
  1. 14 1
      library/Zend/Form.php
  2. 12 0
      library/Zend/Form/Element.php
  3. 9 0
      tests/Zend/Form/ElementTest.php
  4. 9 0
      tests/Zend/Form/FormTest.php

+ 14 - 1
library/Zend/Form.php

@@ -134,6 +134,12 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
     protected $_errorsExist = false;
 
     /**
+     * Has the form been manually flagged as an error?
+     * @var bool
+     */
+    protected $_errorsForced = false;
+
+    /**
      * Form order
      * @var int|null
      */
@@ -2014,6 +2020,12 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
         }
 
         $this->_errorsExist = !$valid;
+
+        // If manually flagged as an error, return invalid status
+        if ($this->_errorsForced) {
+            return false;
+        }
+
         return $valid;
     }
 
@@ -2149,7 +2161,8 @@ class Zend_Form implements Iterator, Countable, Zend_Validate_Interface
      */
     public function markAsError()
     {
-        $this->_errorsExist = true;
+        $this->_errorsExist  = true;
+        $this->_errorsForced = true;
         return $this;
     }
 

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

@@ -122,6 +122,12 @@ class Zend_Form_Element implements Zend_Validate_Interface
     protected $_isError = false;
 
     /**
+     * Has the element been manually marked as invalid?
+     * @var bool
+     */
+    protected $_isErrorForced = false;
+
+    /**
      * Element label
      * @var string
      */
@@ -1339,6 +1345,11 @@ class Zend_Form_Element implements Zend_Validate_Interface
             }
         }
 
+        // If element manually flagged as invalid, return false
+        if ($this->_isErrorForced) {
+            return false;
+        }
+
         return $result;
     }
 
@@ -1416,6 +1427,7 @@ class Zend_Form_Element implements Zend_Validate_Interface
         } else {
             $this->_messages = $messages;
         }
+        $this->_isErrorForced = true;
         return $this;
     }
 

+ 9 - 0
tests/Zend/Form/ElementTest.php

@@ -1976,6 +1976,15 @@ class Zend_Form_ElementTest extends PHPUnit_Framework_TestCase
         $html = $this->element->bogusMethodCall();
     }
 
+    /**
+     * @group ZF-5150
+     */
+    public function testMarkingAsErrorShouldCauseIsErrorToReturnFalse()
+    {
+        $this->element->setValue('foo');
+        $this->element->markAsError();
+        $this->assertFalse($this->element->isValid('foo'));
+    }
 
     /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete

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

@@ -3728,6 +3728,15 @@ class Zend_Form_FormTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @group ZF-5150
+     */
+    public function testIsValidShouldFailIfAddErrorHasBeenCalled()
+    {
+        $this->form->addError('Error');
+        $this->assertFalse($this->form->isValid(array()));
+    }
+
+    /**
      * Used by test methods susceptible to ZF-2794, marks a test as incomplete
      *
      * @link   http://framework.zend.com/issues/browse/ZF-2794