Explorar el Código

[ZF-6186] Zend_Validate:

 - new feature to limit the returned error message size

git-svn-id: http://framework.zend.com/svn/framework/standard/trunk@16170 44c647ce-9c0f-0410-b52a-842ac1e357ba
thomas hace 16 años
padre
commit
ec6d967c7d

+ 36 - 0
documentation/manual/en/module_specs/Zend_Validate-Messages.xml

@@ -574,6 +574,42 @@ $validator = new Zend_Validate_Alnum();
 $messages  = $validator->getMessageTemplates();
 ]]></programlisting>
 
+    <sect2 id="zend.validate.messages">
+        <title>Limit the size of a validation message</title>
+
+        <para>
+            Sometimes it is necessary to limit the maximum size a validation message can have.
+            For example when your view allows a maximum size of 100 chars to be rendered on one
+            line. To simplify the usage, <classname>Zend_Validate</classname> is able to
+            automatically limit the maximum returned size of a validation message.
+        </para>
+
+        <para>
+            To get the actual set size use
+            <methodname>Zend_Validate::getMessageLength()</methodname>. If it is -1, then the
+            returned message will not be truncated. This is default behaviour.
+        </para>
+
+        <para>
+            To limit the returned message size use
+            <methodname>Zend_Validate::setMessageLength()</methodname>. Set it to any integer size
+            you need. When the returned message exceeds the set size, then the message
+            will be truncated and the string '<emphasis>...</emphasis>' will be added instead of
+            the rest of the message.
+        </para>
+
+        <programlisting language="php"><![CDATA[
+Zend_Validate::setMessageLength(100);
+]]></programlisting>
+
+        <note>
+            <para>
+                Note that the set message length is used for all validators, even for self defined ones
+                as long as they extend <classname>Zend_Validate_Abstract</classname>.
+            </para>
+        </note>
+    </sect2>
+
 </sect1>
 <!--
 vim:se ts=4 sw=4 et:

+ 33 - 0
library/Zend/Validate/Abstract.php

@@ -94,6 +94,13 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
     protected $_translatorDisabled = false;
 
     /**
+     * Limits the maximum returned length of a error message
+     *
+     * @var Integer
+     */
+    protected static $_messageLength = -1;
+
+    /**
      * Returns array of validation failure messages
      *
      * @return array
@@ -229,6 +236,12 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
         foreach ($this->_messageVariables as $ident => $property) {
             $message = str_replace("%$ident%", (string) $this->$property, $message);
         }
+
+        $length = self::getMessageLength();
+        if (($length > -1) && (strlen($message) > $length)) {
+            $message = substr($message, 0, (self::getMessageLength() - 3)) . '...';
+        }
+
         return $message;
     }
 
@@ -395,4 +408,24 @@ abstract class Zend_Validate_Abstract implements Zend_Validate_Interface
     {
         return $this->_translatorDisabled;
     }
+
+    /**
+     * Returns the maximum allowed message length
+     *
+     * @return integer
+     */
+    public static function getMessageLength()
+    {
+        return self::$_messageLength;
+    }
+
+    /**
+     * Sets the maximum allowed message length
+     *
+     * @param integer $length
+     */
+    public static function setMessageLength($length = -1)
+    {
+        self::$_messageLength = $length;
+    }
 }

+ 19 - 0
tests/Zend/Validate/AbstractTest.php

@@ -258,6 +258,25 @@ class Zend_Validate_AbstractTest extends PHPUnit_Framework_TestCase
                 Zend_Validate_AbstractTest_Concrete::FOO_MESSAGE => '%value% was passed'), $messages);
     }
 
+    public function testMaximumErrorMessageLength()
+    {
+        require_once 'Zend/Validate.php';
+        $this->assertEquals(-1, Zend_Validate::getMessageLength());
+        Zend_Validate::setMessageLength(10);
+        $this->assertEquals(10, Zend_Validate::getMessageLength());
+
+        $translator = new Zend_Translate(
+            'array',
+            array('fooMessage' => 'This is the translated message for %value%'),
+            'en'
+        );
+        $this->validator->setTranslator($translator);
+        $this->assertFalse($this->validator->isValid('bar'));
+        $messages = $this->validator->getMessages();
+        $this->assertTrue(array_key_exists('fooMessage', $messages));
+        $this->assertEquals('This is...', $messages['fooMessage']);
+    }
+
     /**
      * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
      *