NumberValidator.php 4.63 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 11
use Yii;

w  
Qiang Xue committed
12
/**
w  
Qiang Xue committed
13
 * NumberValidator validates that the attribute value is a number.
w  
Qiang Xue committed
14
 *
w  
Qiang Xue committed
15 16 17 18
 * 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
19
 * @author Qiang Xue <qiang.xue@gmail.com>
w  
Qiang Xue committed
20
 * @since 2.0
w  
Qiang Xue committed
21
 */
w  
Qiang Xue committed
22
class NumberValidator extends Validator
w  
Qiang Xue committed
23
{
Qiang Xue committed
24 25 26 27
	/**
	 * @var boolean whether the attribute value can only be an integer. Defaults to false.
	 */
	public $integerOnly = false;
w  
Qiang Xue committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
	/**
	 * @var boolean whether the attribute value can be null or empty. Defaults to true,
	 * meaning that if the attribute is empty, it is considered valid.
	 */
	public $allowEmpty = true;
	/**
	 * @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
42
	 * @var string user-defined error message used when the value is bigger than [[max]].
w  
Qiang Xue committed
43 44 45
	 */
	public $tooBig;
	/**
w  
Qiang Xue committed
46
	 * @var string user-defined error message used when the value is smaller than [[min]].
w  
Qiang Xue committed
47 48
	 */
	public $tooSmall;
Qiang Xue committed
49 50 51 52
	/**
	 * @var string the regular expression for matching integers.
	 */
	public $integerPattern = '/^\s*[+-]?\d+\s*$/';
w  
Qiang Xue committed
53
	/**
w  
Qiang Xue committed
54 55
	 * @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
56
	 */
Qiang Xue committed
57
	public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
w  
Qiang Xue committed
58 59 60 61 62


	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
63
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
64 65
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
66
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
67 68
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
69
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
70
			return;
w  
Qiang Xue committed
71
		}
Qiang Xue committed
72 73
		if ($this->integerOnly) {
			if (!preg_match($this->integerPattern, "$value")) {
74
				$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.');
Qiang Xue committed
75 76 77 78
				$this->addError($object, $attribute, $message);
			}
		} else {
			if (!preg_match($this->numberPattern, "$value")) {
79
				$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be a number.');
Qiang Xue committed
80 81
				$this->addError($object, $attribute, $message);
			}
w  
Qiang Xue committed
82
		}
w  
Qiang Xue committed
83
		if ($this->min !== null && $value < $this->min) {
84
			$message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii|{attribute} is too small (minimum is {min}).');
w  
Qiang Xue committed
85 86
			$this->addError($object, $attribute, $message, array('{min}' => $this->min));
		}
w  
Qiang Xue committed
87
		if ($this->max !== null && $value > $this->max) {
88
			$message = $this->tooBig !== null ? $this->tooBig : Yii::t('yii|{attribute} is too big (maximum is {max}).');
w  
Qiang Xue committed
89 90 91 92 93 94
			$this->addError($object, $attribute, $message, array('{max}' => $this->max));
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
95
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
96 97 98 99 100 101 102
	 * @param string $attribute the name of the attribute to be validated.
	 * @return string the client-side validation script.
	 */
	public function clientValidateAttribute($object, $attribute)
	{
		$label = $object->getAttributeLabel($attribute);

w  
Qiang Xue committed
103
		if (($message = $this->message) === null) {
104 105
			$message = $this->integerOnly ? Yii::t('yii|{attribute} must be an integer.')
					: Yii::t('yii|{attribute} must be a number.');
w  
Qiang Xue committed
106
		}
w  
Qiang Xue committed
107 108 109 110
		$message = strtr($message, array(
			'{attribute}' => $label,
		));

Qiang Xue committed
111
		$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
w  
Qiang Xue committed
112
		$js = "
Qiang Xue committed
113
if(!value.match($pattern)) {
w  
Qiang Xue committed
114
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
115 116
}
";
w  
Qiang Xue committed
117
		if ($this->min !== null) {
Qiang Xue committed
118
			if (($tooSmall = $this->tooSmall) === null) {
119
				$tooSmall = Yii::t('yii|{attribute} is too small (minimum is {min}).');
Qiang Xue committed
120 121 122 123 124 125
			}
			$tooSmall = strtr($tooSmall, array(
				'{attribute}' => $label,
				'{min}' => $this->min,
			));

w  
Qiang Xue committed
126
			$js .= "
Qiang Xue committed
127
if(value<{$this->min}) {
w  
Qiang Xue committed
128
	messages.push(" . json_encode($tooSmall) . ");
w  
Qiang Xue committed
129 130 131
}
";
		}
w  
Qiang Xue committed
132
		if ($this->max !== null) {
Qiang Xue committed
133
			if (($tooBig = $this->tooBig) === null) {
134
				$tooBig = Yii::t('yii|{attribute} is too big (maximum is {max}).');
Qiang Xue committed
135 136 137 138 139
			}
			$tooBig = strtr($tooBig, array(
				'{attribute}' => $label,
				'{max}' => $this->max,
			));
w  
Qiang Xue committed
140
			$js .= "
Qiang Xue committed
141
if(value>{$this->max}) {
w  
Qiang Xue committed
142
	messages.push(" . json_encode($tooBig) . ");
w  
Qiang Xue committed
143 144 145 146
}
";
		}

w  
Qiang Xue committed
147
		if ($this->allowEmpty) {
w  
Qiang Xue committed
148
			$js = "
Qiang Xue committed
149
if(jQuery.trim(value)!='') {
w  
Qiang Xue committed
150 151 152 153 154 155 156
	$js
}
";
		}

		return $js;
	}
Qiang Xue committed
157
}