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 50144d6 commit 2db510d1c15f2627332cd87fef0538aa61f2a67f
Showing with 23 additions and 3 deletions.
  1. +5 −2 cake/libs/controller/controller.php
  2. +18 −1 cake/tests/cases/libs/controller/controller.test.php
@@ -843,8 +843,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);
@@ -1228,14 +1228,31 @@ 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
*

0 comments on commit 2db510d

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