Browse Source

refacotring validators.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
ae6c69fc8f
  1. 19
      framework/validators/BooleanValidator.php
  2. 19
      framework/validators/CaptchaValidator.php
  3. 2
      framework/validators/CompareValidator.php
  4. 18
      framework/validators/DateValidator.php
  5. 21
      framework/validators/EmailValidator.php
  6. 17
      framework/validators/ExistValidator.php
  7. 2
      framework/validators/NumberValidator.php
  8. 17
      framework/validators/RangeValidator.php
  9. 12
      framework/validators/RegularExpressionValidator.php
  10. 30
      framework/validators/RequiredValidator.php
  11. 41
      framework/validators/StringValidator.php
  12. 18
      framework/validators/UniqueValidator.php
  13. 22
      framework/validators/UrlValidator.php

19
framework/validators/BooleanValidator.php

@ -36,6 +36,17 @@ class BooleanValidator extends Validator
public $strict = false;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} must be either "{true}" or "{false}".');
}
}
/**
* 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
@ -45,8 +56,7 @@ class BooleanValidator extends Validator
{
$value = $object->$attribute;
if (!$this->validateValue($value)) {
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} must be either "{true}" or "{false}".');
$this->addError($object, $attribute, $message, array(
$this->addError($object, $attribute, $this->message, array(
'{true}' => $this->trueValue,
'{false}' => $this->falseValue,
));
@ -72,15 +82,14 @@ class BooleanValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be either "{true}" or "{false}".');
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
'{true}' => $this->trueValue,
'{false}' => $this->falseValue,
));
return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") {
if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . "value!=" . json_encode($this->trueValue) . " && value!=" . json_encode($this->falseValue) . ") {
messages.push(" . json_encode($message) . ");
}
";

19
framework/validators/CaptchaValidator.php

@ -31,6 +31,17 @@ class CaptchaValidator extends Validator
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|The verification code is incorrect.');
}
}
/**
* 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
@ -40,8 +51,7 @@ class CaptchaValidator extends Validator
{
$value = $object->$attribute;
if (!$this->validateValue($value)) {
$message = $this->message !== null ? $this->message : Yii::t('yii|The verification code is incorrect.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
@ -83,8 +93,7 @@ class CaptchaValidator extends Validator
public function clientValidateAttribute($object, $attribute)
{
$captcha = $this->getCaptchaAction();
$message = $this->message !== null ? $this->message : \Yii::t('yii|The verification code is incorrect.');
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
@ -102,7 +111,7 @@ if(h != hash) {
}
";
if ($this->allowEmpty) {
if ($this->skipOnEmpty) {
$js = "
if($.trim(value)!='') {
$js

2
framework/validators/CompareValidator.php

@ -223,7 +223,7 @@ class CompareValidator extends Validator
));
return "
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . json_encode($message) . ");
}
";

18
framework/validators/DateValidator.php

@ -32,6 +32,17 @@ class DateValidator extends Validator
public $timestampAttribute;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|The format of {attribute} is invalid.');
}
}
/**
* 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
@ -40,10 +51,13 @@ class DateValidator extends Validator
public function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, $this->message);
return;
}
$date = DateTime::createFromFormat($this->format, $value);
if ($date === false) {
$message = $this->message !== null ? $this->message : Yii::t('yii|The format of {attribute} is invalid.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
} elseif ($this->timestampAttribute !== false) {
$object->{$this->timestampAttribute} = $date->getTimestamp();
}

21
framework/validators/EmailValidator.php

@ -7,6 +7,8 @@
namespace yii\validators;
use Yii;
/**
* EmailValidator validates that the attribute value is a valid email address.
*
@ -44,6 +46,17 @@ class EmailValidator extends Validator
public $checkPort = false;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is not a valid email address.');
}
}
/**
* 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
@ -53,8 +66,7 @@ class EmailValidator extends Validator
{
$value = $object->$attribute;
if (!$this->validateValue($value)) {
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid email address.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
@ -88,8 +100,7 @@ class EmailValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid email address.');
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
@ -100,7 +111,7 @@ class EmailValidator extends Validator
}
return "
if(" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
if(" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . $condition . ") {
messages.push(" . json_encode($message) . ");
}
";

17
framework/validators/ExistValidator.php

@ -37,6 +37,18 @@ class ExistValidator extends Validator
*/
public $attributeName;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
}
}
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
@ -49,7 +61,7 @@ class ExistValidator extends Validator
$value = $object->$attribute;
if (is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii|{attribute} is invalid.'));
$this->addError($object, $attribute, $this->message);
return;
}
@ -59,8 +71,7 @@ class ExistValidator extends Validator
$query = $className::find();
$query->where(array($attributeName => $value));
if (!$query->exists()) {
$message = $this->message !== null ? $this->message : Yii::t('yii|{attribute} "{value}" is invalid.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}

2
framework/validators/NumberValidator.php

@ -152,7 +152,7 @@ if(value>{$this->max}) {
";
}
if ($this->allowEmpty) {
if ($this->skipOnEmpty) {
$js = "
if(jQuery.trim(value)!='') {
$js

17
framework/validators/RangeValidator.php

@ -6,6 +6,8 @@
*/
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
/**
@ -44,6 +46,9 @@ class RangeValidator extends Validator
if (!is_array($this->range)) {
throw new InvalidConfigException('The "range" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
}
}
/**
@ -55,11 +60,10 @@ class RangeValidator extends Validator
public function validateAttribute($object, $attribute)
{
$value = $object->$attribute;
$message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} is invalid.');
if (!$this->not && !in_array($value, $this->range, $this->strict)) {
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
} elseif ($this->not && in_array($value, $this->range, $this->strict)) {
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
@ -82,10 +86,7 @@ class RangeValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
if (($message = $this->message) === null) {
$message = \Yii::t('yii|{attribute} is invalid.');
}
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
@ -97,7 +98,7 @@ class RangeValidator extends Validator
$range = json_encode($range);
return "
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") {
if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? "$.inArray(value, $range)>=0" : "$.inArray(value, $range)<0") . ") {
messages.push(" . json_encode($message) . ");
}
";

12
framework/validators/RegularExpressionValidator.php

@ -7,6 +7,7 @@
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
/**
@ -40,6 +41,9 @@ class RegularExpressionValidator extends Validator
if ($this->pattern === null) {
throw new InvalidConfigException('The "pattern" property must be set.');
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is invalid.');
}
}
/**
@ -52,8 +56,7 @@ class RegularExpressionValidator extends Validator
{
$value = $object->$attribute;
if (!$this->validateValue($value)) {
$message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} is invalid.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
@ -78,8 +81,7 @@ class RegularExpressionValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is invalid.');
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
@ -99,7 +101,7 @@ class RegularExpressionValidator extends Validator
}
return "
if (" . ($this->allowEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) {
if (" . ($this->skipOnEmpty ? "$.trim(value)!='' && " : '') . ($this->not ? '' : '!') . "value.match($pattern)) {
messages.push(" . json_encode($message) . ");
}
";

30
framework/validators/RequiredValidator.php

@ -7,6 +7,8 @@
namespace yii\validators;
use Yii;
/**
* RequiredValidator validates that the specified attribute does not have null or empty value.
*
@ -39,6 +41,17 @@ class RequiredValidator extends Validator
public $strict = false;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = $this->requiredValue === null ? Yii::t('yii|{attribute} is invalid.') : Yii::t('yii|{attribute} must be "{requiredValue}".');
}
}
/**
* 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
@ -49,13 +62,11 @@ class RequiredValidator extends Validator
$value = $object->$attribute;
if ($this->requiredValue === null) {
if ($this->strict && $value === null || !$this->strict && $this->isEmpty($value, true)) {
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} cannot be blank.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
} else {
if (!$this->strict && $value != $this->requiredValue || $this->strict && $value !== $this->requiredValue) {
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} must be "{requiredValue}".');
$this->addError($object, $attribute, $message, array(
$this->addError($object, $attribute, $this->message, array(
'{requiredValue}' => $this->requiredValue,
));
}
@ -87,12 +98,8 @@ class RequiredValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
$message = $this->message;
if ($this->requiredValue !== null) {
if ($message === null) {
$message = \Yii::t('yii|{attribute} must be "{requiredValue}".');
}
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
'{requiredValue}' => $this->requiredValue,
@ -103,10 +110,7 @@ if (value != " . json_encode($this->requiredValue) . ") {
}
";
} else {
if ($message === null) {
$message = \Yii::t('yii|{attribute} cannot be blank.');
}
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));

41
framework/validators/StringValidator.php

@ -63,6 +63,18 @@ class StringValidator extends Validator
if ($this->encoding === null) {
$this->encoding = Yii::$app->charset;
}
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} must be a string.');
}
if ($this->min !== null && $this->tooShort === null) {
$this->tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.');
}
if ($this->max !== null && $this->tooLong === null) {
$this->tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.');
}
if ($this->is !== null && $this->notEqual === null) {
$this->notEqual = Yii::t('yii|{attribute} should contain {length} characters.');
}
}
/**
@ -76,24 +88,20 @@ class StringValidator extends Validator
$value = $object->$attribute;
if (!is_string($value)) {
$message = ($this->message !== null) ? $this->message : Yii::t('yii|{attribute} must be a string.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
return;
}
$length = mb_strlen($value, $this->encoding);
if ($this->min !== null && $length < $this->min) {
$message = ($this->tooShort !== null) ? $this->tooShort : Yii::t('yii|{attribute} should contain at least {min} characters.');
$this->addError($object, $attribute, $message, array('{min}' => $this->min));
$this->addError($object, $attribute, $this->tooShort, array('{min}' => $this->min));
}
if ($this->max !== null && $length > $this->max) {
$message = ($this->tooLong !== null) ? $this->tooLong : Yii::t('yii|{attribute} should contain at most {max} characters.');
$this->addError($object, $attribute, $message, array('{max}' => $this->max));
$this->addError($object, $attribute, $this->tooLong, array('{max}' => $this->max));
}
if ($this->is !== null && $length !== $this->is) {
$message = ($this->notEqual !== null) ? $this->notEqual : Yii::t('yii|{attribute} should contain {length} characters.');
$this->addError($object, $attribute, $message, array('{length}' => $this->is));
$this->addError($object, $attribute, $this->notEqual, array('{length}' => $this->is));
}
}
@ -124,28 +132,19 @@ class StringValidator extends Validator
$label = $object->getAttributeLabel($attribute);
$value = $object->$attribute;
if (($notEqual = $this->notEqual) === null) {
$notEqual = Yii::t('yii|{attribute} should contain {length} characters.');
}
$notEqual = strtr($notEqual, array(
$notEqual = strtr($this->notEqual, array(
'{attribute}' => $label,
'{value}' => $value,
'{length}' => $this->is,
));
if (($tooShort = $this->tooShort) === null) {
$tooShort = Yii::t('yii|{attribute} should contain at least {min} characters.');
}
$tooShort = strtr($tooShort, array(
$tooShort = strtr($this->tooShort, array(
'{attribute}' => $label,
'{value}' => $value,
'{min}' => $this->min,
));
if (($tooLong = $this->tooLong) === null) {
$tooLong = Yii::t('yii|{attribute} should contain at most {max} characters.');
}
$tooLong = strtr($tooLong, array(
$tooLong = strtr($this->tooLong, array(
'{attribute}' => $label,
'{value}' => $value,
'{max}' => $this->max,
@ -174,7 +173,7 @@ if(value.length!= {$this->is}) {
";
}
if ($this->allowEmpty) {
if ($this->skipOnEmpty) {
$js = "
if($.trim(value)!='') {
$js

18
framework/validators/UniqueValidator.php

@ -6,6 +6,8 @@
*/
namespace yii\validators;
use Yii;
use yii\base\InvalidConfigException;
/**
@ -31,6 +33,17 @@ class UniqueValidator extends Validator
public $attributeName;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} "{value}" has already been taken.');
}
}
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
* @param \yii\db\ActiveRecord $object the object being validated
@ -52,7 +65,7 @@ class UniqueValidator extends Validator
$table = $className::getTableSchema();
if (($column = $table->getColumn($attributeName)) === null) {
throw new InvalidConfigException('Table "' . $table->name . '" does not have a column named "' . $attributeName . '"');
throw new InvalidConfigException("Table '{$table->name}' does not have a column named '$attributeName'.");
}
$query = $className::find();
@ -81,8 +94,7 @@ class UniqueValidator extends Validator
}
if ($exists) {
$message = $this->message !== null ? $this->message : \Yii::t('yii|{attribute} "{value}" has already been taken.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
}

22
framework/validators/UrlValidator.php

@ -7,6 +7,8 @@
namespace yii\validators;
use Yii;
/**
* UrlValidator validates that the attribute value is a valid http or https URL.
*
@ -33,6 +35,18 @@ class UrlValidator extends Validator
**/
public $defaultScheme;
/**
* Initializes the validator.
*/
public function init()
{
parent::init();
if ($this->message === null) {
$this->message = Yii::t('yii|{attribute} is not a valid URL.');
}
}
/**
* Validates the attribute of the object.
* If there is any error, the error message is added to the object.
@ -47,8 +61,7 @@ class UrlValidator extends Validator
$object->$attribute = $this->defaultScheme . '://' . $value;
}
} else {
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.');
$this->addError($object, $attribute, $message);
$this->addError($object, $attribute, $this->message);
}
}
@ -87,8 +100,7 @@ class UrlValidator extends Validator
*/
public function clientValidateAttribute($object, $attribute)
{
$message = ($this->message !== null) ? $this->message : \Yii::t('yii|{attribute} is not a valid URL.');
$message = strtr($message, array(
$message = strtr($this->message, array(
'{attribute}' => $object->getAttributeLabel($attribute),
'{value}' => $object->$attribute,
));
@ -113,7 +125,7 @@ $js
";
}
if ($this->allowEmpty) {
if ($this->skipOnEmpty) {
$js = "
if($.trim(value)!='') {
$js

Loading…
Cancel
Save