Skip to content
Permalink
Browse files

Modifying Controller::validateErrors so it can accept and validate ar…

…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 418b8e59101601249769e255647146d11e24e1e4
Showing with 25 additions and 5 deletions.
  1. +6 −3 cake/libs/controller/controller.php
  2. +19 −2 cake/tests/cases/libs/controller/controller.test.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;
}
}
?>
?>
@@ -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);
}
}
?>
?>

0 comments on commit 418b8e5

Please sign in to comment.
You can’t perform that action at this time.