Issues

ZF-3174: Zend_Validate_StringLength fails when iconv internal encoding differs from input character set

Issue Type: Improvement Created: 2008-04-25T06:21:51.000+0000 Last Updated: 2008-12-15T12:01:21.000+0000 Status: Resolved Fix version(s): - 1.7.3 (19/Jan/09)

Reporter: C Snover (snover) Assignee: Thomas Weidner (thomas) Tags: - Zend_Validate

Related issues: Attachments:

Description

ZF-1913 broke Zend_Validate_StringLength in instances when PHP's iconv.internal_encoding setting differs from the character set of the data that's being validated.

The simple fix is to add a new character set option that defaults to UTF-8:

<pre class="highlight">
Index: library/Zend/Validate/StringLength.php
===================================================================
--- library/Zend/Validate/StringLength.php      (revision 9303)
+++ library/Zend/Validate/StringLength.php      (working copy)
@@ -54,7 +54,7 @@
         'min' => '_min',
         'max' => '_max'
     );
-
+
     /**
      * Minimum length
      *
@@ -70,18 +70,27 @@
      * @var integer|null
      */
     protected $_max;
+
+    /**
+     * String character set
+     *
+     * @var string
+     */
+    protected $_charset;

     /**
      * Sets validator options
      *
      * @param  integer $min
      * @param  integer $max
+     * @param  string  $charset
      * @return void
      */
-    public function __construct($min = 0, $max = null)
+    public function __construct($min = 0, $max = null, $charset = 'utf-8')
     {
         $this->setMin($min);
         $this->setMax($max);
+        $this->setCharset($charset);
     }

     /**
@@ -149,6 +158,28 @@

         return $this;
     }
+
+    /**
+     * Returns the character set
+     *
+     * @return string
+     */
+    public function getCharset()
+    {
+        return $this->_charset;
+    }
+
+    /**
+     * Sets the character set
+     *
+     * @param string $charset
+     * @return Zend_Validate_StringLength Provides a fluent interface
+     */
+    public function setCharset($charset)
+    {
+        $this->_charset = $charset;
+        return $this;
+    }

     /**
      * Defined by Zend_Validate_Interface
@@ -163,7 +194,7 @@
     {
         $valueString = (string) $value;
         $this->_setValue($valueString);
-        $length = iconv_strlen($valueString);
+        $length = iconv_strlen($valueString, $this->_charset);
         if ($length < $this->_min) {
             $this->_error(self::TOO_SHORT);
         }

Comments

Posted by Wil Sinclair (wil) on 2008-06-09T13:40:50.000+0000

Please evaluate and fix/categorize as necessary.

Posted by Thomas Weidner (thomas) on 2008-12-15T12:01:21.000+0000

Duplication of ZF-3015. New feature implemented with r13278

Have you found an issue?

See the Overview section for more details.

Copyright

© 2006-2016 by Zend, a Rogue Wave Company. Made with by awesome contributors.

This website is built using zend-expressive and it runs on PHP 7.

Contacts