Yii2 framework backup
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
4.6 KiB

13 years ago
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
13 years ago
* @license http://www.yiiframework.com/license/
*/
13 years ago
namespace yii\validators;
use Yii;
13 years ago
/**
13 years ago
* NumberValidator validates that the attribute value is a number.
13 years ago
*
13 years ago
* 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.
*
13 years ago
* @author Qiang Xue <qiang.xue@gmail.com>
13 years ago
* @since 2.0
13 years ago
*/
13 years ago
class NumberValidator extends Validator
13 years ago
{
/**
* @var boolean whether the attribute value can only be an integer. Defaults to false.
*/
public $integerOnly = false;
/**
13 years ago
* @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;
/**
13 years ago
* @var string user-defined error message used when the value is bigger than [[max]].
13 years ago
*/
public $tooBig;
/**
13 years ago
* @var string user-defined error message used when the value is smaller than [[min]].
13 years ago
*/
public $tooSmall;
/**
* @var string the regular expression for matching integers.
*/
public $integerPattern = '/^\s*[+-]?\d+\s*$/';
/**
13 years ago
* @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).
13 years ago
*/
public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
13 years ago
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
13 years ago
* @param \yii\base\Model $object the object being validated
13 years ago
* @param string $attribute the attribute being validated
*/
13 years ago
public function validateAttribute($object, $attribute)
13 years ago
{
$value = $object->$attribute;
13 years ago
if ($this->allowEmpty && $this->isEmpty($value)) {
13 years ago
return;
13 years ago
}
if ($this->integerOnly) {
if (!preg_match($this->integerPattern, "$value")) {
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.');
$this->addError($object, $attribute, $message);
}
} else {
if (!preg_match($this->numberPattern, "$value")) {
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be a number.');
$this->addError($object, $attribute, $message);
}
13 years ago
}
13 years ago
if ($this->min !== null && $value < $this->min) {
$message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii|{attribute} is too small (minimum is {min}).');
13 years ago
$this->addError($object, $attribute, $message, array('{min}' => $this->min));
}
13 years ago
if ($this->max !== null && $value > $this->max) {
$message = $this->tooBig !== null ? $this->tooBig : Yii::t('yii|{attribute} is too big (maximum is {max}).');
13 years ago
$this->addError($object, $attribute, $message, array('{max}' => $this->max));
}
}
/**
* Returns the JavaScript needed for performing client-side validation.
13 years ago
* @param \yii\base\Model $object the data object being validated
13 years ago
* @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);
13 years ago
if (($message = $this->message) === null) {
$message = $this->integerOnly ? Yii::t('yii|{attribute} must be an integer.')
: Yii::t('yii|{attribute} must be a number.');
13 years ago
}
13 years ago
$message = strtr($message, array(
'{attribute}' => $label,
));
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
13 years ago
$js = "
if(!value.match($pattern)) {
13 years ago
messages.push(" . json_encode($message) . ");
13 years ago
}
";
13 years ago
if ($this->min !== null) {
if (($tooSmall = $this->tooSmall) === null) {
$tooSmall = Yii::t('yii|{attribute} is too small (minimum is {min}).');
}
$tooSmall = strtr($tooSmall, array(
'{attribute}' => $label,
'{min}' => $this->min,
));
13 years ago
$js .= "
if(value<{$this->min}) {
13 years ago
messages.push(" . json_encode($tooSmall) . ");
13 years ago
}
";
}
13 years ago
if ($this->max !== null) {
if (($tooBig = $this->tooBig) === null) {
$tooBig = Yii::t('yii|{attribute} is too big (maximum is {max}).');
}
$tooBig = strtr($tooBig, array(
'{attribute}' => $label,
'{max}' => $this->max,
));
13 years ago
$js .= "
if(value>{$this->max}) {
13 years ago
messages.push(" . json_encode($tooBig) . ");
13 years ago
}
";
}
13 years ago
if ($this->allowEmpty) {
13 years ago
$js = "
if(jQuery.trim(value)!='') {
13 years ago
$js
}
";
}
return $js;
}
}