Zend Framework

Zend_Filter::isDigits() operates erratically

Details

  • Type: Bug Bug
  • Status: Resolved Resolved
  • Priority: Major Major
  • Resolution: Fixed
  • Affects Version/s: 0.1.3, 0.1.4, 0.1.5
  • Fix Version/s: 0.2.0
  • Component/s: Zend_Filter
  • Labels:
    None

Description

Zend_Filter::isDigits() sometimes returns false on values that are digits. The solution is simple:

public static function isDigits($value)
{
---    return ctype_digit($value);
+++    return ctype_digit((string) $value);
}

I marked this major because I use this for all ID validation in my applications.

Issue Links

Activity

Hide
Mark Evans added a comment -

Can you give some examples where it fails?

Show
Mark Evans added a comment - Can you give some examples where it fails?
Hide
Matthew Ratzloff added a comment -

Fatal error: Uncaught exception 'Exception' with message 'Expected second ID' in /path/to/ExampleController.php:312
Stack trace:
#0 /path/to/ExampleController.php(184): ExampleController->doSomething('7', 19)[...]

public function doSomething($firstId, $secondId)
{
    if(!Zend_Filter::isDigits($firstId))
    {
        throw new Exception("Expected first ID");
    }

    if(!Zend_Filter::isDigits($secondId))
    {
        throw new Exception("Expected second ID");
    }
}

Changing Zend_Filter::isDigits() as indicated results in correct execution. See http://us2.php.net/manual/en/function.ctype-digit.php.

You might also change to

return preg_match("/^\d+$/", $value);

which is what I went ahead and did.

Show
Matthew Ratzloff added a comment -
Fatal error: Uncaught exception 'Exception' with message 'Expected second ID' in /path/to/ExampleController.php:312 Stack trace: #0 /path/to/ExampleController.php(184): ExampleController->doSomething('7', 19)[...]
public function doSomething($firstId, $secondId)
{
    if(!Zend_Filter::isDigits($firstId))
    {
        throw new Exception("Expected first ID");
    }

    if(!Zend_Filter::isDigits($secondId))
    {
        throw new Exception("Expected second ID");
    }
}
Changing Zend_Filter::isDigits() as indicated results in correct execution. See http://us2.php.net/manual/en/function.ctype-digit.php. You might also change to
return preg_match("/^\d+$/", $value);
which is what I went ahead and did.
Hide
Matthew Ratzloff added a comment -

I meant to say see the last two comments of http://us2.php.net/manual/en/function.ctype-digit.php

So much for dynamic typing. A bug in PHP?

Show
Matthew Ratzloff added a comment - I meant to say see the last two comments of http://us2.php.net/manual/en/function.ctype-digit.php So much for dynamic typing. A bug in PHP?

People

Vote (0)
Watch (0)

Dates

  • Created:
    Updated:
    Resolved: