mirror of
https://github.com/kamilwylegala/cakephp2-php8.git
synced 2025-03-17 15:09:50 +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,10 +3302,13 @@ class Model extends Object implements CakeEventListener {
|
||||||
*/
|
*/
|
||||||
public function isUnique($fields, $or = true) {
|
public function isUnique($fields, $or = true) {
|
||||||
if (is_array($or)) {
|
if (is_array($or)) {
|
||||||
|
$isAssociative = count(array_filter(array_keys($or), 'is_string'));
|
||||||
|
if (!$isAssociative) {
|
||||||
$args = func_get_args();
|
$args = func_get_args();
|
||||||
$fields = $args[1];
|
$fields = $args[1];
|
||||||
$or = isset($args[2]) ? $args[2] : true;
|
$or = isset($args[2]) ? $args[2] : true;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
if (!is_array($fields)) {
|
if (!is_array($fields)) {
|
||||||
$fields = func_get_args();
|
$fields = func_get_args();
|
||||||
if (is_bool($fields[count($fields) - 1])) {
|
if (is_bool($fields[count($fields) - 1])) {
|
||||||
|
|
|
@ -2449,6 +2449,33 @@ class ModelValidationTest extends BaseModelTest {
|
||||||
$this->assertFalse($Article->validates(), 'Should fail, conditions are combined with or');
|
$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