From eb0db68d57a56b688ebaa8aa6bcba5f8c7411a4c Mon Sep 17 00:00:00 2001 From: "Agung N. Aprianto" Date: Sun, 31 Jan 2021 14:23:04 +0700 Subject: [PATCH 1/2] add new validation for validate only int type --- src/Valitron/Validator.php | 13 +++++++++++++ tests/Valitron/ValidateTest.php | 18 ++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 9b83cec..dc3ba79 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -269,6 +269,19 @@ protected function validateInteger($field, $value, $params) return filter_var($value, \FILTER_VALIDATE_INT) !== false; } + /** + * Validate that a field is an integer type + * + * @param string $field + * @param mixed $value + * @param array $params + * @return bool + */ + protected function validateIntegerType($field, $value, $params) + { + return is_int($value); + } + /** * Validate the length of a string * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d6e6dd3..6461bac 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -294,6 +294,24 @@ public function testNumericInvalidAltSyntax() $this->assertFalse($v->validate()); } + public function testIntegerTypeValid() + { + $v = new Validator(array('num' => 10)); + $v->rule('integerType', 'num'); + $this->assertTrue($v->validate()); + + $v = new Validator(array('num' => -10)); + $v->rule('integerType', 'num'); + $this->assertTrue($v->validate()); + } + + public function testIntegerTypeNotValid() + { + $v = new Validator(array('num' => '10')); + $v->rule('integerType', 'num'); + $this->assertFalse($v->validate()); + } + public function testIntegerValid() { $v = new Validator(array('num' => '41243')); From c6611165e49e11e133c5704ddefd95eb7f70f1f4 Mon Sep 17 00:00:00 2001 From: "Agung N. Aprianto" Date: Sun, 31 Jan 2021 14:32:59 +0700 Subject: [PATCH 2/2] update readme --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 0acca86..12598f5 100644 --- a/README.md +++ b/README.md @@ -188,6 +188,7 @@ $this->assertTrue($v->validate()); * `different` - Field must be different than another field * `accepted` - Checkbox or Radio must be accepted (yes, on, 1, true) * `numeric` - Must be numeric + * `integerType` - Must be integer type * `integer` - Must be integer number * `boolean` - Must be boolean * `array` - Must be array