From cb880e73b2e397e765dec573fd87f8f2119183ef Mon Sep 17 00:00:00 2001 From: Chris Cornutt Date: Tue, 5 Mar 2013 09:40:28 -0600 Subject: [PATCH 1/4] fixing some logic on the URL checking and adding new "contains" validator --- src/Valitron/Validator.php | 36 +++++++++++++++++++++++++++++++++--- tests/Valitron/Validate.php | 21 +++++++++++++++++++++ 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 3282e4f..fefdafc 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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; } /** diff --git a/tests/Valitron/Validate.php b/tests/Valitron/Validate.php index d70a573..be59089 100644 --- a/tests/Valitron/Validate.php +++ b/tests/Valitron/Validate.php @@ -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()); + } } From 24f26b75e9899486fa1f8d50be0f5553f1ee2327 Mon Sep 17 00:00:00 2001 From: Chris Cornutt Date: Tue, 5 Mar 2013 09:45:58 -0600 Subject: [PATCH 2/4] adding "contains" to the README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 1b0ca27..1e2710b 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ if($v->validate()) { * `dateFormat` - Field is a valid date in the given format * `dateBefore` - Field is a valid date and is before 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 From 6169b04cc59121d852c4817c5818270987650add Mon Sep 17 00:00:00 2001 From: Chris Cornutt Date: Tue, 5 Mar 2013 10:41:38 -0600 Subject: [PATCH 3/4] adding default error message to EN language file for "contains" --- lang/en.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lang/en.php b/lang/en.php index ac657cc..755761a 100644 --- a/lang/en.php +++ b/lang/en.php @@ -22,6 +22,7 @@ return array( 'date' => "Invalid date", 'dateFormat' => "Must be date with format '%s'", 'dateBefore' => "Must be date before '%s'", - 'dateAfter' => "Must be date after '%s'" + 'dateAfter' => "Must be date after '%s'", + 'contains' => "Must contain %s" ); From 36f4e8d3b132286aabc3df9092fbaec3cfb73cc3 Mon Sep 17 00:00:00 2001 From: Vance Lucas Date: Tue, 5 Mar 2013 14:00:13 -0600 Subject: [PATCH 4/4] Ensuring array values are strings for sprintf --- src/Valitron/Validator.php | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index fefdafc..c90fe5a 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -450,7 +450,18 @@ class Validator */ 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); } /**