Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: validation change behavior regex match in integer and numeric to php function #6490

Closed
wants to merge 11 commits into from
9 changes: 5 additions & 4 deletions system/Validation/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,12 @@ public function hex(?string $str = null): bool

/**
* Integer
*
* @param int|string|null $str
*/
public function integer(?string $str = null): bool
public function integer($str = null): bool
{
return (bool) preg_match('/\A[\-+]?\d+\z/', $str ?? '');
return is_int($str);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All data sent via HTTP is represented as strings and arrays. That is, an integer as a string will not pass validation.

}

/**
Expand All @@ -155,8 +157,7 @@ public function is_natural_no_zero(?string $str = null): bool
*/
public function numeric(?string $str = null): bool
{
// @see https://regex101.com/r/bb9wtr/2
return (bool) preg_match('/\A[\-+]?\d*\.?\d+\z/', $str ?? '');
return is_numeric($str);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/system/CodeIgniterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function testPageCacheWithCacheQueryString($cacheQueryStringValue, int $e
$routes->add($testingUrl, static function () {
CodeIgniter::cache(0); // Dont cache the page in the run() function because CodeIgniter class will create default $cacheConfig and overwrite settings from the dataProvider
$response = Services::response();
$string = 'This is a test page, to check cache configuration';
$string = 'This is a test page, to check cache configuration';

return $response->setBody($string);
});
Expand Down
1 change: 0 additions & 1 deletion user_guide_src/source/libraries/validation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,6 @@ It works for most basic cases like validating POST data.

However, for example, if you use JSON input data, it may be a type of bool/null/array.
When you validate the boolean ``true``, it is converted to string ``'1'`` with the Traditional rule classes.
If you validate it with the ``integer`` rule, ``'1'`` passes the validation.

The **Strict Rules** don't use implicit type conversion.

Expand Down