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

add new validation for validate only integer type #329

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions src/Valitron/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
18 changes: 18 additions & 0 deletions tests/Valitron/ValidateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
Expand Down