Skip to content

Commit

Permalink
Fixing yet another issue related to beforeValidate and
Browse files Browse the repository at this point in the history
validateAssociated
  • Loading branch information
lorenzo committed Jun 6, 2012
1 parent 8966f1b commit 111a232
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/Cake/Model/Model.php
Expand Up @@ -2357,7 +2357,9 @@ public function validateAssociated(&$data, $options = array()) {
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)) {
Expand Down
30 changes: 30 additions & 0 deletions lib/Cake/Test/Case/Model/ModelValidationTest.php
Expand Up @@ -1127,4 +1127,34 @@ public function testFirstWithDefaults() {
$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')));
}

}
1 change: 1 addition & 0 deletions lib/Cake/Test/Case/Model/ModelWriteTest.php
Expand Up @@ -5947,6 +5947,7 @@ public function testSaveManyValidateFirstAtomicFalse() {
* @return void
*/
public function testValidateAssociated() {
$this->loadFixtures('Attachment', 'Article', 'Comment');
$TestModel = new Comment();
$TestModel->Attachment->validate = array('attachment' => 'notEmpty');

Expand Down

0 comments on commit 111a232

Please sign in to comment.