diff --git a/cake/libs/controller/controller.php b/cake/libs/controller/controller.php index a7e5a972201..dafe6b6d8a5 100644 --- a/cake/libs/controller/controller.php +++ b/cake/libs/controller/controller.php @@ -742,8 +742,11 @@ function validateErrors() { $errors = array(); foreach ($objects as $object) { - $this->{$object->alias}->set($object->data); - $errors = array_merge($errors, $this->{$object->alias}->invalidFields()); + if (isset($this->{$object->alias})) { + $object =& $this->{$object->alias}; + } + $object->set($object->data); + $errors = array_merge($errors, $object->invalidFields()); } return $this->validationErrors = (!empty($errors) ? $errors : false); @@ -1186,4 +1189,4 @@ function _scaffoldError($method) { return false; } } -?> \ No newline at end of file +?> diff --git a/cake/tests/cases/libs/controller/controller.test.php b/cake/tests/cases/libs/controller/controller.test.php index e65a639fa30..6d3d5926360 100644 --- a/cake/tests/cases/libs/controller/controller.test.php +++ b/cake/tests/cases/libs/controller/controller.test.php @@ -1076,13 +1076,30 @@ function testValidateErrors() { $TestController->ControllerComment->invalidate('some_field', 'error_message'); $TestController->ControllerComment->invalidate('some_field2', 'error_message2'); - $comment = new ControllerComment; + $comment =& new ControllerComment(); $comment->set('someVar', 'data'); $result = $TestController->validateErrors($comment); $expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2'); $this->assertIdentical($result, $expected); $this->assertEqual($TestController->validate($comment), 2); } +/** + * test that validateErrors works with any old model. + * + * @return void + */ + function testValidateErrorsOnArbitraryModels() { + $TestController =& new TestController(); + + $Post = new ControllerPost(); + $Post->validate = array('title' => 'notEmpty'); + $Post->set('title', ''); + $result = $TestController->validateErrors($Post); + + $expected = array('title' => 'This field cannot be left blank'); + $this->assertEqual($result, $expected); + } + /** * testPostConditions method * @@ -1168,4 +1185,4 @@ function testRequestHandlerPrefers(){ unset($Controller); } } -?> \ No newline at end of file +?>