Merge branch 'validation-updates' (closes #3)

This commit is contained in:
Vance Lucas 2013-03-05 14:12:12 -06:00
commit 0c26644400
5 changed files with 70 additions and 6 deletions

View File

@ -92,6 +92,7 @@ if($v->validate()) {
* `dateFormat` - Field is a valid date in the given format * `dateFormat` - Field is a valid date in the given format
* `dateBefore` - Field is a valid date and is before the given date * `dateBefore` - Field is a valid date and is before the given date
* `dateAfter` - Field is a valid date and is after the given date * `dateAfter` - Field is a valid date and is after the given date
* `contains` - Field is a string and contains the given string
## Adding Custom Validation Rules ## Adding Custom Validation Rules

View File

@ -1,6 +1,6 @@
{ {
"name": "vlucas/valitron", "name": "vlucas/valitron",
"version": "1.0.1", "version": "1.0.2",
"type": "library", "type": "library",
"description": "Simple, elegant, stand-alone validation library with NO dependencies", "description": "Simple, elegant, stand-alone validation library with NO dependencies",
"keywords": ["validation", "validator", "valid"], "keywords": ["validation", "validator", "valid"],

View File

@ -22,6 +22,7 @@ return array(
'date' => "Invalid date", 'date' => "Invalid date",
'dateFormat' => "Must be date with format '%s'", 'dateFormat' => "Must be date with format '%s'",
'dateBefore' => "Must be date before '%s'", 'dateBefore' => "Must be date before '%s'",
'dateAfter' => "Must be date after '%s'" 'dateAfter' => "Must be date after '%s'",
'contains' => "Must contain %s"
); );

View File

@ -23,6 +23,7 @@ class Validator
const ERROR_DEFAULT = 'Invalid'; const ERROR_DEFAULT = 'Invalid';
protected $validUrlPrefixes = array('http://', 'https://', 'ftp://');
/** /**
* Setup validation * Setup validation
@ -241,6 +242,25 @@ class Validator
return !$this->validateIn($field, $value, $params); 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 * Validate that a field is a valid IP address
* *
@ -274,7 +294,12 @@ class Validator
*/ */
protected function validateUrl($field, $value) 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) 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;
} }
/** /**
@ -420,7 +450,18 @@ class Validator
*/ */
public function error($field, $msg, array $params = array()) public function error($field, $msg, array $params = array())
{ {
$this->_errors[$field][] = vsprintf($msg, $params); $values = array();
// Printed values need to be in string format
foreach($params as $param) {
if(is_array($param)) {
$param = "['" . implode("', '", $param) . "']";
}
if($param instanceof \DateTime) {
$param = $param->format('Y-m-d');
}
$values[] = $param;
}
$this->_errors[$field][] = vsprintf($msg, $values);
} }
/** /**

View File

@ -396,5 +396,26 @@ class ValidateTest extends \PHPUnit_Framework_TestCase
$v->rule('dateAfter', 'date', '2013-01-28'); $v->rule('dateAfter', 'date', '2013-01-28');
$this->assertFalse($v->validate()); $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());
}
} }