ZF-8868: Zend_Validate_Int validates floats?

Description

"The integers are formed by the natural numbers including 0 (0, 1, 2, 3, ...) together with the negatives of the non-zero natural numbers (−1, −2, −3, ...). Viewed as subset of the real numbers, they are numbers that can be written without a fractional or decimal component, and fall within the set {... −2, −1, 0, 1, 2, ...}. For example, 65, 7, and −756 are integers; 1.6 and 1½ are not integers." [http://en.wikipedia.org/wiki/Integer]

Basically its just a naming issue but in some cases it can lead to confusion why floats are accepted by Zend_Validate_Int as an int does consist only of natural numbers. Maybe there is a special reason or i am confusing something big time?, but a more logic name would be Zend_Validate_Number.

require_once 'Zend/Validate/Int.php
$foo = new Zend_Validate_Int();

zend_debug::dump($foo->isValid(1));
zend_debug::dump($foo->isValid(1.2));
zend_debug::dump($foo->isValid('1,2'));

Comments

Using your example on latest release I get: 1. true 2. false 3. false

In my eyes this is correct behaviour. I see no problems.

Closing as fixed for next release. Probably already fixed within the last 7 months.

Hi Thomas, i found the culprit. The validator is dependent on the specified locale and i think that shouldnt be? :)

Using following code will return true in all three cases:

<?php 
date_default_timezone_set('Europe/Berlin');
setlocale(LC_ALL, 'de_DE.utf8');
    
require_once 'Zend/Debug.php';
require_once 'Zend/Validate/Int.php';
$foo = new Zend_Validate_Int();

zend_debug::dump($foo->isValid(1));
zend_debug::dump($foo->isValid(1.2));
zend_debug::dump($foo->isValid('1,2'));

But the system relies on this locale. Also PHP relies on this locale.

Why should then Zend_Validate_Int not rely in this locale when you don't specify a locale at all?

Zend_Validate_Int then uses system-settings which also PHP uses. In my eyes this is correct behaviour.

There is only one difference... when you specified a Zend_Locale object within your registry, then this object will be used instead of system settings. But this is not given due to your example.

Because an integer is allways an integer and that is a natural number, independent of the locale. It doesnt matter if you are in brazil or germany, an integer is allways the same.

And thats why the validator is confusing, it does not validate if the number is an integer but a numerical value alltogether. Simply said, the validator does not do what its name implys.

How about renaming the validator? I am also confused about Zend_Validate_Digit, for me it seems that Zend_Validate_Int should be named Zend_Validate_Digit and the digit validator should be ditched alltogether.

I still don't see your problem...

As said also your second example returns true for the first, but false on the other two testcases.

I just redownloaded the ZF-1.9.7 minimal package to be sure. The testcode from the comments above returns true on all three zend_debug::dump cases. There is nothing else in the directory besides the zf and the test file.

I am trying to prove there is an issue, and i can reproduce it. I dont know what else todo.

Maybe some other users could test it on their systems?

Oh also i just tested it on my hosting webspace, same issue. You can view the "test" at following site:

http://ohne-ziel.de/Zend_Validate_Int_Test/…

When you say that this issue occurs with 1.9.7 then I also expect that you are using 1.9.7.

On the other hand, when I say that this issue is fixed with the next minor release (which is the upcoming 1.10) then I expect that you tested it with 1.10 when you say that the issue is still there.

Don't you think? ;-)

Duh, i interpreted your message as if it allready was fixed seven months ago and that it should work in the current 1.9.7 release.

Looking above at "Fix-Versions" you can see that it reads "1.10.0", which is the actual release. :-)

7 months was the time between the fix and the next minor release. Some API changes and additional have been done. Therefor it was not possible to backport the changes to 1.9.x.