Modifying Controller::validateErrors so it can accept and validate arbitrary model objects, not just those attached to the controller. Test cases updated, fixes #832

This commit is contained in:
mark_story 2010-06-20 21:47:34 -04:00
parent 50144d6b5b
commit 2db510d1c1
2 changed files with 23 additions and 3 deletions

View file

@ -843,8 +843,11 @@ class Controller extends Object {
$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);

View file

@ -1228,7 +1228,7 @@ class ControllerTest extends CakeTestCase {
$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');
@ -1236,6 +1236,23 @@ class ControllerTest extends CakeTestCase {
$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
*