ZF-3174: Zend_Validate_StringLength fails when iconv internal encoding differs from input character set
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:
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