Skip to content

Commit

Permalink
Merge pull request #3884 from cakephp/3.0-validation-fixes
Browse files Browse the repository at this point in the history
3.0 validation fixes
  • Loading branch information
markstory committed Jul 5, 2014
2 parents c854e1f + a3aa82c commit 2d5244c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ORM/Table.php
Expand Up @@ -1136,8 +1136,9 @@ protected function _processSave($entity, $options) {

$associated = $options['associated'];
$options['associated'] = [];
$validate = $options['validate'];

if ($options['validate'] && !$this->validate($entity, $options)) {
if ($validate && !$this->validate($entity, $options)) {
return false;
}

Expand All @@ -1153,7 +1154,7 @@ protected function _processSave($entity, $options) {
$this,
$entity,
$options['associated'],
$options->getArrayCopy()
['validate' => (bool)$validate] + $options->getArrayCopy()
);

if (!$saved && $options['atomic']) {
Expand Down
24 changes: 24 additions & 0 deletions tests/TestCase/ORM/TableTest.php
Expand Up @@ -2083,6 +2083,30 @@ public function testAfterValidate() {
$this->assertEquals(['Not good'], $entity->errors('username'));
}

/**
* Tests using a custom validation object when saving and saving associations
*
* @return void
*/
public function testSaveWithDifferentValidatorAndAssociations() {
$entity = new \Cake\ORM\Entity([
'title' => 'foo',
'body' => 'bar',
'author' => new \Cake\ORM\Entity([
'name' => 'Susan'
])
]);
$table = TableRegistry::get('articles');
$table->belongsTo('authors');
$validator = (new Validator)->validatePresence('body');
$table->validator('custom', $validator);

$validator2 = (new Validator)->validatePresence('thing');
$table->authors->validator('default', $validator2);
$this->assertFalse($table->save($entity, ['validate' => 'custom']), 'default was not used');
$this->assertNotEmpty($entity->author->errors('thing'));
}

/**
* Test magic findByXX method.
*
Expand Down

0 comments on commit 2d5244c

Please sign in to comment.