mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #187 from notona/feature/fix-interger-validation
Add strict mode to validateInteger
This commit is contained in:
commit
b33c791162
@ -233,10 +233,16 @@ class Validator
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateInteger($field, $value)
|
||||
protected function validateInteger($field, $value, $params)
|
||||
{
|
||||
if (isset($params[0]) && (bool) $params[0]){
|
||||
//strict mode
|
||||
return preg_match('/^-?([0-9])+$/i', $value);
|
||||
}
|
||||
|
||||
return filter_var($value, \FILTER_VALIDATE_INT) !== false;
|
||||
}
|
||||
|
||||
|
||||
@ -110,6 +110,31 @@ class ValidateTest extends BaseTestCase
|
||||
$v = new Validator(array('num' => '41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '-41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testIntegerStrict(){
|
||||
|
||||
$v = new Validator(array('num' => ' 41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => ' 41243'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '+41243'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
|
||||
$v = new Validator(array('num' => '+41243'));
|
||||
$v->rule('integer', 'num', true);
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
}
|
||||
|
||||
public function testIntegerInvalid()
|
||||
@ -117,6 +142,15 @@ class ValidateTest extends BaseTestCase
|
||||
$v = new Validator(array('num' => '42.341569'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
|
||||
$v = new Validator(array('num' => '--1231'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => '0x3a'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testLengthValid()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user