mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-01-31 09:06:17 +00:00
Merge pull request #4871 from rchavik/2.5-is-unique-test
2.5 Add isUnique test + possible fix
This commit is contained in:
commit
1ded0c21dd
2 changed files with 33 additions and 3 deletions
|
@ -3302,9 +3302,12 @@ class Model extends Object implements CakeEventListener {
|
|||
*/
|
||||
public function isUnique($fields, $or = true) {
|
||||
if (is_array($or)) {
|
||||
$args = func_get_args();
|
||||
$fields = $args[1];
|
||||
$or = isset($args[2]) ? $args[2] : true;
|
||||
$isAssociative = count(array_filter(array_keys($or), 'is_string'));
|
||||
if (!$isAssociative) {
|
||||
$args = func_get_args();
|
||||
$fields = $args[1];
|
||||
$or = isset($args[2]) ? $args[2] : true;
|
||||
}
|
||||
}
|
||||
if (!is_array($fields)) {
|
||||
$fields = func_get_args();
|
||||
|
|
|
@ -2449,6 +2449,33 @@ class ModelValidationTest extends BaseModelTest {
|
|||
$this->assertFalse($Article->validates(), 'Should fail, conditions are combined with or');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test backward compatibility of the isUnique method when used as a validator for multiple fields.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testBackwardCompatIsUniqueValidator() {
|
||||
$this->loadFixtures('Article');
|
||||
$Article = ClassRegistry::init('Article');
|
||||
$Article->validate = array(
|
||||
'title' => array(
|
||||
'duplicate' => array(
|
||||
'rule' => 'isUnique',
|
||||
'message' => 'Title must be unique',
|
||||
),
|
||||
'minLength' => array(
|
||||
'rule' => array('minLength', 1),
|
||||
'message' => 'Title cannot be empty',
|
||||
),
|
||||
)
|
||||
);
|
||||
$data = array(
|
||||
'title' => 'First Article',
|
||||
);
|
||||
$data = $Article->create($data);
|
||||
$this->assertFalse($Article->validates(), 'Contains a dupe');
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue