mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 11:28:25 +00:00
Adding a validation rule for checking natural numbers
This commit is contained in:
parent
4fd45c07f5
commit
7546be6394
2 changed files with 33 additions and 0 deletions
|
@ -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
|
||||
*
|
||||
|
|
|
@ -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.
|
||||
*
|
||||
|
|
Loading…
Reference in a new issue