Child classes should override the Validator::validateValue and/or Validator::validateAttribute methods to provide the actual
logic of performing data validation. Child classes may also override Validator::clientValidateAttribute
to provide client-side validation support.
Validator declares a set of [[builtInValidators|built-in validators] which can
be referenced using short names. They are listed as follows:
- boolean: BooleanValidator
- captcha: [[CaptchaValidator]]
- compare: CompareValidator
- date: DateValidator
- default: DefaultValueValidator
- double: NumberValidator
- email: EmailValidator
- exist: ExistValidator
- file: FileValidator
- filter: FilterValidator
- image: ImageValidator
- in: RangeValidator
- integer: NumberValidator
- match: RegularExpressionValidator
- required: RequiredValidator
- safe: SafeValidator
- string: StringValidator
- trim: FilterValidator
- unique: UniqueValidator
- url: UrlValidator
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('users', 'SIMPLE_PASSWORD'); } }
public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('yii', '{attribute} "{value}" has already been taken.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if (is_array($this->length)) { if (isset($this->length[0])) { $this->min = $this->length[0]; } if (isset($this->length[1])) { $this->max = $this->length[1]; } $this->length = null; } if ($this->encoding === null) { $this->encoding = Yii::$app->charset; } if ($this->message === null) { $this->message = Yii::t('yii', '{attribute} must be a string.'); } if ($this->min !== null && $this->tooShort === null) { $this->tooShort = Yii::t('yii', '{attribute} should contain at least {min, number} {min, plural, one{character} other{characters}}.'); } if ($this->max !== null && $this->tooLong === null) { $this->tooLong = Yii::t('yii', '{attribute} should contain at most {max, number} {max, plural, one{character} other{characters}}.'); } if ($this->length !== null && $this->notEqual === null) { $this->notEqual = Yii::t('yii', '{attribute} should contain {length, number} {length, plural, one{character} other{characters}}.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('wb_correios', 'CEP invalid.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = $this->requiredValue === null ? Yii::t('yii', '{attribute} cannot be blank.') : Yii::t('yii', '{attribute} must be "{requiredValue}".'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Module::t('ERROR_VALIDATE_ARRAY'); } }
/** * @inheritdoc */ public function validateAttribute($model, $attribute) { if ($this->range instanceof \Closure) { $this->range = call_user_func($this->range, $model, $attribute); } parent::validateAttribute($model, $attribute); }
/** * @inheritdoc */ public function init() { parent::init(); if (empty($this->className)) { throw InvalidConfigException('Property "className" must be provided'); } }
public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('app', 'Error on {attribute}.'); } }
/** * @inheritdoc */ public function attach($owner) { parent::attach($owner); $validators = $owner->validators; foreach ($this->rules as $rule) { if ($rule instanceof Validator) { $validators->append($rule); $this->validators[] = $rule; // keep a reference in behavior } elseif (is_array($rule) && isset($rule[0], $rule[1])) { // attributes, validator type $validator = Validator::createValidator($rule[1], $owner, (array) $rule[0], array_slice($rule, 2)); $validators->append($validator); $this->validators[] = $validator; // keep a reference in behavior } else { throw new InvalidConfigException('Invalid validation rule: a rule must specify both attribute names and validator type.'); } } $owner->on(ActiveRecord::EVENT_BEFORE_INSERT, function () { $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir); }); $owner->on(ActiveRecord::EVENT_BEFORE_UPDATE, function () { $this->saveRelatedDir($this->txt, $this->id_field, $this->name_field, $this->classDir); }); }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = \Yii::t('app', 'Неправильный формат телефона, ожидается формат +79001234567'); } }
/** * @inheritdoc */ public function attach($owner) { parent::attach($owner); $validators = $owner->validators; $validator = Validator::createValidator('safe', $owner, array_keys($this->attributes)); $validators->append($validator); }
/** * @inheritdoc */ public function init() { parent::init(); if (!$this->message) { $this->message = \Yii::t('app', 'Given value has bad format'); } }
public function init() { if (!$this->message) { $this->message = \Yii::t('yii', 'The format of {attribute} is invalid.'); } parent::init(); }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('yii', '{attribute} must be either "{true}" or "{false}".'); } }
/** * 各类型上传文件验证 * @param $attribute * @param $params */ public function checkFile($attribute, $params) { // 按照类型 验证上传 switch ($this->type) { case Media::TYPE_IMAGE: $rule = [[$attribute], 'file', 'skipOnEmpty' => false, 'extensions' => 'jpg', 'maxSize' => 1048576]; // 1MB break; case Media::TYPE_THUMB: $rule = [[$attribute], 'file', 'skipOnEmpty' => false, 'extensions' => 'jpg', 'maxSize' => 524288]; // 64KB break; case Media::TYPE_VOICE: $rule = [[$attribute], 'file', 'skipOnEmpty' => false, 'extensions' => 'amr, mp3', 'maxSize' => 2097152]; // 2MB break; case Media::TYPE_VIDEO: $rule = [[$attribute], 'file', 'skipOnEmpty' => false, 'extensions' => 'mp4', 'maxSize' => 10485760]; // 10MB break; default: return; } $validator = Validator::createValidator($rule[1], $this, (array) $rule[0], array_slice($rule, 2)); $validator->validateAttributes($this); }
/** * @inheritdoc */ public function attach($owner) { parent::attach($owner); if (!is_array($this->attributes) || empty($this->attributes)) { throw new InvalidParamException('Invalid or empty attributes array.'); } else { foreach ($this->attributes as $attribute => $config) { if (!isset($config['path']) || empty($config['path'])) { throw new InvalidParamException('Path must be set for all attributes.'); } if (!isset($config['tempPath']) || empty($config['tempPath'])) { throw new InvalidParamException('Temporary path must be set for all attributes.'); } if (!isset($config['url']) || empty($config['url'])) { $config['url'] = $this->publish($config['path']); } $this->attributes[$attribute]['path'] = FileHelper::normalizePath(Yii::getAlias($config['path'])) . DIRECTORY_SEPARATOR; $this->attributes[$attribute]['tempPath'] = FileHelper::normalizePath(Yii::getAlias($config['tempPath'])) . DIRECTORY_SEPARATOR; $this->attributes[$attribute]['url'] = rtrim($config['url'], '/') . '/'; $validator = Validator::createValidator('string', $this->owner, $attribute); $this->owner->validators[] = $validator; unset($validator); } } }
/** * @inheritdoc */ public function attach($owner) { parent::attach($owner); $validators = $this->owner->getValidators(); $validator = Validator::createValidator('safe', $this->owner, 'template_id'); $validators->append($validator); }
public function init() { parent::init(); if ($this->message === null) { $this->message = \Yii::t('errors', 'Wrong phone format.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->schemaEmpty === null) { $this->schemaEmpty = 'The "schema" property must be set.'; } if ($this->schemaNotString === null) { $this->schemaNotString = 'The "schema" property must be a a string.'; } if ($this->message === null) { $this->message = Yii::t('app', '{property}: {message}.'); } if ($this->notString === null) { $this->notString = Yii::t('app', 'The value must be a string.'); } if ($this->notJsonString === null) { $this->notJsonString = Yii::t('app', 'The value must be a valid JSON string.'); } if (empty($this->schema)) { throw new InvalidConfigException($this->schemaEmpty); } if (!is_string($this->schema)) { throw new InvalidConfigException($this->schemaNotString); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { switch ($this->operator) { case '==': $this->message = Yii::t('yii', '{attribute} must be repeated exactly.'); break; case '===': $this->message = Yii::t('yii', '{attribute} must be repeated exactly.'); break; case '!=': $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); break; case '!==': $this->message = Yii::t('yii', '{attribute} must not be equal to "{compareValue}".'); break; case '>': $this->message = Yii::t('yii', '{attribute} must be greater than "{compareValue}".'); break; case '>=': $this->message = Yii::t('yii', '{attribute} must be greater than or equal to "{compareValue}".'); break; case '<': $this->message = Yii::t('yii', '{attribute} must be less than "{compareValue}".'); break; case '<=': $this->message = Yii::t('yii', '{attribute} must be less than or equal to "{compareValue}".'); break; default: throw new InvalidConfigException("Unknown operator: {$this->operator}"); } } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('admin', '"{attribute}" must be a valid JSON'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = 'Error in expression'; } }
/** * @inheritdoc */ public function validateAttribute($model, $attribute) { if ($this->urlFilter) { $model->{$attribute} = $this->getCodeFromUrl($model->{$attribute}); } parent::validateAttribute($model, $attribute); }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('yii', '{attribute} is invalid.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('app', 'Неправильный формат поля {attribute}, имеются недопустимые символы.'); } }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->filter === null) { throw new InvalidConfigException('The "filter" property must be set.'); } }
public function init() { parent::init(); $this->_msgCat = 'creditcard'; $this->initI18N(__DIR__); $this->message = Yii::t('creditcard', 'Expiry is invalid.'); }
/** * @inheritdoc */ public function init() { parent::init(); if ($this->message === null) { $this->message = Yii::t('yii', 'The verification code is incorrect.'); } }
/** * Инициализация */ public function init() { parent::init(); if (empty($this->message)) { $this->message = Yii::t('main', 'Wrong uuid value'); } }