Zend Framework

Zend_Validate_Int validates floats?

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Minor Minor
  • Resolution: Fixed
  • Affects Version/s: 1.9.7
  • Fix Version/s: 1.10.0
  • Component/s: Zend_Validate
  • Labels:
    None

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'));

Activity

Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Thomas Weidner added a comment -

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

Show
Thomas Weidner added a comment - Closing as fixed for next release. Probably already fixed within the last 7 months.
Hide
Ota Mares added a comment -

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'));
Show
Ota Mares added a comment - 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'));
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Ota Mares added a comment -

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.

Show
Ota Mares added a comment - 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.
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.
Hide
Ota Mares added a comment - - edited

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?

Show
Ota Mares added a comment - - edited 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?
Hide
Ota Mares added a comment -

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/test.php

Show
Ota Mares added a comment - 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/test.php
Hide
Thomas Weidner added a comment -

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?

Show
Thomas Weidner added a comment - 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?
Hide
Ota Mares added a comment -

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.

Show
Ota Mares added a comment - 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.
Hide
Thomas Weidner added a comment -

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.

Show
Thomas Weidner added a comment - 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.

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: