NumberValidator.php 4.21 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;
11
use yii\helpers\Html;
Qiang Xue committed
12
use yii\web\JsExpression;
13
use yii\helpers\Json;
Qiang Xue committed
14

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


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

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

Qiang Xue committed
98
	/**
Qiang Xue committed
99
	 * @inheritdoc
Qiang Xue committed
100
	 */
Qiang Xue committed
101
	protected function validateValue($value)
Qiang Xue committed
102
	{
Qiang Xue committed
103 104 105 106 107 108 109 110 111 112 113 114 115
		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
116 117
	}

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

Alexander Makarov committed
125
		$options = [
126
			'pattern' => new JsExpression($this->integerOnly ? $this->integerPattern : $this->numberPattern),
127
			'message' => strtr($this->message, [
128
				'{attribute}' => $label,
129
			]),
Alexander Makarov committed
130
		];
w  
Qiang Xue committed
131

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

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