|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416 |
- <?php
-
-
- namespace yii\validators;
-
- use Yii;
- use yii\base\Component;
- use yii\base\NotSupportedException;
-
-
- class Validator extends Component
- {
-
-
- public static $builtInValidators = [
- 'boolean' => 'yii\validators\BooleanValidator',
- 'captcha' => 'yii\captcha\CaptchaValidator',
- 'compare' => 'yii\validators\CompareValidator',
- 'date' => 'yii\validators\DateValidator',
- 'datetime' => [
- 'class' => 'yii\validators\DateValidator',
- 'type' => DateValidator::TYPE_DATETIME,
- ],
- 'time' => [
- 'class' => 'yii\validators\DateValidator',
- 'type' => DateValidator::TYPE_TIME,
- ],
- 'default' => 'yii\validators\DefaultValueValidator',
- 'double' => 'yii\validators\NumberValidator',
- 'each' => 'yii\validators\EachValidator',
- 'email' => 'yii\validators\EmailValidator',
- 'exist' => 'yii\validators\ExistValidator',
- 'file' => 'yii\validators\FileValidator',
- 'filter' => 'yii\validators\FilterValidator',
- 'image' => 'yii\validators\ImageValidator',
- 'in' => 'yii\validators\RangeValidator',
- 'integer' => [
- 'class' => 'yii\validators\NumberValidator',
- 'integerOnly' => true,
- ],
- 'match' => 'yii\validators\RegularExpressionValidator',
- 'number' => 'yii\validators\NumberValidator',
- 'required' => 'yii\validators\RequiredValidator',
- 'safe' => 'yii\validators\SafeValidator',
- 'string' => 'yii\validators\StringValidator',
- 'trim' => [
- 'class' => 'yii\validators\FilterValidator',
- 'filter' => 'trim',
- 'skipOnArray' => true,
- ],
- 'unique' => 'yii\validators\UniqueValidator',
- 'url' => 'yii\validators\UrlValidator',
- 'ip' => 'yii\validators\IpValidator',
- ];
-
-
- public $attributes = [];
-
-
- public $message;
-
-
- public $on = [];
-
-
- public $except = [];
-
-
- public $skipOnError = true;
-
-
- public $skipOnEmpty = true;
-
-
- public $enableClientValidation = true;
-
-
- public $isEmpty;
-
-
- public $when;
-
-
- public $whenClient;
-
-
-
-
- public static function createValidator($type, $model, $attributes, $params = [])
- {
- $params['attributes'] = $attributes;
-
- if ($type instanceof \Closure || $model->hasMethod($type)) {
-
- $params['class'] = __NAMESPACE__ . '\InlineValidator';
- $params['method'] = $type;
- } else {
- if (isset(static::$builtInValidators[$type])) {
- $type = static::$builtInValidators[$type];
- }
- if (is_array($type)) {
- $params = array_merge($type, $params);
- } else {
- $params['class'] = $type;
- }
- }
-
- return Yii::createObject($params);
- }
-
-
-
- public function init()
- {
- parent::init();
- $this->attributes = (array) $this->attributes;
- $this->on = (array) $this->on;
- $this->except = (array) $this->except;
- }
-
-
-
- public function validateAttributes($model, $attributes = null)
- {
- if (is_array($attributes)) {
- $newAttributes = [];
- foreach ($attributes as $attribute) {
- if (in_array($attribute, $this->attributes) || in_array('!' . $attribute, $this->attributes)) {
- $newAttributes[] = $attribute;
- }
- }
- $attributes = $newAttributes;
- } else {
- $attributes = [];
- foreach ($this->attributes as $attribute) {
- $attributes[] = $attribute[0] === '!' ? substr($attribute, 1) : $attribute;
- }
- }
-
- foreach ($attributes as $attribute) {
- $skip = $this->skipOnError && $model->hasErrors($attribute)
- || $this->skipOnEmpty && $this->isEmpty($model->$attribute);
- if (!$skip) {
- if ($this->when === null || call_user_func($this->when, $model, $attribute)) {
- $this->validateAttribute($model, $attribute);
- }
- }
- }
- }
-
-
-
- public function validateAttribute($model, $attribute)
- {
- $result = $this->validateValue($model->$attribute);
- if (!empty($result)) {
- $this->addError($model, $attribute, $result[0], $result[1]);
- }
- }
-
-
-
- public function validate($value, &$error = null)
- {
- $result = $this->validateValue($value);
- if (empty($result)) {
- return true;
- }
-
- list($message, $params) = $result;
- $params['attribute'] = Yii::t('yii', 'the input value');
- if (is_array($value)) {
- $params['value'] = 'array()';
- } elseif (is_object($value)) {
- $params['value'] = 'object';
- } else {
- $params['value'] = $value;
- }
- $error = Yii::$app->getI18n()->format($message, $params, Yii::$app->language);
-
- return false;
- }
-
-
-
- protected function validateValue($value)
- {
- throw new NotSupportedException(get_class($this) . ' does not support validateValue().');
- }
-
-
-
- public function clientValidateAttribute($model, $attribute, $view)
- {
- return null;
- }
-
-
-
- public function isActive($scenario)
- {
- return !in_array($scenario, $this->except, true) && (empty($this->on) || in_array($scenario, $this->on, true));
- }
-
-
-
- public function addError($model, $attribute, $message, $params = [])
- {
- $params['attribute'] = $model->getAttributeLabel($attribute);
- if (!isset($params['value'])) {
- $value = $model->$attribute;
- if (is_array($value)) {
- $params['value'] = 'array()';
- } elseif (is_object($value) && !method_exists($value, '__toString')) {
- $params['value'] = '(object)';
- } else {
- $params['value'] = $value;
- }
- }
- $model->addError($attribute, Yii::$app->getI18n()->format($message, $params, Yii::$app->language));
- }
-
-
-
- public function isEmpty($value)
- {
- if ($this->isEmpty !== null) {
- return call_user_func($this->isEmpty, $value);
- } else {
- return $value === null || $value === [] || $value === '';
- }
- }
- }
|