mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Use an optional parameter to make validateInteger more strict
This commit is contained in:
parent
cb1ecb12e6
commit
28d954817b
@ -233,13 +233,19 @@ 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate the length of a string
|
||||
*
|
||||
|
||||
@ -116,19 +116,33 @@ class ValidateTest extends BaseTestCase
|
||||
$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()
|
||||
{
|
||||
$v = new Validator(array('num' => '42.341569'));
|
||||
$v->rule('integer', 'num');
|
||||
$this->assertFalse($v->validate());
|
||||
|
||||
$v = new Validator(array('num' => ' 41243'));
|
||||
$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' => '--1231'));
|
||||
$v->rule('integer', 'num');
|
||||
@ -142,7 +156,7 @@ class ValidateTest extends BaseTestCase
|
||||
public function testIntegerWithMaxValidation()
|
||||
{
|
||||
$v = new Validator(array('num' => ' 4212341569'));
|
||||
$v->rule('integer', 'num');
|
||||
$v->rule('integer', 'num', true);
|
||||
$v->rule('max', 'num', 1000);
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user