Browse Source

Added Validator::except.

tags/2.0.0-beta
Qiang Xue 13 years ago
parent
commit
a900301507
  1. 10
      framework/base/ApplicationComponent.php
  2. 2
      framework/base/ErrorHandler.php
  3. 23
      framework/validators/Validator.php

10
framework/base/ApplicationComponent.php

@ -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);
}
}
}

2
framework/base/ErrorHandler.php

@ -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);

23
framework/validators/Validator.php

@ -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);
}
/**

Loading…
Cancel
Save