Skip to content

Commit 418b8e5

Browse files
committed
Modifying Controller::validateErrors so it can accept and validate arbitrary model objects, not just those attached to the controller. Test cases updated, fixes #832
1 parent 40cfd5c commit 418b8e5

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

cake/libs/controller/controller.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,11 @@ function validateErrors() {
742742

743743
$errors = array();
744744
foreach ($objects as $object) {
745-
$this->{$object->alias}->set($object->data);
746-
$errors = array_merge($errors, $this->{$object->alias}->invalidFields());
745+
if (isset($this->{$object->alias})) {
746+
$object =& $this->{$object->alias};
747+
}
748+
$object->set($object->data);
749+
$errors = array_merge($errors, $object->invalidFields());
747750
}
748751

749752
return $this->validationErrors = (!empty($errors) ? $errors : false);
@@ -1186,4 +1189,4 @@ function _scaffoldError($method) {
11861189
return false;
11871190
}
11881191
}
1189-
?>
1192+
?>

cake/tests/cases/libs/controller/controller.test.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,13 +1076,30 @@ function testValidateErrors() {
10761076

10771077
$TestController->ControllerComment->invalidate('some_field', 'error_message');
10781078
$TestController->ControllerComment->invalidate('some_field2', 'error_message2');
1079-
$comment = new ControllerComment;
1079+
$comment =& new ControllerComment();
10801080
$comment->set('someVar', 'data');
10811081
$result = $TestController->validateErrors($comment);
10821082
$expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
10831083
$this->assertIdentical($result, $expected);
10841084
$this->assertEqual($TestController->validate($comment), 2);
10851085
}
1086+
/**
1087+
* test that validateErrors works with any old model.
1088+
*
1089+
* @return void
1090+
*/
1091+
function testValidateErrorsOnArbitraryModels() {
1092+
$TestController =& new TestController();
1093+
1094+
$Post = new ControllerPost();
1095+
$Post->validate = array('title' => 'notEmpty');
1096+
$Post->set('title', '');
1097+
$result = $TestController->validateErrors($Post);
1098+
1099+
$expected = array('title' => 'This field cannot be left blank');
1100+
$this->assertEqual($result, $expected);
1101+
}
1102+
10861103
/**
10871104
* testPostConditions method
10881105
*
@@ -1168,4 +1185,4 @@ function testRequestHandlerPrefers(){
11681185
unset($Controller);
11691186
}
11701187
}
1171-
?>
1188+
?>

0 commit comments

Comments
 (0)