From 60bb8f028731e581fe5803cd0dc572807053a59a Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Mon, 14 Jul 2014 19:35:35 +0200 Subject: [PATCH 1/3] Add instanceOf to rules in readme.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index d3be105..f7eefc0 100644 --- a/README.md +++ b/README.md @@ -114,6 +114,7 @@ V::lang('ar'); * `dateAfter` - Field is a valid date and is after the given date * `contains` - Field is a string and contains the given string * `creditCard` - Field is a valid credit card number + * `instanceOf` - Field contains an instance of the given class **NOTE**: If you are comparing floating-point numbers with min/max validators, you should install the [BCMath](http://us3.php.net/manual/en/book.bc.php) From 6f5dad874588d41849176d31731be13ab1d15258 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Mon, 14 Jul 2014 23:27:57 +0200 Subject: [PATCH 2/3] Update en.php to include instanceOf --- lang/en.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lang/en.php b/lang/en.php index 60cb8c6..947ab56 100644 --- a/lang/en.php +++ b/lang/en.php @@ -29,5 +29,6 @@ return array( 'lengthBetween' => "must be between %d and %d characters", 'creditCard' => "must be a valid credit card number", "lengthMin" => "must contain greater than %d characters", - "lengthMax" => "must contain less than %d characters" + "lengthMax" => "must contain less than %d characters", + "instanceOf" => "must be an instance of '%s'" ); From 9d90b08813ebaf29adcaee42531f6ef10b703578 Mon Sep 17 00:00:00 2001 From: Victor Bjelkholm Date: Tue, 29 Jul 2014 16:39:14 +0200 Subject: [PATCH 3/3] Show instance name in instanceOf error message --- src/Valitron/Validator.php | 4 ++++ tests/Valitron/ValidateTest.php | 13 +++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 0a96bf2..8709fc9 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -736,6 +736,10 @@ class Validator } if ($param instanceof \DateTime) { $param = $param->format('Y-m-d'); + } else { + if(is_object($param)) { + $param = get_class($param); + } } // Use custom label instead of field name if set if(is_string($params[0])) { diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d1ae011..0a71b18 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -829,6 +829,19 @@ class ValidateTest extends BaseTestCase $this->assertTrue($v->validate()); } + public function testInstanceOfErrorMessageShowsInstanceName() + { + $v = new Validator(array('attributeName' => new Validator(array()))); + $v->rule('instanceOf', 'attributeName', new stdClass()); + $v->validate(); + $expected_error = array( + "attributeName" => array( + "AttributeName must be an instance of 'stdClass'" + ) + ); + $this->assertEquals($expected_error, $v->errors()); + } + public function testInstanceOfInvalidWithString() { $v = new Validator(array('attributeName' => new stdClass()));