mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Merge pull request #261 from rauwebieten/master
ipv4, ipv6 and ascii rules
This commit is contained in:
commit
14702f00d3
@ -133,12 +133,15 @@ V::lang('ar');
|
||||
* `in` - Performs in_array check on given array values
|
||||
* `notIn` - Negation of `in` rule (not in array of values)
|
||||
* `ip` - Valid IP address
|
||||
* `ipv4` - Valid IP v4 address
|
||||
* `ipv6` - Valid IP v6 address
|
||||
* `email` - Valid email address
|
||||
* `emailDNS` - Valid email address with active DNS record
|
||||
* `url` - Valid URL
|
||||
* `urlActive` - Valid URL with active DNS record
|
||||
* `alpha` - Alphabetic characters only
|
||||
* `alphaNum` - Alphabetic and numeric characters only
|
||||
* `ascii` - ASCII characters only
|
||||
* `slug` - URL slug characters (a-z, 0-9, -, \_)
|
||||
* `regex` - Field matches given regex pattern
|
||||
* `date` - Field is a valid date
|
||||
|
||||
@ -493,6 +493,30 @@ class Validator
|
||||
return filter_var($value, \FILTER_VALIDATE_IP) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid IP v4 address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIpv4($field, $value)
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV4) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid IP v6 address
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateIpv6($field, $value)
|
||||
{
|
||||
return filter_var($value, \FILTER_VALIDATE_IP, \FILTER_FLAG_IPV6) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address
|
||||
*
|
||||
@ -505,6 +529,24 @@ class Validator
|
||||
return filter_var($value, \FILTER_VALIDATE_EMAIL) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field contains only ASCII characters
|
||||
*
|
||||
* @param $field
|
||||
* @param $value
|
||||
* @return bool|false|string
|
||||
*/
|
||||
protected function validateAscii($field, $value)
|
||||
{
|
||||
// multibyte extension needed
|
||||
if (function_exists('mb_detect_encoding')) {
|
||||
return mb_detect_encoding($value, 'ASCII', true);
|
||||
}
|
||||
|
||||
// fallback with regex
|
||||
return 0 === preg_match('/[^\x00-\x7F]/', $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid e-mail address and the domain name is active
|
||||
*
|
||||
|
||||
@ -621,6 +621,20 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testAsciiValid()
|
||||
{
|
||||
$v = new Validator(array('text' => '12345 abcde'));
|
||||
$v->rule('ascii', 'text');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testAsciiInvalid()
|
||||
{
|
||||
$v = new Validator(array('text' => '12345 abcdé'));
|
||||
$v->rule('ascii', 'text');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testIpValid()
|
||||
{
|
||||
$v = new Validator(array('ip' => '127.0.0.1'));
|
||||
@ -635,6 +649,34 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testIpv4Valid()
|
||||
{
|
||||
$v = new Validator(array('ip' => '127.0.0.1'));
|
||||
$v->rule('ipv4', 'ip');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testIpv4Invalid()
|
||||
{
|
||||
$v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329'));
|
||||
$v->rule('ipv4', 'ip');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testIpv6Valid()
|
||||
{
|
||||
$v = new Validator(array('ip' => 'FE80::0202:B3FF:FE1E:8329'));
|
||||
$v->rule('ipv6', 'ip');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testIpv6Invalid()
|
||||
{
|
||||
$v = new Validator(array('ip' => '127.0.0.1'));
|
||||
$v->rule('ipv6', 'ip');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testEmailValid()
|
||||
{
|
||||
$v = new Validator(array('name' => 'Chester Tester', 'email' => 'chester@tester.com'));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user