1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\validators; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\helpers\StringHelper; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* NumberValidator validates that the attribute value is a number. |
15
|
|
|
* |
16
|
|
|
* The format of the number must match the regular expression specified in [[integerPattern]] or [[numberPattern]]. |
17
|
|
|
* Optionally, you may configure the [[max]] and [[min]] properties to ensure the number |
18
|
|
|
* is within certain range. |
19
|
|
|
* |
20
|
|
|
* @author Qiang Xue <[email protected]> |
21
|
|
|
* @since 2.0 |
22
|
|
|
*/ |
23
|
|
|
class NumberValidator extends Validator |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var bool whether the attribute value can only be an integer. Defaults to false. |
27
|
|
|
*/ |
28
|
|
|
public $integerOnly = false; |
29
|
|
|
/** |
30
|
|
|
* @var int|float upper limit of the number. Defaults to null, meaning no upper limit. |
31
|
|
|
* @see tooBig for the customized message used when the number is too big. |
32
|
|
|
*/ |
33
|
|
|
public $max; |
34
|
|
|
/** |
35
|
|
|
* @var int|float lower limit of the number. Defaults to null, meaning no lower limit. |
36
|
|
|
* @see tooSmall for the customized message used when the number is too small. |
37
|
|
|
*/ |
38
|
|
|
public $min; |
39
|
|
|
/** |
40
|
|
|
* @var string user-defined error message used when the value is bigger than [[max]]. |
41
|
|
|
*/ |
42
|
|
|
public $tooBig; |
43
|
|
|
/** |
44
|
|
|
* @var string user-defined error message used when the value is smaller than [[min]]. |
45
|
|
|
*/ |
46
|
|
|
public $tooSmall; |
47
|
|
|
/** |
48
|
|
|
* @var string the regular expression for matching integers. |
49
|
|
|
*/ |
50
|
|
|
public $integerPattern = '/^\s*[+-]?\d+\s*$/'; |
51
|
|
|
/** |
52
|
|
|
* @var string the regular expression for matching numbers. It defaults to a pattern |
53
|
|
|
* that matches floating numbers with optional exponential part (e.g. -1.23e-10). |
54
|
|
|
*/ |
55
|
|
|
public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'; |
56
|
|
|
|
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
53 |
|
public function init() |
62
|
|
|
{ |
63
|
53 |
|
parent::init(); |
64
|
53 |
|
if ($this->message === null) { |
65
|
52 |
|
$this->message = $this->integerOnly ? Yii::t('yii', '{attribute} must be an integer.') |
66
|
46 |
|
: Yii::t('yii', '{attribute} must be a number.'); |
67
|
|
|
} |
68
|
53 |
|
if ($this->min !== null && $this->tooSmall === null) { |
69
|
31 |
|
$this->tooSmall = Yii::t('yii', '{attribute} must be no less than {min}.'); |
70
|
|
|
} |
71
|
53 |
|
if ($this->max !== null && $this->tooBig === null) { |
72
|
31 |
|
$this->tooBig = Yii::t('yii', '{attribute} must be no greater than {max}.'); |
73
|
|
|
} |
74
|
53 |
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
19 |
|
public function validateAttribute($model, $attribute) |
80
|
|
|
{ |
81
|
19 |
|
$value = $model->$attribute; |
82
|
19 |
|
if ($this->isNotNumber($value)) { |
83
|
3 |
|
$this->addError($model, $attribute, $this->message); |
84
|
3 |
|
return; |
85
|
|
|
} |
86
|
18 |
|
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; |
87
|
|
|
|
88
|
18 |
|
if (!preg_match($pattern, StringHelper::normalizeNumber($value))) { |
89
|
5 |
|
$this->addError($model, $attribute, $this->message); |
90
|
|
|
} |
91
|
18 |
|
if ($this->min !== null && $value < $this->min) { |
92
|
2 |
|
$this->addError($model, $attribute, $this->tooSmall, ['min' => $this->min]); |
93
|
|
|
} |
94
|
18 |
|
if ($this->max !== null && $value > $this->max) { |
95
|
1 |
|
$this->addError($model, $attribute, $this->tooBig, ['max' => $this->max]); |
96
|
|
|
} |
97
|
18 |
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* {@inheritdoc} |
101
|
|
|
*/ |
102
|
11 |
|
protected function validateValue($value) |
103
|
|
|
{ |
104
|
11 |
|
if ($this->isNotNumber($value)) { |
105
|
2 |
|
return [Yii::t('yii', '{attribute} is invalid.'), []]; |
106
|
|
|
} |
107
|
9 |
|
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern; |
108
|
9 |
|
if (!preg_match($pattern, StringHelper::normalizeNumber($value))) { |
109
|
6 |
|
return [$this->message, []]; |
110
|
8 |
|
} elseif ($this->min !== null && $value < $this->min) { |
111
|
2 |
|
return [$this->tooSmall, ['min' => $this->min]]; |
112
|
8 |
|
} elseif ($this->max !== null && $value > $this->max) { |
113
|
2 |
|
return [$this->tooBig, ['max' => $this->max]]; |
114
|
|
|
} |
115
|
|
|
|
116
|
8 |
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/* |
120
|
|
|
* @param mixed $value the data value to be checked. |
121
|
|
|
*/ |
122
|
27 |
|
private function isNotNumber($value) |
123
|
|
|
{ |
124
|
27 |
|
return is_array($value) |
125
|
27 |
|
|| (is_object($value) && !method_exists($value, '__toString')) |
126
|
27 |
|
|| (!is_object($value) && !is_scalar($value) && !is_null($value)); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|