From 7546be63948d658e33f6d9017172cbb9c8395a4b Mon Sep 17 00:00:00 2001 From: Jelle Henkens Date: Mon, 21 May 2012 13:39:31 +0100 Subject: [PATCH 1/3] Adding a validation rule for checking natural numbers --- lib/Cake/Test/Case/Utility/ValidationTest.php | 20 +++++++++++++++++++ lib/Cake/Utility/Validation.php | 13 ++++++++++++ 2 files changed, 33 insertions(+) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index 9bd7af111..bed0f438b 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1960,6 +1960,26 @@ class ValidationTest extends CakeTestCase { $this->assertTrue(Validation::numeric('2.2')); } +/** + * testNaturalNumber method + * + * @return void + */ + public function testNaturalNumber() { + $this->assertFalse(Validation::naturalNumber('teststring')); + $this->assertFalse(Validation::naturalNumber('5.4')); + $this->assertFalse(Validation::naturalNumber(99.004)); + $this->assertFalse(Validation::naturalNumber('0,05')); + $this->assertFalse(Validation::naturalNumber('-2')); + $this->assertFalse(Validation::naturalNumber(-2)); + $this->assertFalse(Validation::naturalNumber('0')); + + $this->assertTrue(Validation::naturalNumber('2')); + $this->assertTrue(Validation::naturalNumber(49)); + $this->assertTrue(Validation::naturalNumber('0', true)); + $this->assertTrue(Validation::naturalNumber(0, true)); + } + /** * testPhone method * diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index 66300448e..e13145551 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -563,6 +563,19 @@ class Validation { return is_numeric($check); } +/** + * Checks if a value is a natural value. + * + * @param string $check Value to check + * @param boolean $allowZero Set true to allow zero, defaults to false + * @return boolean Success + * @see http://en.wikipedia.org/wiki/Natural_number + */ + public static function naturalNumber($check, $allowZero = false) { + $regex = $allowZero ? '/^(?:0|[1-9][0-9]*)$/' : '/^[1-9][0-9]*$/'; + return self::_check($check, $regex); + } + /** * Check that a value is a valid phone number. * From 7d080ea2d1e74ba869a8bbd17344cb1fde892834 Mon Sep 17 00:00:00 2001 From: Jelle Henkens Date: Tue, 22 May 2012 16:32:16 +0100 Subject: [PATCH 2/3] Adding an assert to check for zero prefixed numbers --- lib/Cake/Test/Case/Utility/ValidationTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/Cake/Test/Case/Utility/ValidationTest.php b/lib/Cake/Test/Case/Utility/ValidationTest.php index bed0f438b..90de95ca4 100644 --- a/lib/Cake/Test/Case/Utility/ValidationTest.php +++ b/lib/Cake/Test/Case/Utility/ValidationTest.php @@ -1973,6 +1973,7 @@ class ValidationTest extends CakeTestCase { $this->assertFalse(Validation::naturalNumber('-2')); $this->assertFalse(Validation::naturalNumber(-2)); $this->assertFalse(Validation::naturalNumber('0')); + $this->assertFalse(Validation::naturalNumber('050')); $this->assertTrue(Validation::naturalNumber('2')); $this->assertTrue(Validation::naturalNumber(49)); From f6a6e3381e364d60cef6895ba502ca8ec0e5b86e Mon Sep 17 00:00:00 2001 From: Jelle Henkens Date: Thu, 24 May 2012 20:07:48 +0100 Subject: [PATCH 3/3] Fixing typo in the docblock --- lib/Cake/Utility/Validation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Cake/Utility/Validation.php b/lib/Cake/Utility/Validation.php index e13145551..e7885fe4c 100644 --- a/lib/Cake/Utility/Validation.php +++ b/lib/Cake/Utility/Validation.php @@ -564,7 +564,7 @@ class Validation { } /** - * Checks if a value is a natural value. + * Checks if a value is a natural number. * * @param string $check Value to check * @param boolean $allowZero Set true to allow zero, defaults to false