diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index e9c3fd225c4..0b69f0904be 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -73,6 +73,7 @@ Yii Framework 2 Change Log - Bug #20639: Add missing generics in `yii\web` namespace (mspirkov) - Bug #20645: Add missing generics in `yii\helpers` and `yii\test` namespaces. Fix PHPDoc annotations in `ArrayAccessTrait` (mspirkov) - Bug #20640: Fix `@param` annotation for `$block` in `yii\console\Markdown::renderParagraph()` (mspirkov) +- Enh #20642: Pass `$model` and `$attribute` in Compare Validator's compareValue property closure (samuelrajan747) - Enh #20650: Add PHPStan/Psalm annotations for `yii\di\Container` (mspirkov) - Bug #20654: Add missing generics in `yii\db` namespace. Fix PHPDoc annotations in `yii\db\ArrayExpression` (mspirkov) - Bug #20651: Add missing generics in `yii\filters` namespace (mspirkov) diff --git a/framework/validators/CompareValidator.php b/framework/validators/CompareValidator.php index c893387f258..ce016793c47 100644 --- a/framework/validators/CompareValidator.php +++ b/framework/validators/CompareValidator.php @@ -57,8 +57,17 @@ class CompareValidator extends Validator */ public $compareAttribute; /** - * @var mixed the constant value to be compared with. When both this property - * and [[compareAttribute]] are set, this property takes precedence. + * @var mixed the constant value to be compared with or an anonymous function + * that returns the constant value. When both this property and + * [[compareAttribute]] are set, this property takes precedence. + * The signature of the anonymous function should be as follows, + * + * ``` + * function($model, $attribute) { + * // compute value to compare with + * return $value; + * } + * ``` * @see compareAttribute */ public $compareValue; @@ -144,7 +153,7 @@ public function validateAttribute($model, $attribute) } if ($this->compareValue !== null) { if ($this->compareValue instanceof \Closure) { - $this->compareValue = call_user_func($this->compareValue); + $this->compareValue = call_user_func($this->compareValue, $model, $attribute); } $compareLabel = $compareValue = $compareValueOrAttribute = $this->compareValue; } else {