ソースを参照

Similar error messages for a multi element are not duplicated

See http://framework.zend.com/issues/browse/ZF-11667
Martin Hujer 12 年 前
コミット
f98b49f319
2 ファイル変更69 行追加1 行削除
  1. 35 1
      library/Zend/Form/Element.php
  2. 34 0
      tests/Zend/Form/Element/MultiselectTest.php

+ 35 - 1
library/Zend/Form/Element.php

@@ -227,6 +227,13 @@ class Zend_Form_Element implements Zend_Validate_Interface
     protected $_isPartialRendering = false;
 
     /**
+     * Use one error message for array elements with concatenated values
+     *
+     * @var bool
+     */
+    protected $_concatJustValuesInErrorMessage = false;
+
+    /**
      * Constructor
      *
      * $spec may be:
@@ -915,6 +922,28 @@ class Zend_Form_Element implements Zend_Validate_Interface
     }
 
     /**
+     * Use one error message for array elements with concatenated values
+     *
+     * @param boolean $concatJustValuesInErrorMessage
+     * @return Zend_Form_Element
+     */
+    public function setConcatJustValuesInErrorMessage($concatJustValuesInErrorMessage)
+    {
+        $this->_concatJustValuesInErrorMessage = $concatJustValuesInErrorMessage;
+        return $this;
+    }
+
+    /**
+     * Use one error message for array elements with concatenated values
+     *
+     * @return boolean
+     */
+    public function getConcatJustValuesInErrorMessage()
+    {
+        return $this->_concatJustValuesInErrorMessage;
+    }
+
+    /**
      * Overloading: retrieve object property
      *
      * Prevents access to properties beginning with '_'.
@@ -2248,7 +2277,12 @@ class Zend_Form_Element implements Zend_Validate_Interface
                     $aggregateMessages[] = str_replace('%value%', $val, $message);
                 }
                 if (count($aggregateMessages)) {
-                    $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
+                    if ($this->_concatJustValuesInErrorMessage) {
+                        $values = implode($this->getErrorMessageSeparator(), $value);
+                        $messages[$key] = str_replace('%value%', $values, $message);
+                    } else {
+                        $messages[$key] = implode($this->getErrorMessageSeparator(), $aggregateMessages);
+                    }
                 }
             } else {
                 $messages[$key] = str_replace('%value%', $value, $message);

+ 34 - 0
tests/Zend/Form/Element/MultiselectTest.php

@@ -53,6 +53,11 @@ class Zend_Form_Element_MultiselectTest extends PHPUnit_Framework_TestCase
     }
 
     /**
+     * @var Zend_Form_Element_Multiselect
+     */
+    public $element;
+
+    /**
      * Sets up the fixture, for example, open a network connection.
      * This method is called before a test is executed.
      *
@@ -342,6 +347,35 @@ class Zend_Form_Element_MultiselectTest extends PHPUnit_Framework_TestCase
             $this->markTestIncomplete('Error occurs for PHP 5.1.4 on Windows');
         }
     }
+
+    /**
+     * @group ZF-11667
+     */
+    public function testSimilarErrorMessagesForMultiElementAreNotDuplicated()
+    {
+        $this->element->setConcatJustValuesInErrorMessage(true);
+
+        // create element with 4 checkboxes
+        $this->element->setMultiOptions(array(
+            'multiOptions' => array(
+                array('key' => 'a', 'value' => 'A'),
+                array('key' => 'b', 'value' => 'B'),
+                array('key' => 'c', 'value' => 'C'),
+                array('key' => 'd', 'value' => 'D'),
+            )
+        ));
+
+        // check 3 of them
+        $this->element->setValue(array('A', 'B', 'D'));
+
+        // later on, fails some validation on submit
+        $this->element->addError('some error! %value%');
+
+        $this->assertEquals(
+            array('some error! A; B; D'),
+            $this->element->getMessages()
+        );
+    }
 }
 
 // Call Zend_Form_Element_MultiselectTest::main() if this source file is executed directly.