diff --git a/framework/validators/IntegerValidator.php b/framework/validators/IntegerValidator.php new file mode 100644 index 0000000..fcf33c1 --- /dev/null +++ b/framework/validators/IntegerValidator.php @@ -0,0 +1,52 @@ + + * @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); + } +} diff --git a/framework/validators/NumberValidator.php b/framework/validators/NumberValidator.php index 487e802..25e5434 100644 --- a/framework/validators/NumberValidator.php +++ b/framework/validators/NumberValidator.php @@ -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 * @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. + * @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 $integerPattern = '/^\s*[+-]?\d+\s*$/'; - /** - * @var string the regular expression for matching numbers. - */ - public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'; + public $pattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/'; /** @@ -64,17 +61,9 @@ 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")) { - $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a number.'); - $this->addError($object, $attribute, $message); - } + 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}).'); @@ -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) . "); } "; diff --git a/framework/validators/Validator.php b/framework/validators/Validator.php index 1fd3ed5..5a782d3 100644 --- a/framework/validators/Validator.php +++ b/framework/validators/Validator.php @@ -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; } diff --git a/todo.txt b/todo.txt index a4aa0c9..f9c5ddc 100644 --- a/todo.txt +++ b/todo.txt @@ -1,2 +1,3 @@ - CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" -- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD \ No newline at end of file +- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD +- Can consider merging UniqueValidator and ExistValidator and using a NOT property. \ No newline at end of file