NumberValidator.php 4.49 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
	/**
	 * @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
37
	 * @var string user-defined error message used when the value is bigger than [[max]].
w  
Qiang Xue committed
38 39 40
	 */
	public $tooBig;
	/**
w  
Qiang Xue committed
41
	 * @var string user-defined error message used when the value is smaller than [[min]].
w  
Qiang Xue committed
42 43
	 */
	public $tooSmall;
Qiang Xue committed
44 45 46 47
	/**
	 * @var string the regular expression for matching integers.
	 */
	public $integerPattern = '/^\s*[+-]?\d+\s*$/';
w  
Qiang Xue committed
48
	/**
w  
Qiang Xue committed
49 50
	 * @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
51
	 */
Qiang Xue committed
52
	public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
w  
Qiang Xue committed
53 54


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

w  
Qiang Xue committed
73 74 75
	/**
	 * Validates the attribute of the object.
	 * If there is any error, the error message is added to the object.
w  
Qiang Xue committed
76
	 * @param \yii\base\Model $object the object being validated
w  
Qiang Xue committed
77 78
	 * @param string $attribute the attribute being validated
	 */
w  
Qiang Xue committed
79
	public function validateAttribute($object, $attribute)
w  
Qiang Xue committed
80 81
	{
		$value = $object->$attribute;
82 83 84 85
		if (is_array($value)) {
			$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
			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) {
Qiang Xue committed
91
			$this->addError($object, $attribute, $this->tooSmall, array('{min}' => $this->min));
w  
Qiang Xue committed
92
		}
w  
Qiang Xue committed
93
		if ($this->max !== null && $value > $this->max) {
Qiang Xue committed
94
			$this->addError($object, $attribute, $this->tooBig, array('{max}' => $this->max));
w  
Qiang Xue committed
95 96 97
		}
	}

Qiang Xue committed
98 99 100 101 102 103 104 105 106 107 108 109
	/**
	 * Validates the given value.
	 * @param mixed $value the value to be validated.
	 * @return boolean whether the value is valid.
	 */
	public function validateValue($value)
	{
		return preg_match($this->integerOnly ? $this->integerPattern : $this->numberPattern, "$value")
			&& ($this->min === null || $value >= $this->min)
			&& ($this->max === null || $value <= $this->max);
	}

w  
Qiang Xue committed
110 111
	/**
	 * Returns the JavaScript needed for performing client-side validation.
w  
Qiang Xue committed
112
	 * @param \yii\base\Model $object the data object being validated
w  
Qiang Xue committed
113 114 115 116 117 118
	 * @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);
Qiang Xue committed
119
		$message = strtr($this->message, array(
w  
Qiang Xue committed
120 121 122
			'{attribute}' => $label,
		));

Qiang Xue committed
123
		$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
w  
Qiang Xue committed
124
		$js = "
Qiang Xue committed
125
if(!value.match($pattern)) {
w  
Qiang Xue committed
126
	messages.push(" . json_encode($message) . ");
w  
Qiang Xue committed
127 128
}
";
w  
Qiang Xue committed
129
		if ($this->min !== null) {
Qiang Xue committed
130
			$tooSmall = strtr($this->tooSmall, array(
Qiang Xue committed
131 132 133 134
				'{attribute}' => $label,
				'{min}' => $this->min,
			));

w  
Qiang Xue committed
135
			$js .= "
Qiang Xue committed
136
if(value<{$this->min}) {
w  
Qiang Xue committed
137
	messages.push(" . json_encode($tooSmall) . ");
w  
Qiang Xue committed
138 139 140
}
";
		}
w  
Qiang Xue committed
141
		if ($this->max !== null) {
Qiang Xue committed
142
			$tooBig = strtr($this->tooBig, array(
Qiang Xue committed
143 144 145
				'{attribute}' => $label,
				'{max}' => $this->max,
			));
w  
Qiang Xue committed
146
			$js .= "
Qiang Xue committed
147
if(value>{$this->max}) {
w  
Qiang Xue committed
148
	messages.push(" . json_encode($tooBig) . ");
w  
Qiang Xue committed
149 150 151 152
}
";
		}

Qiang Xue committed
153
		if ($this->skipOnEmpty) {
w  
Qiang Xue committed
154
			$js = "
Qiang Xue committed
155
if(jQuery.trim(value)!='') {
w  
Qiang Xue committed
156 157 158 159 160 161 162
	$js
}
";
		}

		return $js;
	}
Qiang Xue committed
163
}