Skip to content

Commit

Permalink
Modifying Controller::validateErrors so it can accept and validate ar…
Browse files Browse the repository at this point in the history
…bitrary model objects, not just those attached to the controller. Test cases updated, fixes #832
  • Loading branch information
markstory committed Jun 21, 2010
1 parent 40cfd5c commit 418b8e5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
9 changes: 6 additions & 3 deletions cake/libs/controller/controller.php
Expand Up @@ -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);
Expand Down Expand Up @@ -1186,4 +1189,4 @@ function _scaffoldError($method) {
return false;
}
}
?>
?>
21 changes: 19 additions & 2 deletions cake/tests/cases/libs/controller/controller.test.php
Expand Up @@ -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
*
Expand Down Expand Up @@ -1168,4 +1185,4 @@ function testRequestHandlerPrefers(){
unset($Controller);
}
}
?>
?>

0 comments on commit 418b8e5

Please sign in to comment.