Issues

ZF-9293: Wrong behavior in Zend_Validate_Int and Zend_Validate_Float when locale is present.

Description

When locale is present, isValid() shouldn't test using 'en' locale also. A example of this inconsistency is:


//consider Zend_Locale is already in Zend_Registry and it's 'pt_BR'
$number = $_POST['VALUE_IN_BRL'] // Value must be in Brazil's currency format, but let's suppose user typed 2,500.99 ('en' format)
$validate = new Zend_Validate_Float();
if(is_valid($number)){
   //get number from localized string and save it to database
   $number = Zend_Locale_Format::getNumber($number);
}
else{
   $msg = 'Enter the correct value!'
}

In addition to wrong behavior (2,500.99 is not valid in my application), i get this error:

Uncaught exception 'Zend_Locale_Exception' with message 'No localized value in 2,500.99 found, or the given number does not match the localized format' in path_to_zf\library\Zend\Locale\Format.php:247

Comments


$number = '1.5';
//$number   = '1,5'; // <- correct value for de_DE

$locale = new Zend_Locale('de_DE');
$validate = new Zend_Validate_Float($locale);

if($validate->isValid($number)){
   echo Zend_Locale_Format::getNumber($number, array('locale' => $locale));
   // Throws Exception: No localized value in 1.5 found, or the given number does not match the localized format
}
else{
   echo 'Wrong number or format';
}

One of my co-workers noticed me about the above mentioned code thats not working. Its still checking against the "en" locale, instaed of checking against the given de_DE locale.

I think that it's not necessary to check against the english locale if another locale (in this case de_DE) is given?!

Additionally to my comment above:

As we can see, the Float Validator always checks for the english locale.


<?php
class Zend_Validate_Float extends Zend_Validate_Abstract
{
//..
    public function isValid($value)
    {
//..
                if (!Zend_Locale_Format::isFloat($value, array('locale' => 'en')) && // <- not needed if $this->_locale defined???
                    !Zend_Locale_Format::isFloat($value, array('locale' => $this->_locale))) {
                    $this->_error(self::NOT_FLOAT);
                    return false;
                }
//..
    }
}

Exactly Benjamin! Same with Zend_Validate_Int. Another example why isValid() shouldn't test against 'en' could be in a financial test, when students must type the number format of many countries and the application must validate this.

Reducing from critical to major. The inclusion of EN was forced by the core team despite my negative response to this request. Has to be discussed with team members before changing this in my eyes errorous behaviour.

Not reproducable for Zend_Validate_Int. There is no relation to "en" within the Int validator.

Closing as not reproduceable

Wether Int nor Float have a fixed "en" locale as mentioned. Both validators do not validate localized "en" numbers when another locale is given within or after the mentioned release