NumberValidator.php 4.33 KB
Newer Older
w  
Qiang Xue committed
1 2 3
<?php
/**
 * @link http://www.yiiframework.com/
Qiang Xue committed
4
 * @copyright Copyright (c) 2008 Yii Software LLC
w  
Qiang Xue committed
5 6 7
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
8 9
namespace yii\validators;

Qiang Xue committed
10
use Yii;
Qiang Xue committed
11
use yii\web\JsExpression;
12
use yii\helpers\Json;
Qiang Xue committed
13

w  
Qiang Xue committed
14
/**
w  
Qiang Xue committed
15
 * NumberValidator validates that the attribute value is a number.
w  
Qiang Xue committed
16
 *
17
 * The format of the number must match the regular expression specified in [[integerPattern]] or [[numberPattern]].
w  
Qiang Xue committed
18 19 20
 * Optionally, you may configure the [[max]] and [[min]] properties to ensure the number
 * is within certain range.
 *
w  
Qiang Xue committed
21
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
22
 * @since 2.0
w  
Qiang Xue committed
23
 */
w  
Qiang Xue committed
24
class NumberValidator extends Validator
w  
Qiang Xue committed
25
{
Qiang Xue committed
26 27 28 29
	/**
	 * @var boolean whether the attribute value can only be an integer. Defaults to false.
	 */
	public $integerOnly = false;
w  
Qiang Xue committed
30 31 32 33 34 35 36 37 38
	/**
	 * @var integer|float upper limit of the number. Defaults to null, meaning no upper limit.
	 */
	public $max;
	/**
	 * @var integer|float lower limit of the number. Defaults to null, meaning no lower limit.
	 */
	public $min;
	/**
w  
Qiang Xue committed
39
	 * @var string user-defined error message used when the value is bigger than [[max]].
w  
Qiang Xue committed
40 41 42
	 */
	public $tooBig;
	/**
w  
Qiang Xue committed
43
	 * @var string user-defined error message used when the value is smaller than [[min]].
w  
Qiang Xue committed
44 45
	 */
	public $tooSmall;
Qiang Xue committed
46 47 48 49
	/**
	 * @var string the regular expression for matching integers.
	 */
	public $integerPattern = '/^\s*[+-]?\d+\s*$/';
w  
Qiang Xue committed
50
	/**
w  
Qiang Xue committed
51 52
	 * @var string the regular expression for matching numbers. It defaults to a pattern
	 * that matches floating numbers with optional exponential part (e.g. -1.23e-10).
w  
Qiang Xue committed
53
	 */
Qiang Xue committed
54
	public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
w  
Qiang Xue committed
55 56


Qiang Xue committed
57
	/**
Qiang Xue committed
58
	 * @inheritdoc
Qiang Xue committed
59 60 61 62 63
	 */
	public function init()
	{
		parent::init();
		if ($this->message === null) {
64 65
			$this->message = $this->integerOnly ? Yii::t('yii', '{attribute} must be an integer.')
				: Yii::t('yii', '{attribute} must be a number.');
Qiang Xue committed
66 67
		}
		if ($this->min !== null && $this->tooSmall === null) {
68
			$this->tooSmall = Yii::t('yii', '{attribute} must be no less than {min}.');
Qiang Xue committed
69 70
		}
		if ($this->max !== null && $this->tooBig === null) {
71
			$this->tooBig = Yii::t('yii', '{attribute} must be no greater than {max}.');
Qiang Xue committed
72 73 74
		}
	}

w  
Qiang Xue committed
75
	/**
Qiang Xue committed
76
	 * @inheritdoc
w  
Qiang Xue committed
77
	 */
w  
Qiang Xue committed
78
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
79 80
	{
		$value = $object->$attribute;
81
		if (is_array($value)) {
82
			$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
83 84
			return;
		}
Qiang Xue committed
85 86 87
		$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
		if (!preg_match($pattern, "$value")) {
			$this->addError($object, $attribute, $this->message);
w  
Qiang Xue committed
88
		}
w  
Qiang Xue committed
89
		if ($this->min !== null && $value < $this->min) {
90
			$this->addError($object, $attribute, $this->tooSmall, ['min' => $this->min]);
w  
Qiang Xue committed
91
		}
w  
Qiang Xue committed
92
		if ($this->max !== null && $value > $this->max) {
93
			$this->addError($object, $attribute, $this->tooBig, ['max' => $this->max]);
w  
Qiang Xue committed
94 95 96
		}
	}

Qiang Xue committed
97
	/**
Qiang Xue committed
98
	 * @inheritdoc
Qiang Xue committed
99
	 */
Qiang Xue committed
100
	protected function validateValue($value)
Qiang Xue committed
101
	{
Qiang Xue committed
102 103 104 105 106 107 108 109 110 111 112 113 114
		if (is_array($value)) {
			return [Yii::t('yii', '{attribute} is invalid.'), []];
		}
		$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
		if (!preg_match($pattern, "$value")) {
			return [$this->message, []];
		} elseif ($this->min !== null && $value < $this->min) {
			return [$this->tooSmall, ['min' => $this->min]];
		} elseif ($this->max !== null && $value > $this->max) {
			return [$this->tooBig, ['max' => $this->max]];
		} else {
			return null;
		}
Qiang Xue committed
115 116
	}

w  
Qiang Xue committed
117
	/**
Qiang Xue committed
118
	 * @inheritdoc
w  
Qiang Xue committed
119
	 */
120
	public function clientValidateAttribute($object, $attribute, $view)
w  
Qiang Xue committed
121 122
	{
		$label = $object->getAttributeLabel($attribute);
123

Alexander Makarov committed
124
		$options = [
125
			'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern),
126
			'message' => Yii::$app->getI18n()->format($this->message, [
127
				'attribute' => $label,
128
			], Yii::$app->language),
Alexander Makarov committed
129
		];
w  
Qiang Xue committed
130

w  
Qiang Xue committed
131
		if ($this->min !== null) {
132
			$options['min'] = $this->min;
133
			$options['tooSmall'] = Yii::$app->getI18n()->format($this->tooSmall, [
134 135
				'attribute' => $label,
				'min' => $this->min,
136
			], Yii::$app->language);
w  
Qiang Xue committed
137
		}
w  
Qiang Xue committed
138
		if ($this->max !== null) {
139
			$options['max'] = $this->max;
140
			$options['tooBig'] = Yii::$app->getI18n()->format($this->tooBig, [
141 142
				'attribute' => $label,
				'max' => $this->max,
143
			], Yii::$app->language);
w  
Qiang Xue committed
144
		}
Qiang Xue committed
145
		if ($this->skipOnEmpty) {
146
			$options['skipOnEmpty'] = 1;
w  
Qiang Xue committed
147 148
		}

149
		ValidationAsset::register($view);
150
		return 'yii.validation.number(value, messages, ' . Json::encode($options) . ');';
w  
Qiang Xue committed
151
	}
Zander Baldwin committed
152
}