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) diff --git a/lang/en.php b/lang/en.php index d768a20..f20c85e 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'" ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 15bb32c..b9b4e41 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 eb51756..acd566e 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -833,6 +833,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()));