Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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...
commit 418b8e59101601249769e255647146d11e24e1e4 1 parent 40cfd5c
@markstory markstory authored
View
9 cake/libs/controller/controller.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;
}
}
-?>
+?>
View
21 cake/tests/cases/libs/controller/controller.test.php
@@ -1076,7 +1076,7 @@ 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');
@@ -1084,6 +1084,23 @@ function testValidateErrors() {
$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
*
* @access public
@@ -1168,4 +1185,4 @@ function testRequestHandlerPrefers(){
unset($Controller);
}
}
-?>
+?>
Please sign in to comment.
Something went wrong with that request. Please try again.