mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
fixing some logic on the URL checking and adding new "contains" validator
This commit is contained in:
parent
650db517b4
commit
cb880e73b2
@ -23,6 +23,7 @@ class Validator
|
||||
|
||||
const ERROR_DEFAULT = 'Invalid';
|
||||
|
||||
protected $validUrlPrefixes = array('http://', 'https://', 'ftp://');
|
||||
|
||||
/**
|
||||
* Setup validation
|
||||
@ -241,6 +242,25 @@ class Validator
|
||||
return !$this->validateIn($field, $value, $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a field contains a given string
|
||||
*
|
||||
* @param string $field
|
||||
* @param mixed $value
|
||||
* @param array $params
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateContains($field, $value, $params)
|
||||
{
|
||||
if(!isset($params[0])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_string($params[0]) || !is_string($value)) {
|
||||
return false;
|
||||
}
|
||||
return (strpos($value, $params[0]) !== false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate that a field is a valid IP address
|
||||
*
|
||||
@ -274,7 +294,12 @@ class Validator
|
||||
*/
|
||||
protected function validateUrl($field, $value)
|
||||
{
|
||||
return filter_var($value, FILTER_VALIDATE_URL) !== false;
|
||||
foreach ($this->validUrlPrefixes as $prefix) {
|
||||
if(strpos($value, $prefix) !== false) {
|
||||
return filter_var($value, FILTER_VALIDATE_URL) !== false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -286,9 +311,14 @@ class Validator
|
||||
*/
|
||||
protected function validateUrlActive($field, $value)
|
||||
{
|
||||
$url = str_replace(array('http://', 'https://', 'ftp://'), '', strtolower($value));
|
||||
foreach ($this->validUrlPrefixes as $prefix) {
|
||||
if(strpos($value, $prefix) !== false) {
|
||||
$url = str_replace($prefix, '', strtolower($value));
|
||||
|
||||
return checkdnsrr($url);
|
||||
return checkdnsrr($url);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@ -396,5 +396,26 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
|
||||
$v->rule('dateAfter', 'date', '2013-01-28');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testContainsValid()
|
||||
{
|
||||
$v = new Validator(array('test_string' => 'this is a test'));
|
||||
$v->rule('contains', 'test_string', 'a test');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testContainsNotFound()
|
||||
{
|
||||
$v = new Validator(array('test_string' => 'this is a test'));
|
||||
$v->rule('contains', 'test_string', 'foobar');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testContainsInvalidValue()
|
||||
{
|
||||
$v = new Validator(array('test_string' => 'this is a test'));
|
||||
$v->rule('contains', 'test_string', array('test'));
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user