Browse Source

Fix #18460: `compareValue` in `CompareValidator` can now take a closure returning a value

tags/2.0.41
Mohamed Abdel-Monem Hussein 4 years ago committed by GitHub
parent
commit
b8e31c0d31
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      docs/guide/tutorial-core-validators.md
  2. 1
      framework/CHANGELOG.md
  3. 10
      framework/validators/CompareValidator.php
  4. 9
      tests/framework/validators/CompareValidatorTest.php

2
docs/guide/tutorial-core-validators.md

@ -82,7 +82,7 @@ is as specified by the `operator` property.
is being used to validate an attribute, the default value of this property would be the name of
the attribute suffixed with `_repeat`. For example, if the attribute being validated is `password`,
then this property will default to `password_repeat`.
- `compareValue`: a constant value that the input value should be compared with. When both
- `compareValue`: a constant value (or a closure returning a value) that the input value should be compared with. When both
of this property and `compareAttribute` are specified, this property will take precedence.
- `operator`: the comparison operator. Defaults to `==`, meaning checking if the input value is equal
to that of `compareAttribute` or `compareValue`. The following operators are supported:

1
framework/CHANGELOG.md

@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Enh #18455: Add ability to use separate attributes for data model and filter model of `yii\grid\GridView` in `yii\grid\DataColumn` (PowerGamer1)
- Enh #18447: Do not use `getLastInsertID` to get PK from insert query to lower collision probability for concurrent inserts (darkdef)
- Bug #18448: Fix issues in queries and tests for older MSSQL versions (darkdef)
- Enh #18460: `compareValue` in `CompareValidator` can now take a closure returning a value (mmonem)
- Bug #18464: Fix bug with processing fallback messages when translation language is set to `null` (bizley)
- Enh #18457: Add `EVENT_RESET` and `EVENT_FINISH` events to `yii\db\BatchQueryResult` (brandonkelly)
- Bug #18472: Fix initializing `db` component configuration in `yii\data\ActiveDataProvider` (bizley)

10
framework/validators/CompareValidator.php

@ -146,6 +146,9 @@ class CompareValidator extends Validator
return;
}
if ($this->compareValue !== null) {
if ($this->compareValue instanceof \Closure) {
$this->compareValue = call_user_func($this->compareValue);
}
$compareLabel = $compareValue = $compareValueOrAttribute = $this->compareValue;
} else {
$compareAttribute = $this->compareAttribute === null ? $attribute . '_repeat' : $this->compareAttribute;
@ -170,6 +173,9 @@ class CompareValidator extends Validator
if ($this->compareValue === null) {
throw new InvalidConfigException('CompareValidator::compareValue must be set.');
}
if ($this->compareValue instanceof \Closure) {
$this->compareValue = call_user_func($this->compareValue);
}
if (!$this->compareValues($this->operator, $this->type, $value, $this->compareValue)) {
return [$this->message, [
'compareAttribute' => $this->compareValue,
@ -225,6 +231,10 @@ class CompareValidator extends Validator
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->compareValue != null && $this->compareValue instanceof \Closure) {
$this->compareValue = call_user_func($this->compareValue);
}
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);

9
tests/framework/validators/CompareValidatorTest.php

@ -40,6 +40,15 @@ class CompareValidatorTest extends TestCase
$this->assertTrue($val->validate($value));
$this->assertTrue($val->validate((string) $value));
$this->assertFalse($val->validate($value + 1));
// Using a closure for compareValue
$val = new CompareValidator(['compareValue' => function() use ($value) {
return $value;
}]);
$this->assertTrue($val->validate($value));
$this->assertTrue($val->validate((string) $value));
$this->assertFalse($val->validate($value + 1));
foreach ($this->getOperationTestData($value) as $op => $tests) {
$val = new CompareValidator(['compareValue' => $value]);
$val->operator = $op;

Loading…
Cancel
Save