ZF-10103: Zend_Validate_Float validates the int 7 as a float

Description

Windows XP running as localhost Apache/2.0.59 (Win32) PHP/5.2.8 DAV/2

Class: Zend_Validate_Float

if ( Zend_validate::is( 7, 'float' ) ) {
  echo 'Validated';
} else echo 'NOT Validated';

Result = Validated

Note: is_float() correctly shows as invalid.

Comments

7 equals 7.00 and therefore it imho makes sense that it is validated.

The value 7 can be used as float.

It can be degraded to the decimal 7 with the precision 0 which makes it a perfect float.

Note that Zend_Validate_Float and Zend_Validate_Int do not check if the type is float or int, but if the contained value is a float or integer value representation.

Therefor closing as non-issue.

Thanks for replying Thomas, I am new to Zend Framework ( if not Zend and PHP5 ) and let me say as a disclaimer that I think it is great.

I find your answer "interesting" however. ( and the fact that you graciously tag is as a "non issue" ).

$vals = array( 1, 11, 2566, '34', 0.07, 1.01, 77.777 );
foreach( $vals as $index => $val ) {
  if ( Zend_Validate::is( $val, 'float' ) ) {
    echo $val . ' is a valid float<br />';
  }
}

Every single one is "it states" a valid float .. whether it is an int, a string or a float ( double ) with a floating point, where's the value? it does no more than is_numeric() ( with a hell of a lot more confusion and overhead ). It is therefore in my opinion misleading if not just substandard to the already existing is_float().

If not a bug then surely it's at least irrelevant as a validator.

It does have value; after all "foobar" definitely will be marked as invalid ;)

The fact that this validator doesn't work the way you want it to, you probably need another validator (which you may or may not have to write yourself). It doesn't mean this validator "doesn't work"

Again and replying the manual:

Zend_Validate_Float does not check the type of the given variable. It checks if the content is a floating value, or otherwise said: If the content of the variable can be used as float.

Example: Your user enters "34" into your form. You want to save it into your database but the field is defined as float. By using Zend_Validate_Float this would work.

But your expectation is that any input is denied when it's not a float type. A value of "1,234.56" is also a valid float. It uses a localized representation.

is_float only checks the type of a variable, not the content.

LOL @ foobar! :)

Yeah I'm perfectly capable of adding a simple validator. I posted here only because I saw it as a bug, and I like to help where I can where I have been given freebies, especially freebies of the quality of Zend Framework.

I will no doubt see you Thomas at my next bug which will probably be PostCodes :)

Thanks again for taking the time to reply and discuss.

Hi Thomas and sorry that I misread the last post I was responding to Dolf not yourself.

I do see the complexity of locale and why therefore is_float() is not up to the task. Which I confess I hadn't seen before your reply .. thanks.

Equally though I can see Zend_Validate_Float being used incorrectly, perhaps a "locale_aware" flag?

In your forum example, in my opinion "34" should NOT work if I am expecting a float .. it is user input and should be type cast ... usually one would (float)$forum_value .. should we accept it verbatim?

But before doing a typecast you need to check if the input can be typecast to a float... otherwise it would throw an exception or an invalid output. Therefor you can use Zend_Validate_Float.

And you can do "(float) 34"... it can be typecasted to a float... therefor Zend_Validate_Float return true for "34".

Maybe Thomas, but to take your example in a locale aware environment.

en_GB

1,234.56 is validated and when being inserted into a MySQL table with TYPE FLOAT it will fail with a "#1265 - Data truncated"

When you want to work with localized input you have to be aware of localization.

When your database uses another locale than your user you MUST OF COURSE use a filter to change the locale.

I did not know that things which are obvious have to be discussed. Your problem was that 7 is accepted as float and not that your database uses another locale than your user.

Please keep on the issue.

I will not add further discussions to this topic.

Correct fix version as it has been deleted unintentionally by another user