-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
/
FilterValidator.php
112 lines (101 loc) · 2.93 KB
/
FilterValidator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/**
* @link https://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license https://www.yiiframework.com/license/
*/
namespace yii\validators;
use yii\base\InvalidConfigException;
use yii\helpers\Json;
/**
* FilterValidator converts the attribute value according to a filter.
*
* FilterValidator is actually not a validator but a data processor.
* It invokes the specified filter callback to process the attribute value
* and save the processed value back to the attribute. The filter must be
* a valid PHP callback with the following signature:
*
* ```php
* function foo($value) {
* // compute $newValue here
* return $newValue;
* }
* ```
*
* 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.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class FilterValidator extends Validator
{
/**
* @var callable the filter. This can be a global function name, anonymous function, etc.
* The function signature must be as follows,
*
* ```php
* function foo($value) {
* // compute $newValue here
* return $newValue;
* }
* ```
*/
public $filter;
/**
* @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;
/**
* @var bool this property is overwritten to be false so that this validator will
* be applied when the value being validated is empty.
*/
public $skipOnEmpty = false;
/**
* {@inheritdoc}
*/
public function init()
{
parent::init();
if ($this->filter === null) {
throw new InvalidConfigException('The "filter" property must be set.');
}
}
/**
* {@inheritdoc}
*/
public function validateAttribute($model, $attribute)
{
$value = $model->$attribute;
if (!$this->skipOnArray || !is_array($value)) {
$model->$attribute = call_user_func($this->filter, $value);
}
}
/**
* {@inheritdoc}
*/
public function clientValidateAttribute($model, $attribute, $view)
{
if ($this->filter !== 'trim') {
return null;
}
ValidationAsset::register($view);
$options = $this->getClientOptions($model, $attribute);
return 'value = yii.validation.trim($form, attribute, ' . Json::htmlEncode($options) . ', value);';
}
/**
* {@inheritdoc}
*/
public function getClientOptions($model, $attribute)
{
$options = [];
if ($this->skipOnEmpty) {
$options['skipOnEmpty'] = 1;
}
return $options;
}
}