Commit a9003015 by Qiang Xue

Added Validator::except.

parent b1047e7f
...@@ -15,12 +15,18 @@ namespace yii\base; ...@@ -15,12 +15,18 @@ namespace yii\base;
* @author Qiang Xue <qiang.xue@gmail.com> * @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0 * @since 2.0
*/ */
abstract class ApplicationComponent extends Component class ApplicationComponent extends Component
{ {
/** /**
* Initializes the application component. * @var string unique ID of this application component
*/ */
public $id;
public function init() public function init()
{ {
parent::init();
if ($this->id === null) {
$this->id = get_class($this);
}
} }
} }
...@@ -283,8 +283,10 @@ class ErrorHandler extends ApplicationComponent ...@@ -283,8 +283,10 @@ class ErrorHandler extends ApplicationComponent
{ {
$category = get_class($exception); $category = get_class($exception);
if ($exception instanceof HttpException) { if ($exception instanceof HttpException) {
/** @var $exception HttpException */
$category .= '\\' . $exception->statusCode; $category .= '\\' . $exception->statusCode;
} elseif ($exception instanceof \ErrorException) { } elseif ($exception instanceof \ErrorException) {
/** @var $exception \ErrorException */
$category .= '\\' . $exception->getSeverity(); $category .= '\\' . $exception->getSeverity();
} }
\Yii::error((string)$exception, $category); \Yii::error((string)$exception, $category);
......
...@@ -93,6 +93,11 @@ abstract class Validator extends \yii\base\Component ...@@ -93,6 +93,11 @@ abstract class Validator extends \yii\base\Component
*/ */
public $on; public $on;
/** /**
* @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;
/**
* @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 the previous rules. Defaults to true.
*/ */
...@@ -144,6 +149,17 @@ abstract class Validator extends \yii\base\Component ...@@ -144,6 +149,17 @@ abstract class Validator extends \yii\base\Component
$params['on'] = array(); $params['on'] = array();
} }
if (isset($params['except'])) {
if (is_array($params['except'])) {
$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(
...@@ -225,11 +241,8 @@ abstract class Validator extends \yii\base\Component ...@@ -225,11 +241,8 @@ abstract class Validator extends \yii\base\Component
*/ */
public function applyTo($scenario, $attribute = null) public function applyTo($scenario, $attribute = null)
{ {
if ($attribute === null) { $applies = !isset($this->except[$scenario]) && (empty($this->on) || isset($this->on[$scenario]));
return empty($this->on) || isset($this->on[$scenario]); return $attribute === null ? $applies : $applies && in_array($attribute, $this->attributes, true);
} else {
return (empty($this->on) || isset($this->on[$scenario])) && in_array($attribute, $this->attributes, 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