Commit 32f27c88 by Qiang Xue

w

parent a5b3932a
<?php
/**
* IntegerValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008-2012 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
/**
* IntegerValidator validates that the attribute value is an integer.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class IntegerValidator extends NumberValidator
{
/**
* @var string the regular expression for matching integers.
*/
public $pattern = '/^\s*[+-]?\d+\s*$/';
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param \yii\base\Model $object the object being validated
* @param string $attribute the attribute being validated
*/
public function validateAttribute($object, $attribute)
{
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} must be an integer.');
}
parent::validateAttribute($object, $attribute);
}
/**
* Returns the JavaScript needed for performing client-side validation.
* @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script.
*/
public function clientValidateAttribute($object, $attribute)
{
if ($this->message === null) {
$this->message = Yii::t('yii', '{attribute} must be an integer.');
}
return parent::clientValidateAttribute($object, $attribute);
}
}
......@@ -12,16 +12,16 @@ namespace yii\validators;
/**
* NumberValidator validates that the attribute value is a number.
*
* 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.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class NumberValidator extends Validator
{
/**
* @var boolean whether the attribute value can only be an integer. Defaults to false.
*/
public $integerOnly = false;
/**
* @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.
*/
......@@ -43,13 +43,10 @@ class NumberValidator extends Validator
*/
public $tooSmall;
/**
* @var string the regular expression for matching integers.
*/
public $integerPattern = '/^\s*[+-]?\d+\s*$/';
/**
* @var string the regular expression for matching numbers.
* @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).
*/
public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
public $pattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
/**
......@@ -64,18 +61,10 @@ class NumberValidator extends Validator
if ($this->allowEmpty && $this->isEmpty($value)) {
return;
}
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")) {
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);
}
}
if ($this->min !== null && $value < $this->min) {
$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));
......@@ -98,7 +87,7 @@ class NumberValidator extends Validator
$value = $object->$attribute;
if (($message = $this->message) === null) {
$message = $this->integerOnly ? Yii::t('yii', '{attribute} must be an integer.') : Yii::t('yii', '{attribute} must be a number.');
$message = Yii::t('yii', '{attribute} must be a number.');
}
$message = strtr($message, array(
'{attribute}' => $label,
......@@ -123,9 +112,8 @@ class NumberValidator extends Validator
'{min}' => $this->min,
));
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
$js = "
if(!value.match($pattern)) {
if(!value.match({$this->pattern})) {
messages.push(" . json_encode($message) . ");
}
";
......
......@@ -66,10 +66,7 @@ abstract class Validator extends \yii\base\Component
'in' => '\yii\validators\RangeValidator',
'boolean' => '\yii\validators\BooleanValidator',
'string' => '\yii\validators\StringValidator',
'integer' => array(
'class' => '\yii\validators\NumberValidator',
'integerOnly' => true,
),
'integer' => '\yii\validators\IntegerValidator',
'double' => '\yii\validators\NumberValidator',
'compare' => '\yii\validators\CompareValidator',
......@@ -166,10 +163,10 @@ abstract class Validator extends \yii\base\Component
'attributes' => $attributes,
);
}
$validator = \Yii::createComponent($config);
foreach ($params as $name => $value) {
$validator->$name = $value;
$config[$name] = $value;
}
$validator = \Yii::createComponent($config);
return $validator;
}
......
- CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
- Can consider merging UniqueValidator and ExistValidator and using a NOT property.
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment