mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
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:
@ -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 #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)
|
||||
- Enh #19304: Add filtering validator `yii\validators\TrimValidator` (WinterSilence)
|
||||
- Enh #19309: Optimize `yii\base\Model::attributes()` (WinterSilence)
|
||||
|
||||
|
||||
|
82
framework/validators/TrimValidator.php
Normal file
82
framework/validators/TrimValidator.php
Normal 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,
|
||||
];
|
||||
}
|
||||
}
|
@ -41,7 +41,7 @@ use yii\base\NotSupportedException;
|
||||
* - `required`: [[RequiredValidator]]
|
||||
* - `safe`: [[SafeValidator]]
|
||||
* - `string`: [[StringValidator]]
|
||||
* - `trim`: [[FilterValidator]]
|
||||
* - `trim`: [[TrimValidator]]
|
||||
* - `unique`: [[UniqueValidator]]
|
||||
* - `url`: [[UrlValidator]]
|
||||
* - `ip`: [[IpValidator]]
|
||||
@ -91,8 +91,7 @@ class Validator extends Component
|
||||
'safe' => 'yii\validators\SafeValidator',
|
||||
'string' => 'yii\validators\StringValidator',
|
||||
'trim' => [
|
||||
'class' => 'yii\validators\FilterValidator',
|
||||
'filter' => 'trim',
|
||||
'class' => 'yii\validators\TrimValidator',
|
||||
'skipOnArray' => true,
|
||||
],
|
||||
'unique' => 'yii\validators\UniqueValidator',
|
||||
|
Reference in New Issue
Block a user