Skip to content

Commit

Permalink
Fix #19322: Revert force setting value to empty string in case it's `…
Browse files Browse the repository at this point in the history
…null` in `yii\validators\FilterValidator::validateAttribute()`
  • Loading branch information
Bizley committed Mar 24, 2022
1 parent 7cc45f7 commit 2874e07
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
1 change: 1 addition & 0 deletions framework/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Yii Framework 2 Change Log
- Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence)
- Enh #19304: Add filtering validator `yii\validators\TrimValidator` (WinterSilence)
- Enh #19309: Optimize `yii\base\Model::attributes()` (WinterSilence)
- Bug #19322: Revert force setting value to empty string in case it's `null` in `yii\validators\FilterValidator::validateAttribute()` (bizley)


2.0.45 February 11, 2022
Expand Down
3 changes: 2 additions & 1 deletion framework/validators/FilterValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
* ```
*
* Many PHP functions qualify this signature (e.g. `trim()`).
* If the callback function requires non-null argument (important since PHP 8.1)
* remember to set [[skipOnEmpty]] to `true` otherwise you may trigger an error.
*
* To specify the filter, set [[filter]] property to be the callback.
*
Expand Down Expand Up @@ -75,7 +77,6 @@ public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if (!$this->skipOnArray || !is_array($value)) {
$value = isset($value) ? $value : '';

This comment has been minimized.

Copy link
@liasica

liasica Jul 11, 2022

That's not working.

We need check $value null and set default string value, because when $value is null, will throw exception PHP Deprecated Warning 'yii\base\ErrorException' with message 'trim(): Passing null to parameter #1 ($string) of type string is deprecated'

$model->$attribute = call_user_func($this->filter, $value);
}
}
Expand Down
2 changes: 1 addition & 1 deletion tests/framework/base/ModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ public function rules()
{
return [
[['id'], 'required', 'except' => 'administration'],
[['name', 'description'], 'filter', 'filter' => 'trim'],
[['name', 'description'], 'filter', 'filter' => 'trim', 'skipOnEmpty' => true],
[['is_disabled'], 'boolean', 'on' => 'administration'],
];
}
Expand Down

0 comments on commit 2874e07

Please sign in to comment.