NumberValidator.php 4.11 KB
Newer Older
w  
Qiang Xue committed
1 2
<?php
/**
w  
Qiang Xue committed
3
 * NumberValidator class file.
w  
Qiang Xue committed
4 5
 *
 * @link http://www.yiiframework.com/
w  
Qiang Xue committed
6
 * @copyright Copyright &copy; 2008-2012 Yii Software LLC
w  
Qiang Xue committed
7 8 9
 * @license http://www.yiiframework.com/license/
 */

w  
Qiang Xue committed
10 11
namespace yii\validators;

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 24 25 26 27 28 29 30 31 32 33 34 35 36 37
{
	/**
	 * @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
38
	 * @var string user-defined error message used when the value is bigger than [[max]].
w  
Qiang Xue committed
39 40 41
	 */
	public $tooBig;
	/**
w  
Qiang Xue committed
42
	 * @var string user-defined error message used when the value is smaller than [[min]].
w  
Qiang Xue committed
43 44 45
	 */
	public $tooSmall;
	/**
w  
Qiang Xue committed
46 47
	 * @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
48
	 */
w  
Qiang Xue committed
49
	public $pattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
w  
Qiang Xue committed
50 51 52 53 54


	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
55
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
56 57
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
58
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
59 60
	{
		$value = $object->$attribute;
w  
Qiang Xue committed
61
		if ($this->allowEmpty && $this->isEmpty($value)) {
w  
Qiang Xue committed
62
			return;
w  
Qiang Xue committed
63
		}
w  
Qiang Xue committed
64 65 66
		if (!preg_match($this->pattern, "$value")) {
			$message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a number.');
			$this->addError($object, $attribute, $message);
w  
Qiang Xue committed
67
		}
w  
Qiang Xue committed
68
		if ($this->min !== null && $value < $this->min) {
w  
Qiang Xue committed
69 70 71
			$message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', '{attribute} is too small (minimum is {min}).');
			$this->addError($object, $attribute, $message, array('{min}' => $this->min));
		}
w  
Qiang Xue committed
72
		if ($this->max !== null && $value > $this->max) {
w  
Qiang Xue committed
73 74 75 76 77 78 79
			$message = $this->tooBig !== null ? $this->tooBig : Yii::t('yii', '{attribute} is too big (maximum is {max}).');
			$this->addError($object, $attribute, $message, array('{max}' => $this->max));
		}
	}

	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
80
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
81 82 83 84 85 86
	 * @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
87
		$value = $object->$attribute;
w  
Qiang Xue committed
88

w  
Qiang Xue committed
89
		if (($message = $this->message) === null) {
w  
Qiang Xue committed
90
			$message = Yii::t('yii', '{attribute} must be a number.');
w  
Qiang Xue committed
91
		}
w  
Qiang Xue committed
92 93
		$message = strtr($message, array(
			'{attribute}' => $label,
w  
Qiang Xue committed
94
			'{value}' => $value,
w  
Qiang Xue committed
95 96
		));

w  
Qiang Xue committed
97
		if (($tooBig = $this->tooBig) === null) {
w  
Qiang Xue committed
98
			$tooBig = Yii::t('yii', '{attribute} is too big (maximum is {max}).');
w  
Qiang Xue committed
99
		}
w  
Qiang Xue committed
100 101
		$tooBig = strtr($tooBig, array(
			'{attribute}' => $label,
w  
Qiang Xue committed
102
			'{value}' => $value,
w  
Qiang Xue committed
103 104 105
			'{max}' => $this->max,
		));

w  
Qiang Xue committed
106
		if (($tooSmall = $this->tooSmall) === null) {
w  
Qiang Xue committed
107
			$tooSmall = Yii::t('yii', '{attribute} is too small (minimum is {min}).');
w  
Qiang Xue committed
108
		}
w  
Qiang Xue committed
109 110
		$tooSmall = strtr($tooSmall, array(
			'{attribute}' => $label,
w  
Qiang Xue committed
111
			'{value}' => $value,
w  
Qiang Xue committed
112 113 114 115
			'{min}' => $this->min,
		));

		$js = "
w  
Qiang Xue committed
116
if(!value.match({$this->pattern})) {
w  
Qiang Xue committed
117
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
118 119
}
";
w  
Qiang Xue committed
120
		if ($this->min !== null) {
w  
Qiang Xue committed
121 122
			$js .= "
if(value< {$this->min}) {
w  
Qiang Xue committed
123
	messages.push(" . json_encode($tooSmall) . ");
w  
Qiang Xue committed
124 125 126
}
";
		}
w  
Qiang Xue committed
127
		if ($this->max !== null) {
w  
Qiang Xue committed
128 129
			$js .= "
if(value> {$this->max}) {
w  
Qiang Xue committed
130
	messages.push(" . json_encode($tooBig) . ");
w  
Qiang Xue committed
131 132 133 134
}
";
		}

w  
Qiang Xue committed
135
		if ($this->allowEmpty) {
w  
Qiang Xue committed
136 137 138 139 140 141 142 143 144 145
			$js = "
if($.trim(value)!='') {
	$js
}
";
		}

		return $js;
	}
}