Details
-
Type:
Bug
-
Status:
Resolved
-
Priority:
Major
-
Resolution: Fixed
-
Affects Version/s: 1.5.2
-
Fix Version/s: 1.8.0
-
Component/s: Zend_Validate
-
Labels:None
Description
First: Zend_Framework provides a powerful Zend_Locale. In my optionion Zend_Locale should be used with Zend_Validate_Float instead of localeconv().
Second: Zend_Validate_Float always failes when PHP is localized.
In method isValid(), the float value is first converted to an english value:
$valueFiltered = str_replace($locale['thousands_sep'], '', $valueString);
$valueFiltered = str_replace($locale['decimal_point'], '.', $valueFiltered);
So $valueFiltered always ends up with a value like "0.3" (also in localized environments).
But now the problem is that floatval is localized itself; floatval(0.3) will give 0,3 in a german environment. Finally, stringval(floatval(0.3)) gives 0,3. And exactly this string is compared to the original $valueFiltered:
if (strval(floatval($valueFiltered)) != $valueFiltered) {
So this if-statement will always fail.
I would recommend to use a class like this:
class Zend_Validate_Float extends Zend_Validate_Abstract
{
const NOT_FLOAT = 'notFloat';
protected $_messageTemplates = array(
self::NOT_FLOAT => "'%value%' does not appear to be a float"
);
public function isValid($value)
{
$valueString = (string) $value;
$this->_setValue($valueString);
$locale = Zend_Registry::get('Zend_Locale');
if(!$locale) { $locale = new Zend_Locale(); }
if(!Zend_Locale_Format::isFloat($value, array('locale' => $locale))) { $this->_error(); return false; }
return true;
}
}
Issue Links
| This issue duplicates: | ||||
| ZF-3423 | Validate_Float doesn't work with local than doesn't have a dot as decimal separator |
|
|
|
Duplicates
ZF-3423ZF-3423