Browse Source

w

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
32f27c8851
  1. 52
      framework/validators/IntegerValidator.php
  2. 32
      framework/validators/NumberValidator.php
  3. 9
      framework/validators/Validator.php
  4. 1
      todo.txt

52
framework/validators/IntegerValidator.php

@ -0,0 +1,52 @@
<?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);
}
}

32
framework/validators/NumberValidator.php

@ -12,16 +12,16 @@ namespace yii\validators;
/** /**
* NumberValidator validates that the attribute value is a number. * 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> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
class NumberValidator extends Validator 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, * @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. * meaning that if the attribute is empty, it is considered valid.
*/ */
@ -43,13 +43,10 @@ class NumberValidator extends Validator
*/ */
public $tooSmall; 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,18 +61,10 @@ class NumberValidator extends Validator
if ($this->allowEmpty && $this->isEmpty($value)) { if ($this->allowEmpty && $this->isEmpty($value)) {
return; return;
} }
if ($this->integerOnly) { if (!preg_match($this->pattern, "$value")) {
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.'); $message = $this->message !== null ? $this->message : Yii::t('yii', '{attribute} must be a number.');
$this->addError($object, $attribute, $message); $this->addError($object, $attribute, $message);
} }
}
if ($this->min !== null && $value < $this->min) { if ($this->min !== null && $value < $this->min) {
$message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii', '{attribute} is too small (minimum is {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)); $this->addError($object, $attribute, $message, array('{min}' => $this->min));
@ -98,7 +87,7 @@ class NumberValidator extends Validator
$value = $object->$attribute; $value = $object->$attribute;
if (($message = $this->message) === null) { 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( $message = strtr($message, array(
'{attribute}' => $label, '{attribute}' => $label,
@ -123,9 +112,8 @@ class NumberValidator extends Validator
'{min}' => $this->min, '{min}' => $this->min,
)); ));
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
$js = " $js = "
if(!value.match($pattern)) { if(!value.match({$this->pattern})) {
messages.push(" . json_encode($message) . "); messages.push(" . json_encode($message) . ");
} }
"; ";

9
framework/validators/Validator.php

@ -66,10 +66,7 @@ abstract class Validator extends \yii\base\Component
'in' => '\yii\validators\RangeValidator', 'in' => '\yii\validators\RangeValidator',
'boolean' => '\yii\validators\BooleanValidator', 'boolean' => '\yii\validators\BooleanValidator',
'string' => '\yii\validators\StringValidator', 'string' => '\yii\validators\StringValidator',
'integer' => array( 'integer' => '\yii\validators\IntegerValidator',
'class' => '\yii\validators\NumberValidator',
'integerOnly' => true,
),
'double' => '\yii\validators\NumberValidator', 'double' => '\yii\validators\NumberValidator',
'compare' => '\yii\validators\CompareValidator', 'compare' => '\yii\validators\CompareValidator',
@ -166,10 +163,10 @@ abstract class Validator extends \yii\base\Component
'attributes' => $attributes, 'attributes' => $attributes,
); );
} }
$validator = \Yii::createComponent($config);
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
$validator->$name = $value; $config[$name] = $value;
} }
$validator = \Yii::createComponent($config);
return $validator; return $validator;
} }

1
todo.txt

@ -1,2 +1,3 @@
- CompareValidator::clientValidateAttribute(): search for "CHtml::activeId" - CompareValidator::clientValidateAttribute(): search for "CHtml::activeId"
- FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD - FileValidator, UniqueValidator, ExistValidator, DateValidator: TBD
- Can consider merging UniqueValidator and ExistValidator and using a NOT property.
Loading…
Cancel
Save