Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Correctly setting the error message for existsIn() when an array is used
  • Loading branch information
lorenzo committed Mar 27, 2015
1 parent 099b39f commit e7580d3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/ORM/RulesChecker.php
Expand Up @@ -354,7 +354,7 @@ public function existsIn($field, $table, $message = null)
}
}

$errorField = $field;
$errorField = is_string($field) ? $field : current($field);
return $this->_addError(new ExistsIn($field, $table), '_existsIn', compact('errorField', 'message'));
}

Expand Down
21 changes: 21 additions & 0 deletions tests/TestCase/ORM/RulesCheckerIntegrationTest.php
Expand Up @@ -633,6 +633,27 @@ public function testExistsInAliasPrefix()
$this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
}

/**
* Tests that using an array in existsIn() sets the error message correctly
*
* @return
*/
public function testExistsInErrorWithArrayField()
{
$entity = new Entity([
'title' => 'An Article',
'author_id' => 500
]);

$table = TableRegistry::get('Articles');
$table->belongsTo('Authors');
$rules = $table->rulesChecker();
$rules->add($rules->existsIn(['author_id'], 'Authors'));

$this->assertFalse($table->save($entity));
$this->assertEquals(['_existsIn' => 'This value does not exist'], $entity->errors('author_id'));
}

/**
* Tests using rules to prevent delete operations
*
Expand Down

0 comments on commit e7580d3

Please sign in to comment.