mirror of
https://github.com/yiisoft/yii2.git
synced 2025-08-26 14:26:54 +08:00
refactored validators.
This commit is contained in:
@ -59,6 +59,45 @@ class CompareValidator extends Validator
|
|||||||
*/
|
*/
|
||||||
public $operator = '=';
|
public $operator = '=';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initializes the validator.
|
||||||
|
*/
|
||||||
|
public function init()
|
||||||
|
{
|
||||||
|
parent::init();
|
||||||
|
if ($this->message === null) {
|
||||||
|
switch ($this->operator) {
|
||||||
|
case '==':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be repeated exactly.');
|
||||||
|
break;
|
||||||
|
case '===':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be repeated exactly.');
|
||||||
|
break;
|
||||||
|
case '!=':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
|
||||||
|
break;
|
||||||
|
case '!==':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
|
||||||
|
break;
|
||||||
|
case '>':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be greater than "{compareValue}".');
|
||||||
|
break;
|
||||||
|
case '>=':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be greater than or equal to "{compareValue}".');
|
||||||
|
break;
|
||||||
|
case '<':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be less than "{compareValue}".');
|
||||||
|
break;
|
||||||
|
case '<=':
|
||||||
|
$this->message = Yii::t('yii|{attribute} must be less than or equal to "{compareValue}".');
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new InvalidConfigException("Unknown operator: {$this->operator}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the attribute of the object.
|
* Validates the attribute of the object.
|
||||||
* If there is any error, the error message is added to the object.
|
* If there is any error, the error message is added to the object.
|
||||||
@ -69,6 +108,10 @@ class CompareValidator extends Validator
|
|||||||
public function validateAttribute($object, $attribute)
|
public function validateAttribute($object, $attribute)
|
||||||
{
|
{
|
||||||
$value = $object->$attribute;
|
$value = $object->$attribute;
|
||||||
|
if (is_array($value)) {
|
||||||
|
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
|
||||||
|
return;
|
||||||
|
}
|
||||||
if ($this->compareValue !== null) {
|
if ($this->compareValue !== null) {
|
||||||
$compareLabel = $compareValue = $this->compareValue;
|
$compareLabel = $compareValue = $this->compareValue;
|
||||||
} else {
|
} else {
|
||||||
@ -78,56 +121,21 @@ class CompareValidator extends Validator
|
|||||||
}
|
}
|
||||||
|
|
||||||
switch ($this->operator) {
|
switch ($this->operator) {
|
||||||
case '==':
|
case '==': $valid = $value == $compareValue; break;
|
||||||
if ($value != $compareValue) {
|
case '===': $valid = $value === $compareValue; break;
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.');
|
case '!=': $valid = $value != $compareValue; break;
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel));
|
case '!==': $valid = $value !== $compareValue; break;
|
||||||
}
|
case '>': $valid = $value > $compareValue; break;
|
||||||
break;
|
case '>=': $valid = $value >= $compareValue; break;
|
||||||
case '===':
|
case '<': $valid = $value < $compareValue; break;
|
||||||
if ($value !== $compareValue) {
|
case '<=': $valid = $value <= $compareValue; break;
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be repeated exactly.');
|
default: $valid = false; break;
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel));
|
}
|
||||||
}
|
if (!$valid) {
|
||||||
break;
|
$this->addError($object, $attribute, $this->message, array(
|
||||||
case '!=':
|
'{compareAttribute}' => $compareLabel,
|
||||||
if ($value == $compareValue) {
|
'{compareValue}' => $compareValue,
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
|
));
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '!==':
|
|
||||||
if ($value === $compareValue) {
|
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
|
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '>':
|
|
||||||
if ($value <= $compareValue) {
|
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be greater than "{compareValue}".');
|
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '>=':
|
|
||||||
if ($value < $compareValue) {
|
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be greater than or equal to "{compareValue}".');
|
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '<':
|
|
||||||
if ($value >= $compareValue) {
|
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be less than "{compareValue}".');
|
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case '<=':
|
|
||||||
if ($value > $compareValue) {
|
|
||||||
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be less than or equal to "{compareValue}".');
|
|
||||||
$this->addError($object, $attribute, $message, array('{compareAttribute}' => $compareLabel, '{compareValue}' => $compareValue));
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new InvalidConfigException("Unknown operator: {$this->operator}");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,6 +143,7 @@ class CompareValidator extends Validator
|
|||||||
* Validates the given value.
|
* Validates the given value.
|
||||||
* @param mixed $value the value to be validated.
|
* @param mixed $value the value to be validated.
|
||||||
* @return boolean whether the value is valid.
|
* @return boolean whether the value is valid.
|
||||||
|
* @throws InvalidConfigException if [[compareValue]] is not set.
|
||||||
*/
|
*/
|
||||||
public function validateValue($value)
|
public function validateValue($value)
|
||||||
{
|
{
|
||||||
@ -151,8 +160,6 @@ class CompareValidator extends Validator
|
|||||||
case '>=': return $value >= $this->compareValue;
|
case '>=': return $value >= $this->compareValue;
|
||||||
case '<': return $value < $this->compareValue;
|
case '<': return $value < $this->compareValue;
|
||||||
case '<=': return $value <= $this->compareValue;
|
case '<=': return $value <= $this->compareValue;
|
||||||
default:
|
|
||||||
throw new InvalidConfigException("Unknown operator \"{$this->operator}\"");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,51 +180,8 @@ class CompareValidator extends Validator
|
|||||||
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
|
$compareValue = "\$('#" . (CHtml::activeId($object, $compareAttribute)) . "').val()";
|
||||||
$compareLabel = $object->getAttributeLabel($compareAttribute);
|
$compareLabel = $object->getAttributeLabel($compareAttribute);
|
||||||
}
|
}
|
||||||
|
$condition = "value {$this->operator} $compareValue";
|
||||||
$message = $this->message;
|
$message = strtr($this->message, array(
|
||||||
switch ($this->operator) {
|
|
||||||
case '=':
|
|
||||||
case '==':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must be repeated exactly.');
|
|
||||||
}
|
|
||||||
$condition = 'value!=' . $compareValue;
|
|
||||||
break;
|
|
||||||
case '!=':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must not be equal to "{compareValue}".');
|
|
||||||
}
|
|
||||||
$condition = 'value==' . $compareValue;
|
|
||||||
break;
|
|
||||||
case '>':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must be greater than "{compareValue}".');
|
|
||||||
}
|
|
||||||
$condition = 'value<=' . $compareValue;
|
|
||||||
break;
|
|
||||||
case '>=':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must be greater than or equal to "{compareValue}".');
|
|
||||||
}
|
|
||||||
$condition = 'value<' . $compareValue;
|
|
||||||
break;
|
|
||||||
case '<':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must be less than "{compareValue}".');
|
|
||||||
}
|
|
||||||
$condition = 'value>=' . $compareValue;
|
|
||||||
break;
|
|
||||||
case '<=':
|
|
||||||
if ($message === null) {
|
|
||||||
$message = Yii::t('yii|{attribute} must be less than or equal to "{compareValue}".');
|
|
||||||
}
|
|
||||||
$condition = 'value>' . $compareValue;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
throw new InvalidConfigException("Unknown operator: {$this->operator}");
|
|
||||||
}
|
|
||||||
|
|
||||||
$message = strtr($message, array(
|
|
||||||
'{attribute}' => $object->getAttributeLabel($attribute),
|
'{attribute}' => $object->getAttributeLabel($attribute),
|
||||||
'{compareValue}' => $compareLabel,
|
'{compareValue}' => $compareLabel,
|
||||||
));
|
));
|
||||||
|
@ -52,6 +52,24 @@ class NumberValidator extends Validator
|
|||||||
public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
|
public $numberPattern = '/^\s*[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?\s*$/';
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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}.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validates the attribute of the object.
|
* Validates the attribute of the object.
|
||||||
* If there is any error, the error message is added to the object.
|
* If there is any error, the error message is added to the object.
|
||||||
@ -65,24 +83,15 @@ class NumberValidator extends Validator
|
|||||||
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
|
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if ($this->integerOnly) {
|
$pattern = $this->integerOnly ? $this->integerPattern : $this->numberPattern;
|
||||||
if (!preg_match($this->integerPattern, "$value")) {
|
if (!preg_match($pattern, "$value")) {
|
||||||
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be an integer.');
|
$this->addError($object, $attribute, $this->message);
|
||||||
$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 ($this->min !== null && $value < $this->min) {
|
if ($this->min !== null && $value < $this->min) {
|
||||||
$message = $this->tooSmall !== null ? $this->tooSmall : Yii::t('yii|{attribute} must be no less than {min}.');
|
$this->addError($object, $attribute, $this->tooSmall, array('{min}' => $this->min));
|
||||||
$this->addError($object, $attribute, $message, array('{min}' => $this->min));
|
|
||||||
}
|
}
|
||||||
if ($this->max !== null && $value > $this->max) {
|
if ($this->max !== null && $value > $this->max) {
|
||||||
$message = $this->tooBig !== null ? $this->tooBig : Yii::t('yii|{attribute} must be no greater than {max}.');
|
$this->addError($object, $attribute, $this->tooBig, array('{max}' => $this->max));
|
||||||
$this->addError($object, $attribute, $message, array('{max}' => $this->max));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -107,12 +116,7 @@ class NumberValidator extends Validator
|
|||||||
public function clientValidateAttribute($object, $attribute)
|
public function clientValidateAttribute($object, $attribute)
|
||||||
{
|
{
|
||||||
$label = $object->getAttributeLabel($attribute);
|
$label = $object->getAttributeLabel($attribute);
|
||||||
|
$message = strtr($this->message, array(
|
||||||
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 = strtr($message, array(
|
|
||||||
'{attribute}' => $label,
|
'{attribute}' => $label,
|
||||||
));
|
));
|
||||||
|
|
||||||
@ -123,10 +127,7 @@ if(!value.match($pattern)) {
|
|||||||
}
|
}
|
||||||
";
|
";
|
||||||
if ($this->min !== null) {
|
if ($this->min !== null) {
|
||||||
if (($tooSmall = $this->tooSmall) === null) {
|
$tooSmall = strtr($this->tooSmall, array(
|
||||||
$tooSmall = Yii::t('yii|{attribute} must be no less than {min}.');
|
|
||||||
}
|
|
||||||
$tooSmall = strtr($tooSmall, array(
|
|
||||||
'{attribute}' => $label,
|
'{attribute}' => $label,
|
||||||
'{min}' => $this->min,
|
'{min}' => $this->min,
|
||||||
));
|
));
|
||||||
@ -138,10 +139,7 @@ if(value<{$this->min}) {
|
|||||||
";
|
";
|
||||||
}
|
}
|
||||||
if ($this->max !== null) {
|
if ($this->max !== null) {
|
||||||
if (($tooBig = $this->tooBig) === null) {
|
$tooBig = strtr($this->tooBig, array(
|
||||||
$tooBig = Yii::t('yii|{attribute} must be no greater than {max}.');
|
|
||||||
}
|
|
||||||
$tooBig = strtr($tooBig, array(
|
|
||||||
'{attribute}' => $label,
|
'{attribute}' => $label,
|
||||||
'{max}' => $this->max,
|
'{max}' => $this->max,
|
||||||
));
|
));
|
||||||
|
@ -47,7 +47,8 @@ class RequiredValidator extends Validator
|
|||||||
{
|
{
|
||||||
parent::init();
|
parent::init();
|
||||||
if ($this->message === null) {
|
if ($this->message === null) {
|
||||||
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} is invalid.') : Yii::t('yii|{attribute} must be "{requiredValue}".');
|
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} is invalid.')
|
||||||
|
: Yii::t('yii|{attribute} must be "{requiredValue}".');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -245,8 +245,9 @@ abstract class Validator extends Component
|
|||||||
*/
|
*/
|
||||||
public function addError($object, $attribute, $message, $params = array())
|
public function addError($object, $attribute, $message, $params = array())
|
||||||
{
|
{
|
||||||
|
$value = $object->$attribute;
|
||||||
$params['{attribute}'] = $object->getAttributeLabel($attribute);
|
$params['{attribute}'] = $object->getAttributeLabel($attribute);
|
||||||
$params['{value}'] = $object->$attribute;
|
$params['{value}'] = is_array($value) ? 'array()' : $value;
|
||||||
$object->addError($attribute, strtr($message, $params));
|
$object->addError($attribute, strtr($message, $params));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user