Commit a9003015 by Qiang Xue

Added Validator::except.

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