mirror of
https://github.com/yiisoft/yii2.git
synced 2025-11-10 23:50:38 +08:00
Fix #18756: Fix \yii\validators\ExistValidator::queryValueExists to validate against an array of unique values
This commit is contained in:
@@ -19,6 +19,7 @@ Yii Framework 2 Change Log
|
|||||||
- Bug #18648: Fix `yii\web\Request` to properly handle HTTP Basic Auth headers (olegbaturin)
|
- Bug #18648: Fix `yii\web\Request` to properly handle HTTP Basic Auth headers (olegbaturin)
|
||||||
- Enh #18726: Added `yii\helpers\Json::$prettyPrint` (rhertogh)
|
- Enh #18726: Added `yii\helpers\Json::$prettyPrint` (rhertogh)
|
||||||
- Enh #18734: Added `yii\validators\EmailValidator::$enableLocalIDN` (brandonkelly)
|
- Enh #18734: Added `yii\validators\EmailValidator::$enableLocalIDN` (brandonkelly)
|
||||||
|
- Bug #18756: Fix `\yii\validators\ExistValidator::queryValueExists` to validate against an array of unique values (DrDeath72)
|
||||||
- Enh #18656: Added ability for `yii serve`'s `--router` param to take an alias (markhuot)
|
- Enh #18656: Added ability for `yii serve`'s `--router` param to take an alias (markhuot)
|
||||||
- Bug #18274: Fix `yii\log\Logger` to calculate profile timings no matter the value of the flush interval (bizley)
|
- Bug #18274: Fix `yii\log\Logger` to calculate profile timings no matter the value of the flush interval (bizley)
|
||||||
|
|
||||||
|
|||||||
@@ -280,7 +280,7 @@ class ExistValidator extends Validator
|
|||||||
private function queryValueExists($query, $value)
|
private function queryValueExists($query, $value)
|
||||||
{
|
{
|
||||||
if (is_array($value)) {
|
if (is_array($value)) {
|
||||||
return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value) ;
|
return $query->count("DISTINCT [[$this->targetAttribute]]") == count(array_unique($value)) ;
|
||||||
}
|
}
|
||||||
return $query->exists();
|
return $query->exists();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -97,6 +97,13 @@ abstract class ExistValidatorTest extends DatabaseTestCase
|
|||||||
$m->test_val = [2, 3, 4, 5];
|
$m->test_val = [2, 3, 4, 5];
|
||||||
$val->validateAttribute($m, 'test_val');
|
$val->validateAttribute($m, 'test_val');
|
||||||
$this->assertFalse($m->hasErrors('test_val'));
|
$this->assertFalse($m->hasErrors('test_val'));
|
||||||
|
// existing non-unique array
|
||||||
|
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
||||||
|
$val->allowArray = true;
|
||||||
|
$m = new ValidatorTestRefModel();
|
||||||
|
$m->test_val = [2, 2, 3, 3, 4, 4, 5, 5];
|
||||||
|
$val->validateAttribute($m, 'test_val');
|
||||||
|
$this->assertFalse($m->hasErrors('test_val'));
|
||||||
// non-existing array
|
// non-existing array
|
||||||
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
$val = new ExistValidator(['targetAttribute' => 'ref']);
|
||||||
$val->allowArray = true;
|
$val->allowArray = true;
|
||||||
|
|||||||
Reference in New Issue
Block a user