Add TrimValidator (#19304)

* Add TrimValidator

* Update Validator.php

* Update Validator.php

* Update TrimValidator.php

* Update TrimValidator.php

* Update TrimValidator.php

* Update TrimValidator.php

* Update CHANGELOG.md

* Update Validator.php
This commit is contained in:
Anton
2022-03-22 13:16:47 +03:00
committed by GitHub
parent 7f244793f5
commit 7cc45f75f5
3 changed files with 85 additions and 3 deletions

View File

@ -7,6 +7,7 @@ Yii Framework 2 Change Log
- Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran) - Bug #19243: Handle `finfo_open` for tar.xz as `application/octet-stream` on PHP 8.1 (longthanhtran)
- Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer) - Bug #19235: Fix return type compatibility of `yii\web\SessionIterator` class methods for PHP 8.1 (virtual-designer)
- Bug #19291: Reset errors and validators in `yii\base\Model::__clone()` (WinterSilence) - 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) - Enh #19309: Optimize `yii\base\Model::attributes()` (WinterSilence)

View File

@ -0,0 +1,82 @@
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
/**
* This class converts the attribute value(s) to string(s) and strip characters.
*
* @since 2.0.46
*/
class TrimValidator extends Validator
{
/**
* @var string The list of characters to strip, with `..` can specify a range of characters.
* For example, set '\/ ' to normalize path or namespace.
*/
public $chars;
/**
* @var bool Whether the filter should be skipped if an array input is given.
* If true and an array input is given, the filter will not be applied.
*/
public $skipOnArray = false;
/**
* @inheritDoc
*/
public $skipOnEmpty = false;
/**
* @inheritDoc
*/
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if (!$this->skipOnArray || !is_array($value)) {
$model->$attribute = is_array($value)
? array_map([$this, 'trimValue'], $value)
: $this->trimValue($value);
}
}
/**
* Converts given value to string and strips declared characters.
*
* @param mixed $value the value to strip
* @return string
*/
protected function trimValue($value)
{
return $this->isEmpty($value) ? '' : trim((string) $value, $this->chars ?: " \n\r\t\v\x00");
}
/**
* @inheritDoc
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->skipOnArray && is_array($model->$attribute)) {
return null;
}
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'value = yii.validation.trim($form, attribute, ' . json_encode($options) . ', value);';
}
/**
* @inheritDoc
*/
public function getClientOptions($model, $attribute)
{
return [
'skipOnArray' => (bool) $this->skipOnArray,
'skipOnEmpty' => (bool) $this->skipOnEmpty,
'chars' => $this->chars ?: false,
];
}
}

View File

@ -41,7 +41,7 @@ use yii\base\NotSupportedException;
* - `required`: [[RequiredValidator]] * - `required`: [[RequiredValidator]]
* - `safe`: [[SafeValidator]] * - `safe`: [[SafeValidator]]
* - `string`: [[StringValidator]] * - `string`: [[StringValidator]]
* - `trim`: [[FilterValidator]] * - `trim`: [[TrimValidator]]
* - `unique`: [[UniqueValidator]] * - `unique`: [[UniqueValidator]]
* - `url`: [[UrlValidator]] * - `url`: [[UrlValidator]]
* - `ip`: [[IpValidator]] * - `ip`: [[IpValidator]]
@ -91,8 +91,7 @@ class Validator extends Component
'safe' => 'yii\validators\SafeValidator', 'safe' => 'yii\validators\SafeValidator',
'string' => 'yii\validators\StringValidator', 'string' => 'yii\validators\StringValidator',
'trim' => [ 'trim' => [
'class' => 'yii\validators\FilterValidator', 'class' => 'yii\validators\TrimValidator',
'filter' => 'trim',
'skipOnArray' => true, 'skipOnArray' => true,
], ],
'unique' => 'yii\validators\UniqueValidator', 'unique' => 'yii\validators\UniqueValidator',