Skip to content

Commit

Permalink
Fix yiisoft#18370: Add option to provide a string replacement for `nu…
Browse files Browse the repository at this point in the history
…ll` value in `yii\data\DataFilter`
  • Loading branch information
Bizley authored Nov 10, 2020
1 parent d60252f commit ee0fe97
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 8 deletions.
2 changes: 1 addition & 1 deletion framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Yii Framework 2 Change Log
2.0.40 under development
------------------------

- no changes in this release.
- Enh #18370: Add option to provide a string replacement for `null` value in `yii\data\DataFilter` (bizley)


2.0.39 November 10, 2020
Expand Down
2 changes: 1 addition & 1 deletion framework/data/ActiveDataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ActiveDataFilter extends DataFilter
*
* Usually the map can be left empty as filter operator names are consistent with the ones
* used in [[\yii\db\QueryInterface::where()]]. However, you may want to adjust it in some special cases.
* For example, when using PosgreSQL you may want to setup the following map:
* For example, when using PostgreSQL you may want to setup the following map:
*
* ```php
* [
Expand Down
19 changes: 13 additions & 6 deletions framework/data/DataFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,11 @@ class DataFilter extends Model
* Attribute map will be applied to filter condition in [[normalize()]] method.
*/
public $attributeMap = [];
/**
* @var string representation of `null` instead of literal `null` in case the latter cannot be used.
* @since 2.0.40
*/
public $nullValue = 'NULL';

/**
* @var array|\Closure list of error messages responding to invalid filter structure, in format: `[errorKey => message]`.
Expand Down Expand Up @@ -360,24 +365,24 @@ protected function detectSearchAttributeType(Validator $validator)
if ($validator instanceof BooleanValidator) {
return self::TYPE_BOOLEAN;
}

if ($validator instanceof NumberValidator) {
return $validator->integerOnly ? self::TYPE_INTEGER : self::TYPE_FLOAT;
}

if ($validator instanceof StringValidator) {
return self::TYPE_STRING;
}

if ($validator instanceof EachValidator) {
return self::TYPE_ARRAY;
}

if ($validator instanceof DateValidator) {
if ($validator->type == DateValidator::TYPE_DATETIME) {
return self::TYPE_DATETIME;
}

if ($validator->type == DateValidator::TYPE_TIME) {
return self::TYPE_TIME;
}
Expand Down Expand Up @@ -660,7 +665,7 @@ protected function validateAttributeValue($attribute, $value)
return;
}

$model->{$attribute} = $value;
$model->{$attribute} = $value === $this->nullValue ? null : $value;
if (!$model->validate([$attribute])) {
$this->addError($this->filterAttributeName, $model->getFirstError($attribute));
return;
Expand Down Expand Up @@ -754,6 +759,8 @@ private function normalizeComplexFilter(array $filter)
}
if (is_array($value)) {
$result[$key] = $this->normalizeComplexFilter($value);
} elseif ($value === $this->nullValue) {
$result[$key] = null;
} else {
$result[$key] = $value;
}
Expand Down
23 changes: 23 additions & 0 deletions tests/framework/data/ActiveDataFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,29 @@ public function dataProviderBuild()
],
],
],
[
[
'name' => 'NULL',
'number' => 'NULL',
'price' => 'NULL',
'tags' => ['NULL'],
],
[
'AND',
['name' => ''],
['number' => null],
['price' => null],
['tags' => [null]],
],
],
[
[
'number' => [
'neq' => 'NULL'
],
],
['!=', 'number', null],
],
];
}

Expand Down
42 changes: 42 additions & 0 deletions tests/framework/data/DataFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,31 @@ public function dataProviderValidate()
true,
[],
],
[
[
'name' => [
'eq' => 'NULL',
],
],
true,
[],
],
[
[
'name' => 'NULL',
],
true,
[],
],
[
[
'name' => [
'neq' => 'NULL',
],
],
true,
[],
],
];
}

Expand Down Expand Up @@ -363,6 +388,14 @@ public function dataProviderNormalize()
'datetime' => '2015-06-06 17:46:12',
],
],
[
[
'name' => 'NULL',
],
[
'name' => null,
],
],
];
}

Expand Down Expand Up @@ -403,6 +436,15 @@ public function testNormalize($filter, $expectedResult)
$this->assertEquals($expectedResult, $builder->normalize(false));
}

public function testNormalizeNonDefaultNull()
{
$builder = new DataFilter();
$builder->nullValue = 'abcde';
$builder->setSearchModel((new DynamicModel(['name' => null]))->addRule('name', 'string'));
$builder->filter = ['name' => 'abcde'];
$this->assertEquals(['name' => null], $builder->normalize(false));
}

public function testSetupErrorMessages()
{
$builder = new DataFilter();
Expand Down

0 comments on commit ee0fe97

Please sign in to comment.