diff --git a/lib/Cake/Model/Model.php b/lib/Cake/Model/Model.php index a2bf2a588..6a0083fe1 100644 --- a/lib/Cake/Model/Model.php +++ b/lib/Cake/Model/Model.php @@ -2357,7 +2357,9 @@ class Model extends Object implements CakeEventListener { if ($options['deep']) { $validates = $this->{$association}->validateAssociated($values, $options); } else { - $validates = $this->{$association}->create($values) !== null && $this->{$association}->validates($options); + $this->{$association}->create(null); + $validates = $this->{$association}->set($values) && $this->{$association}->validates($options); + $data[$association] = $this->{$association}->data[$this->{$association}->alias]; } if (is_array($validates)) { if (in_array(false, $validates, true)) { diff --git a/lib/Cake/Test/Case/Model/ModelValidationTest.php b/lib/Cake/Test/Case/Model/ModelValidationTest.php index eb90ebbfa..edbd9efd4 100644 --- a/lib/Cake/Test/Case/Model/ModelValidationTest.php +++ b/lib/Cake/Test/Case/Model/ModelValidationTest.php @@ -1127,4 +1127,34 @@ class ModelValidationTest extends BaseModelTest { $this->assertEquals($expected['Article'], $result['Article']); } +/** + * Tests that altering data in a beforeValidate callback will lead to saving those + * values in database, this time with belongsTo associations + * + * @return void + */ + public function testValidateFirstAssociatedWithBeforeValidate2() { + $this->loadFixtures('Article', 'User'); + $model = new CustomArticle(); + $model->validate = array( + 'title' => array( + 'notempty' => array( + 'rule' => 'notEmpty', + 'required' => true + ) + ) + ); + + $data = array( + 'User' => array('user' => 'foo', 'password' => 'bar'), + 'CustomArticle' => array( + 'body' => 'a test' + ) + ); + $result = $model->saveAll($data, array('validate' => 'first')); + $this->assertTrue($result); + + $this->assertEquals('foo', $model->field('title', array('body' => 'a test'))); + } + } \ No newline at end of file diff --git a/lib/Cake/Test/Case/Model/ModelWriteTest.php b/lib/Cake/Test/Case/Model/ModelWriteTest.php index 0c8b4e054..1be1bbf77 100644 --- a/lib/Cake/Test/Case/Model/ModelWriteTest.php +++ b/lib/Cake/Test/Case/Model/ModelWriteTest.php @@ -5947,6 +5947,7 @@ class ModelWriteTest extends BaseModelTest { * @return void */ public function testValidateAssociated() { + $this->loadFixtures('Attachment', 'Article', 'Comment'); $TestModel = new Comment(); $TestModel->Attachment->validate = array('attachment' => 'notEmpty');