mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2024-11-15 03:18:26 +00:00
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:
parent
50144d6b5b
commit
2db510d1c1
2 changed files with 23 additions and 3 deletions
|
@ -843,8 +843,11 @@ class Controller extends Object {
|
||||||
|
|
||||||
$errors = array();
|
$errors = array();
|
||||||
foreach ($objects as $object) {
|
foreach ($objects as $object) {
|
||||||
$this->{$object->alias}->set($object->data);
|
if (isset($this->{$object->alias})) {
|
||||||
$errors = array_merge($errors, $this->{$object->alias}->invalidFields());
|
$object =& $this->{$object->alias};
|
||||||
|
}
|
||||||
|
$object->set($object->data);
|
||||||
|
$errors = array_merge($errors, $object->invalidFields());
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this->validationErrors = (!empty($errors) ? $errors : false);
|
return $this->validationErrors = (!empty($errors) ? $errors : false);
|
||||||
|
|
|
@ -1228,7 +1228,7 @@ class ControllerTest extends CakeTestCase {
|
||||||
|
|
||||||
$TestController->ControllerComment->invalidate('some_field', 'error_message');
|
$TestController->ControllerComment->invalidate('some_field', 'error_message');
|
||||||
$TestController->ControllerComment->invalidate('some_field2', 'error_message2');
|
$TestController->ControllerComment->invalidate('some_field2', 'error_message2');
|
||||||
$comment = new ControllerComment;
|
$comment =& new ControllerComment();
|
||||||
$comment->set('someVar', 'data');
|
$comment->set('someVar', 'data');
|
||||||
$result = $TestController->validateErrors($comment);
|
$result = $TestController->validateErrors($comment);
|
||||||
$expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
|
$expected = array('some_field' => 'error_message', 'some_field2' => 'error_message2');
|
||||||
|
@ -1236,6 +1236,23 @@ class ControllerTest extends CakeTestCase {
|
||||||
$this->assertEqual($TestController->validate($comment), 2);
|
$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
|
* testPostConditions method
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue