Commit 78396afb by Qiang Xue

validator cleanup.

parent 76b153a3
...@@ -88,15 +88,6 @@ class Controller extends Component ...@@ -88,15 +88,6 @@ class Controller extends Component
} }
/** /**
* Initializes the controller.
* This method is called by the application before the controller starts to execute an action.
* You may override this method to perform the needed initialization for the controller.
*/
public function init()
{
}
/**
* Runs the controller with the specified action and parameters. * Runs the controller with the specified action and parameters.
* @param Action|string $action the action to be executed. This can be either an action object * @param Action|string $action the action to be executed. This can be either an action object
* or the ID of the action. * or the ID of the action.
......
...@@ -138,7 +138,7 @@ class Logger extends \yii\base\Component ...@@ -138,7 +138,7 @@ class Logger extends \yii\base\Component
*/ */
public function flush($final = false) public function flush($final = false)
{ {
$this->trigger($final ? 'finalFlush' : 'flush'); $this->trigger($final ? self::EVENT_FINAL_FLUSH : self::EVENT_FLUSH);
$this->messages = array(); $this->messages = array();
} }
......
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
*/ */
namespace yii\validators; namespace yii\validators;
use yii\base\InvalidConfigException;
/** /**
* CUniqueValidator validates that the attribute value is unique in the corresponding database table. * CUniqueValidator validates that the attribute value is unique in the corresponding database table.
...@@ -23,10 +24,9 @@ class UniqueValidator extends Validator ...@@ -23,10 +24,9 @@ class UniqueValidator extends Validator
*/ */
public $allowEmpty = true; public $allowEmpty = true;
/** /**
* @var string the yii\db\ActiveRecord class name or alias of the class * @var string the ActiveRecord class name or alias of the class
* that should be used to look for the attribute value being validated. * that should be used to look for the attribute value being validated.
* Defaults to null, meaning using the yii\db\ActiveRecord class of * Defaults to null, meaning using the ActiveRecord class of the attribute being validated.
* the attribute being validated.
* @see attributeName * @see attributeName
*/ */
public $className; public $className;
...@@ -36,23 +36,13 @@ class UniqueValidator extends Validator ...@@ -36,23 +36,13 @@ class UniqueValidator extends Validator
* meaning using the name of the attribute being validated. * meaning using the name of the attribute being validated.
*/ */
public $attributeName; public $attributeName;
/**
* @var string the user-defined error message. The placeholders "{attribute}" and "{value}"
* are recognized, which will be replaced with the actual attribute name and value, respectively.
*/
public $message;
/**
* @var boolean whether this validation rule should be skipped if when there is already a validation
* error for the current attribute. Defaults to true.
*/
public $skipOnError = true;
/** /**
* 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.
* @param \yii\db\ActiveRecord $object the object being validated * @param \yii\db\ActiveRecord $object the object being validated
* @param string $attribute the attribute being validated * @param string $attribute the attribute being validated
* @throws \yii\base\Exception if table doesn't have column specified * @throws InvalidConfigException if table doesn't have column specified
*/ */
public function validateAttribute($object, $attribute) public function validateAttribute($object, $attribute)
{ {
...@@ -62,12 +52,12 @@ class UniqueValidator extends Validator ...@@ -62,12 +52,12 @@ class UniqueValidator extends Validator
} }
/** @var $className \yii\db\ActiveRecord */ /** @var $className \yii\db\ActiveRecord */
$className = ($this->className === null) ? get_class($object) : \Yii::import($this->className); $className = $this->className === null ? get_class($object) : \Yii::import($this->className);
$attributeName = ($this->attributeName === null) ? $attribute : $this->attributeName; $attributeName = $this->attributeName === null ? $attribute : $this->attributeName;
$table = $className::getTableSchema(); $table = $className::getTableSchema();
if (($column = $table->getColumn($attributeName)) === null) { if (($column = $table->getColumn($attributeName)) === null) {
throw new \yii\base\Exception('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(); $query = $className::find();
...@@ -97,8 +87,8 @@ class UniqueValidator extends Validator ...@@ -97,8 +87,8 @@ class UniqueValidator extends Validator
} }
if ($exists) { if ($exists) {
$message = ($this->message !== null) ? $this->message : \Yii::t('yii', '{attribute} "{value}" has already been taken.'); $message = $this->message !== null ? $this->message : \Yii::t('yii', '{attribute} "{value}" has already been taken.');
$this->addError($object, $attribute, $message, array('{value}' => $value)); $this->addError($object, $attribute, $message);
} }
} }
} }
\ No newline at end of file
<?php
/**
* UnsafeValidator class file.
*
* @link http://www.yiiframework.com/
* @copyright Copyright &copy; 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yii\validators;
/**
* UnsafeValidator marks the associated attributes to be unsafe so that they cannot be massively assigned.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class UnsafeValidator extends Validator
{
/**
* @var boolean whether attributes listed with this validator should be considered safe for massive assignment.
* Defaults to false.
*/
public $safe = 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
* @param string $attribute the attribute being validated
*/
public function validateAttribute($object, $attribute)
{
}
}
...@@ -93,7 +93,7 @@ class UrlValidator extends Validator ...@@ -93,7 +93,7 @@ class UrlValidator extends Validator
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object being validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
* @return string the client-side validation script. * @return string the client-side validation script.
* @see CActiveForm::enableClientValidation * @see \yii\Web\ActiveForm::enableClientValidation
*/ */
public function clientValidateAttribute($object, $attribute) public function clientValidateAttribute($object, $attribute)
{ {
......
...@@ -9,67 +9,62 @@ ...@@ -9,67 +9,62 @@
namespace yii\validators; namespace yii\validators;
use yii\base\Component;
/** /**
* Validator is the base class for all validators. * Validator is the base class for all validators.
* *
* Child classes must override the [[validateAttribute]] method to provide the actual * Child classes should override the [[validateAttribute()]] method to provide the actual
* logic of performing data validation. Child classes may also override [[clientValidateAttribute]] * logic of performing data validation. Child classes may also override [[clientValidateAttribute()]]
* to provide client-side validation support. * to provide client-side validation support.
* *
* Validator defines the following properties that are common among concrete validators: * Validator declares a set of [[builtInValidators|built-in validators] which can
*
* - [[attributes]]: array, list of attributes to be validated;
* - [[message]]: string, the error message used when validation fails;
* - [[on]]: string, scenarios on which the validator applies.
*
* Validator also declares a set of [[builtInValidators|built-in validators] which can
* be referenced using short names. They are listed as follows: * be referenced using short names. They are listed as follows:
* *
* - `required`: [[RequiredValidator]]
* - `filter`: [[FilterValidator]]
* - `match`: [[RegularExpressionValidator]]
* - `email`: [[EmailValidator]]
* - `url`: [[UrlValidator]]
* - `unique`: [[UniqueValidator]]
* - `compare`: [[CompareValidator]]
* - `in`: [[RangeValidator]]
* - `boolean`: [[BooleanValidator]] * - `boolean`: [[BooleanValidator]]
* - `string`: [[StringValidator]]
* - `integer`: [[NumberValidator]]
* - `double`: [[NumberValidator]]
* - `date`: [[DateValidator]]
* - `file`: [[FileValidator]]
* - `captcha`: [[CaptchaValidator]] * - `captcha`: [[CaptchaValidator]]
* - `compare`: [[CompareValidator]]
* - `date`: [[DateValidator]]
* - `default`: [[DefaultValueValidator]] * - `default`: [[DefaultValueValidator]]
* - `double`: [[NumberValidator]]
* - `email`: [[EmailValidator]]
* - `exist`: [[ExistValidator]] * - `exist`: [[ExistValidator]]
* - `file`: [[FileValidator]]
* - `filter`: [[FilterValidator]]
* - `in`: [[RangeValidator]]
* - `integer`: [[NumberValidator]]
* - `match`: [[RegularExpressionValidator]]
* - `required`: [[RequiredValidator]]
* - `string`: [[StringValidator]]
* - `unique`: [[UniqueValidator]]
* - `url`: [[UrlValidator]]
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
abstract class Validator extends \yii\base\Component abstract class Validator extends Component
{ {
/** /**
* @var array list of built-in validators (name => class or configuration) * @var array list of built-in validators (name => class or configuration)
*/ */
public static $builtInValidators = array( public static $builtInValidators = array(
'required' => '\yii\validators\RequiredValidator', 'boolean' => 'yii\validators\BooleanValidator',
'match' => '\yii\validators\RegularExpressionValidator', 'captcha' => 'yii\validators\CaptchaValidator',
'email' => '\yii\validators\EmailValidator', 'compare' => 'yii\validators\CompareValidator',
'url' => '\yii\validators\UrlValidator', 'date' => 'yii\validators\DateValidator',
'filter' => '\yii\validators\FilterValidator', 'default' => 'yii\validators\DefaultValueValidator',
'captcha' => '\yii\validators\CaptchaValidator', 'double' => 'yii\validators\NumberValidator',
'default' => '\yii\validators\DefaultValueValidator', 'email' => 'yii\validators\EmailValidator',
'in' => '\yii\validators\RangeValidator', 'exist' => 'yii\validators\ExistValidator',
'boolean' => '\yii\validators\BooleanValidator', 'file' => 'yii\validators\FileValidator',
'string' => '\yii\validators\StringValidator', 'filter' => 'yii\validators\FilterValidator',
'integer' => '\yii\validators\IntegerValidator', 'in' => 'yii\validators\RangeValidator',
'double' => '\yii\validators\NumberValidator', 'integer' => 'yii\validators\IntegerValidator',
'compare' => '\yii\validators\CompareValidator', 'match' => 'yii\validators\RegularExpressionValidator',
'file' => '\yii\validators\FileValidator', 'required' => 'yii\validators\RequiredValidator',
'date' => '\yii\validators\DateValidator', 'string' => 'yii\validators\StringValidator',
'unique' => '\yii\validators\UniqueValidator', 'unique' => 'yii\validators\UniqueValidator',
'exist' => '\yii\validators\ExistValidator', 'url' => 'yii\validators\UrlValidator',
); );
/** /**
...@@ -85,30 +80,27 @@ abstract class Validator extends \yii\base\Component ...@@ -85,30 +80,27 @@ abstract class Validator extends \yii\base\Component
public $message; public $message;
/** /**
* @var array list of scenarios that the validator should be applied. * @var array list of scenarios that the validator should be applied.
* Each array value refers to a scenario name with the same name as its array key.
*/ */
public $on; public $on = array();
/** /**
* @var array list of scenarios that the validator should not be applied to. * @var array list of scenarios that the validator should not be applied to.
* Each array value refers to a scenario name with the same name as its array key.
*/ */
public $except; public $except = array();
/** /**
* @var boolean whether this validation rule should be skipped if the attribute being validated * @var boolean whether this validation rule should be skipped if the attribute being validated
* already has some validation error according to the previous rules. Defaults to true. * already has some validation error according to some previous rules. Defaults to true.
*/ */
public $skipOnError = true; public $skipOnError = true;
/** /**
* @var boolean whether to enable client-side validation. Defaults to true. * @var boolean whether to enable client-side validation. Defaults to null, meaning
* Please refer to [[\yii\web\ActiveForm::enableClientValidation]] for more details about * its actual value inherits from that of [[\yii\web\ActiveForm::enableClientValidation]].
* client-side validation.
*/ */
public $enableClientValidation = true; public $enableClientValidation;
/** /**
* Validates a single attribute. * Validates a single attribute.
* Child classes must implement this method to provide the actual validation logic. * Child classes must implement this method to provide the actual validation logic.
* @param \yii\base\Model $object the data object being validated * @param \yii\base\Model $object the data object to be validated
* @param string $attribute the name of the attribute to be validated. * @param string $attribute the name of the attribute to be validated.
*/ */
abstract public function validateAttribute($object, $attribute); abstract public function validateAttribute($object, $attribute);
...@@ -116,9 +108,9 @@ abstract class Validator extends \yii\base\Component ...@@ -116,9 +108,9 @@ abstract class Validator extends \yii\base\Component
/** /**
* Creates a validator object. * Creates a validator object.
* @param string $type the validator type. This can be a method name, * @param string $type the validator type. This can be a method name,
* a built-in validator name, a class name, or a path alias of validator class. * a built-in validator name, a class name, or a path alias of the validator class.
* @param \yii\base\Model $object the data object being validated. * @param \yii\base\Model $object the data object to be validated.
* @param mixed $attributes list of attributes to be validated. This can be either an array of * @param array|string $attributes list of attributes to be validated. This can be either an array of
* the attribute names or a string of comma-separated attribute names. * the attribute names or a string of comma-separated attribute names.
* @param array $params initial values to be applied to the validator properties * @param array $params initial values to be applied to the validator properties
* @return Validator the validator * @return Validator the validator
...@@ -129,32 +121,18 @@ abstract class Validator extends \yii\base\Component ...@@ -129,32 +121,18 @@ abstract class Validator extends \yii\base\Component
$attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY); $attributes = preg_split('/[\s,]+/', $attributes, -1, PREG_SPLIT_NO_EMPTY);
} }
if (isset($params['on'])) { if (isset($params['on']) && !is_array($params['on'])) {
if (is_array($params['on'])) { $params['on'] = preg_split('/[\s,]+/', $params['on'], -1, PREG_SPLIT_NO_EMPTY);
$on = $params['on'];
} else {
$on = preg_split('/[\s,]+/', $params['on'], -1, PREG_SPLIT_NO_EMPTY);
}
$params['on'] = empty($on) ? array() : array_combine($on, $on);
} else {
$params['on'] = array();
} }
if (isset($params['except'])) { if (isset($params['except']) && !is_array($params['except'])) {
if (is_array($params['except'])) { $params['except'] = preg_split('/[\s,]+/', $params['except'], -1, PREG_SPLIT_NO_EMPTY);
$except = $params['except'];
} else {
$except = preg_split('/[\s,]+/', $params['except'], -1, PREG_SPLIT_NO_EMPTY);
}
$params['except'] = empty($on) ? array() : array_combine($except, $except);
} else {
$params['except'] = array();
} }
if (method_exists($object, $type)) { if (method_exists($object, $type)) {
// method-based validator // method-based validator
$config = array( $config = array(
'class' => '\yii\validators\InlineValidator', 'class' => __NAMESPACE__ . '\InlineValidator',
'method' => $type, 'method' => $type,
'attributes' => $attributes, 'attributes' => $attributes,
); );
...@@ -170,9 +148,8 @@ abstract class Validator extends \yii\base\Component ...@@ -170,9 +148,8 @@ abstract class Validator extends \yii\base\Component
foreach ($params as $name => $value) { foreach ($params as $name => $value) {
$config[$name] = $value; $config[$name] = $value;
} }
$validator = \Yii::createObject($config);
return $validator; return \Yii::createObject($config);
} }
/** /**
...@@ -191,7 +168,7 @@ abstract class Validator extends \yii\base\Component ...@@ -191,7 +168,7 @@ abstract class Validator extends \yii\base\Component
$attributes = $this->attributes; $attributes = $this->attributes;
} }
foreach ($attributes as $attribute) { foreach ($attributes as $attribute) {
if (!$this->skipOnError || !$object->hasErrors($attribute)) { if (!($this->skipOnError && $object->hasErrors($attribute))) {
$this->validateAttribute($object, $attribute); $this->validateAttribute($object, $attribute);
} }
} }
...@@ -233,7 +210,7 @@ abstract class Validator extends \yii\base\Component ...@@ -233,7 +210,7 @@ abstract class Validator extends \yii\base\Component
*/ */
public function isActive($scenario) public function isActive($scenario)
{ {
return !isset($this->except[$scenario]) && (empty($this->on) || isset($this->on[$scenario])); return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
} }
/** /**
......
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