Skip to content

Commit

Permalink
Merge pull request #16545 from Stricted/ip_validation
Browse files Browse the repository at this point in the history
[5.4] Add ipv4 and ipv6 validations
  • Loading branch information
taylorotwell authored Nov 25, 2016
2 parents 4d27029 + b81cfae commit 3e03a2a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -1551,6 +1551,30 @@ protected function validateIp($attribute, $value)
return filter_var($value, FILTER_VALIDATE_IP) !== false;
}

/**
* Validate that an attribute is a valid IPv4.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateIpv4($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false;
}

/**
* Validate that an attribute is a valid IPv6.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
protected function validateIpv6($attribute, $value)
{
return filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false;
}

/**
* Validate that an attribute is a valid e-mail address.
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1505,6 +1505,18 @@ public function testValidateIp()

$v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ip']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv4']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv6']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['ip' => '127.0.0.1'], ['ip' => 'Ipv6']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['ip' => '::1'], ['ip' => 'Ipv4']);
$this->assertTrue($v->fails());
}

public function testValidateEmail()
Expand Down

0 comments on commit 3e03a2a

Please sign in to comment.